2024-03-18 13:58:05 +01:00
|
|
|
import type React from 'react';
|
2022-02-23 15:08:44 +01:00
|
|
|
import { createGlobalState } from 'react-hooks-global-state';
|
|
|
|
|
|
|
|
type UseGlobalState<T> = () => [
|
|
|
|
value: T,
|
2023-10-02 14:25:46 +02:00
|
|
|
setValue: React.Dispatch<React.SetStateAction<T>>,
|
2022-02-23 15:08:44 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
// Create a hook that stores global state (shared across all hook instances).
|
|
|
|
export const createGlobalStateHook = <T>(
|
|
|
|
key: string,
|
2023-10-02 14:25:46 +02:00
|
|
|
initialValue: T,
|
2022-02-23 15:08:44 +01:00
|
|
|
): UseGlobalState<T> => {
|
|
|
|
const container = createGlobalState<{ [key: string]: T }>({
|
|
|
|
[key]: initialValue,
|
|
|
|
});
|
|
|
|
|
|
|
|
const setGlobalState = (value: React.SetStateAction<T>) => {
|
|
|
|
container.setGlobalState(key, value);
|
|
|
|
};
|
|
|
|
|
|
|
|
return () => [container.useGlobalState(key)[0], setGlobalState];
|
|
|
|
};
|