Files
trezor-suite/suite-native/react-native-graph/src/hooks/useEventTooltipProps.ts
Petr Knetl 7aa69dc8e2 Feat/graph interactions (#8857)
* chore(suite-native): upgrade react-native-graph to v. 1.0.2

* feat(react-native-graph): graph events added

`react-native-graph` can display graph events/interactions now.

The AnimatedLineGraph component accepts two new props `events` and `EventComponent`.

- `events` is an array of objects representing every individual interaction and it's metadata.
  Each of these objects contains a `date` property that is used to compute the event coordinates in the graph.
- `EventComponent` accepts the coordinates and metadata of each event and represent it in the graph

* refactor(suite-native): graph works with real date

We used to covert graph points `Date` objects to unix epoch time.
This commit makes the graph work with original dates as library intends.

* feat(suite-native): graph implements transaction events

Graph of Account detail displays transaction events.
2023-07-13 11:42:52 +00:00

35 lines
1.2 KiB
TypeScript

import { useState } from 'react';
import { EventTooltipComponentProps, GraphEventWithCords } from '../LineGraphProps';
/**
* Returns props for tooltip of active graph event.
*/
export const useEventTooltipProps = <TEventPayload extends object>(
eventsWithCords: GraphEventWithCords<TEventPayload>[] | null,
onEventHover?: () => void,
) => {
const [activeEventIndex, setActiveEventIndex] = useState<number | null>(null);
const handleDisplayEventTooltip = (eventIndex: number, isDisplayed: boolean) => {
if (activeEventIndex === eventIndex && !isDisplayed) setActiveEventIndex(null);
if (activeEventIndex === null && isDisplayed) {
onEventHover?.();
setActiveEventIndex(eventIndex);
}
};
const activeEvent =
eventsWithCords && typeof activeEventIndex === 'number'
? eventsWithCords[activeEventIndex]
: null;
const eventTooltipProps: EventTooltipComponentProps<TEventPayload> | null = activeEvent
? {
eventX: activeEvent.x,
eventY: activeEvent.y,
eventPayload: activeEvent.payload,
}
: null;
return { handleDisplayEventTooltip, eventTooltipProps };
};