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