@@ -2,6 +2,11 @@ import type { StoreState } from "@/lib/types/state/store";
22import { store } from "@/lib/store" ;
33import { useMemo } from "react" ;
44import 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
611const 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+ }
0 commit comments