Skip to content

Commit d07cd54

Browse files
committed
Added hook to create stable CSHs
1 parent d83a39f commit d07cd54

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

chartlets.js/src/demo/components/PanelsRow.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import { type ComponentChangeEvent, handleComponentChange } from "@/lib";
2-
import { usePanelStates } from "@/demo/hooks";
1+
import type { PanelState } from "@/demo/types";
2+
import { useComponentChangeHandlers, useContributions } from "@/lib/hooks";
33
import Panel from "./Panel";
44

55
function PanelsRow() {
6-
const panelStates = usePanelStates();
6+
const panelStates = useContributions<PanelState>("panels");
7+
const panelChangeHandlers = useComponentChangeHandlers(
8+
"panels",
9+
panelStates?.length || 0,
10+
);
711
if (!panelStates) {
812
// Ok, not ready yet
913
return null;
1014
}
1115

12-
const handlePanelChange = (
13-
panelIndex: number,
14-
panelEvent: ComponentChangeEvent,
15-
) => {
16-
handleComponentChange("panels", panelIndex, panelEvent);
17-
};
1816
const panels = panelStates.map((panelState, panelIndex) => {
1917
const { container, component, componentResult } = panelState;
2018
return (
@@ -24,7 +22,7 @@ function PanelsRow() {
2422
componentProps={component}
2523
componentStatus={componentResult.status}
2624
componentError={componentResult.error}
27-
onChange={(e) => handlePanelChange(panelIndex, e)}
25+
onChange={panelChangeHandlers[panelIndex]}
2826
/>
2927
);
3028
});

chartlets.js/src/demo/hooks.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

chartlets.js/src/lib/hooks.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import type { StoreState } from "@/lib/types/state/store";
22
import { store } from "@/lib/store";
33
import { useMemo } from "react";
44
import type { ContributionState } from "@/lib/types/state/contribution";
5+
import type {
6+
ComponentChangeEvent,
7+
ComponentChangeHandler,
8+
} from "@/lib/types/state/event";
9+
import { handleComponentChange } from "@/lib/actions/handleComponentChange";
510

611
const selectConfiguration = (state: StoreState) => state.configuration;
712

@@ -32,3 +37,44 @@ export function makeContributionsHook<S extends object = object>(
3237
}, [contributions]);
3338
};
3439
}
40+
41+
/**
42+
* A hook that retrieves the contributions for the given contribution
43+
* point given by `contribPoint`.
44+
*
45+
* @param contribPoint Contribution point name.
46+
* @typeParam S Type of the container state.
47+
*/
48+
export function useContributions<S extends object = object>(
49+
contribPoint: string,
50+
): ContributionState<S>[] {
51+
const contributionsRecord = useContributionsRecord();
52+
return contributionsRecord[contribPoint] as ContributionState<S>[];
53+
}
54+
55+
/**
56+
* A hook that creates an array of length `numContribs` with stable
57+
* component change handlers of type `ComponentChangeHandler` for
58+
* the contribution point given by `contribPoint`.
59+
*
60+
* @param contribPoint Contribution point name.
61+
* @param numContribs Number of contributions. This should be the length
62+
* of the array of contributions you get using the `useContributions` hook.
63+
*/
64+
export function useComponentChangeHandlers(
65+
contribPoint: string,
66+
numContribs: number,
67+
): ComponentChangeHandler[] {
68+
return useMemo(
69+
() =>
70+
Array.from({ length: numContribs }).map(
71+
(_, contribIndex) => (componentEvent: ComponentChangeEvent) =>
72+
void handleComponentChange(
73+
contribPoint,
74+
contribIndex,
75+
componentEvent,
76+
),
77+
),
78+
[contribPoint, numContribs],
79+
);
80+
}

chartlets.js/src/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ export {
1818
useExtensions,
1919
useContributionsResult,
2020
useContributionsRecord,
21+
useContributions,
22+
useComponentChangeHandlers,
2123
makeContributionsHook,
2224
} from "@/lib/hooks";

0 commit comments

Comments
 (0)