|
1 | 1 | import type { StoreState } from "@/lib/types/state/store"; |
2 | 2 | import { store } from "@/lib/store"; |
3 | | -import { useCallback, useMemo } from "react"; |
| 3 | +import { useMemo } from "react"; |
4 | 4 | import type { ContributionState } from "@/lib/types/state/contribution"; |
5 | | -import { |
6 | | - isTopLevelSelectionParameter, |
7 | | - type SignalHandler, |
8 | | -} from "@/lib/types/state/vega"; |
9 | | -import type { TopLevelSpec } from "vega-lite/src/spec"; |
10 | | -import { isString } from "@/lib/utils/isString"; |
11 | 5 | import type { |
12 | 6 | ComponentChangeEvent, |
13 | 7 | ComponentChangeHandler, |
@@ -44,104 +38,6 @@ export function makeContributionsHook<S extends object = object>( |
44 | 38 | }; |
45 | 39 | } |
46 | 40 |
|
47 | | -export function useSignalListeners( |
48 | | - chart: TopLevelSpec | null, |
49 | | - id: string | undefined, |
50 | | - onChange: ComponentChangeHandler, |
51 | | -): { [key: string]: SignalHandler } { |
52 | | - /* |
53 | | - Here, we loop through all the params to create map of signals which will |
54 | | - be then used to create the map of signal-listeners because not all |
55 | | - params are event-listeners, and we need to identify them. Later in the |
56 | | - code, we then see which handlers do we have so that we can create |
57 | | - those listeners with the `name` specified in the event-listener object. |
58 | | - */ |
59 | | - const signals: { [key: string]: string } = useMemo(() => { |
60 | | - if (!chart) return {}; |
61 | | - const tempSignals: { [key: string]: string } = {}; |
62 | | - chart.params?.forEach((param) => { |
63 | | - if (isTopLevelSelectionParameter(param)) { |
64 | | - if ( |
65 | | - typeof param.select === "object" && |
66 | | - "on" in param.select && |
67 | | - param.select.on != null |
68 | | - ) { |
69 | | - const signalName = param.select.on; |
70 | | - /* |
71 | | - The signal name extracted from the `select.on` property can be |
72 | | - either a string or of Stream type (internal Vega |
73 | | - type). But since we create the selection events in |
74 | | - Altair (in the server) using `on="click"` etc., the event type |
75 | | - will be a string, but we need this type-guard to be sure. |
76 | | - If it is a Stream object, that case is not handled yet. |
77 | | - */ |
78 | | - if (isString(signalName)) { |
79 | | - tempSignals[signalName] = param.name; |
80 | | - } else { |
81 | | - console.warn( |
82 | | - `The signal ${param} is of Stream type` + |
83 | | - " (internal Vega-lite type) which is not handled yet.", |
84 | | - ); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - }); |
89 | | - return tempSignals; |
90 | | - }, [chart]); |
91 | | - |
92 | | - const handleClickSignal = useCallback( |
93 | | - (signalName: string, signalValue: unknown) => { |
94 | | - if (id) { |
95 | | - return onChange({ |
96 | | - componentType: "Plot", |
97 | | - id: id, |
98 | | - property: signalName, |
99 | | - value: signalValue, |
100 | | - }); |
101 | | - } |
102 | | - }, |
103 | | - [id, onChange], |
104 | | - ); |
105 | | - |
106 | | - /* |
107 | | - Currently, we only have click events support, but if more are required, |
108 | | - they can be implemented and added in the map below. |
109 | | - */ |
110 | | - const signalHandlerMap: { [key: string]: SignalHandler } = useMemo( |
111 | | - () => ({ |
112 | | - click: handleClickSignal, |
113 | | - }), |
114 | | - [handleClickSignal], |
115 | | - ); |
116 | | - |
117 | | - /* |
118 | | - This function creates the map of signal listeners based on the `signals` |
119 | | - map computed above. |
120 | | - */ |
121 | | - const createSignalListeners = useCallback( |
122 | | - (signals: { [key: string]: string }) => { |
123 | | - const signalListeners: { [key: string]: SignalHandler } = {}; |
124 | | - Object.entries(signals).forEach(([event, signalName]) => { |
125 | | - if (signalHandlerMap[event]) { |
126 | | - signalListeners[signalName] = signalHandlerMap[event]; |
127 | | - } else { |
128 | | - console.warn( |
129 | | - "The signal " + event + " is not yet supported in chartlets.js", |
130 | | - ); |
131 | | - } |
132 | | - }); |
133 | | - |
134 | | - return signalListeners; |
135 | | - }, |
136 | | - [signalHandlerMap], |
137 | | - ); |
138 | | - |
139 | | - return useMemo( |
140 | | - () => createSignalListeners(signals), |
141 | | - [createSignalListeners, signals], |
142 | | - ); |
143 | | -} |
144 | | - |
145 | 41 | /** |
146 | 42 | * A hook that retrieves the contributions for the given contribution |
147 | 43 | * point given by `contribPoint`. |
|
0 commit comments