forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseChartInteractionState.ts
More file actions
129 lines (117 loc) · 3.65 KB
/
Copy pathuseChartInteractionState.ts
File metadata and controls
129 lines (117 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {useState} from 'react';
import type {SharedValue} from 'react-native-reanimated';
import {makeMutable, useAnimatedReaction} from 'react-native-reanimated';
import {scheduleOnRN} from 'react-native-worklets';
/**
* Input field type - matches Victory Native's InputFieldType
*/
type InputFieldType = string | number | Date;
/**
* Initial values for chart interaction state
*/
type ChartInteractionStateInit = {
x: InputFieldType;
y: Record<string, number>;
};
/**
* Chart interaction state structure - compatible with Victory's handleTouch function
*/
type ChartInteractionState<Init extends ChartInteractionStateInit> = {
/** Whether interaction (hover/press) is currently active */
isActive: SharedValue<boolean>;
/** Index of the matched data point (-1 if none) */
matchedIndex: SharedValue<number>;
/** X-axis value and position */
x: {
value: SharedValue<Init['x']>;
position: SharedValue<number>;
};
/** Y-axis values and positions for each y key */
y: Record<
keyof Init['y'],
{
value: SharedValue<number>;
position: SharedValue<number>;
}
>;
/** Y index for stacked bar charts */
yIndex: SharedValue<number>;
/** Raw cursor position */
cursor: {
x: SharedValue<number>;
y: SharedValue<number>;
};
};
/**
* Hook to track whether interaction is active as React state
*/
function useIsInteractionActive<Init extends ChartInteractionStateInit>(state: ChartInteractionState<Init>): boolean {
const [isInteractionActive, setIsInteractionActive] = useState(() => state.isActive.get());
useAnimatedReaction(
() => state.isActive.get(),
(val, oldVal) => {
if (val === oldVal) {
return;
}
scheduleOnRN(setIsInteractionActive, val);
},
);
return isInteractionActive;
}
/**
* Creates shared state for chart interactions (hover, tap, press).
* Compatible with Victory Native's handleTouch function exposed via actionsRef.
*
* @param initialValues - Initial x and y values matching your chart data structure
* @returns Object containing the interaction state and a boolean indicating if interaction is active
*
* @example
* ```tsx
* const { state, isActive } = useChartInteractionState({
* x: '',
* y: { value: 0 }
* });
*
* // Use with customGestures and actionsRef
* const hoverGesture = Gesture.Hover()
* .onUpdate((e) => {
* state.isActive.set(true);
* actionsRef.current?.handleTouch(state, e.x, e.y);
* })
* .onEnd(() => {
* state.isActive.set(false);
* });
* ```
*/
function useChartInteractionState<Init extends ChartInteractionStateInit>(
initialValues: Init,
): {
state: ChartInteractionState<Init>;
isActive: boolean;
} {
const yState = {} as Record<keyof Init['y'], {value: SharedValue<number>; position: SharedValue<number>}>;
for (const [key, initVal] of Object.entries(initialValues.y)) {
yState[key as keyof Init['y']] = {
value: makeMutable(initVal),
position: makeMutable(0),
};
}
const state: ChartInteractionState<Init> = {
isActive: makeMutable(false),
matchedIndex: makeMutable(-1),
x: {
value: makeMutable(initialValues.x),
position: makeMutable(0),
},
y: yState,
yIndex: makeMutable(-1),
cursor: {
x: makeMutable(0),
y: makeMutable(0),
},
};
const isActive = useIsInteractionActive(state);
return {state, isActive};
}
export {useChartInteractionState};
export type {ChartInteractionState, ChartInteractionStateInit};