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