mirror of
https://github.com/Unleash/unleash.git
synced 2024-11-01 19:07:38 +01:00
a11cb72d99
* feat: persistent table query * project overview sort query * refactor: api methods as hook callbacks * persitent columns in project overview * enable new project overview * fix: refactor feature state change in overview * add type to sort * update e2e tests now takes 10% less time with use of cypress session * prevent sort reset on features list * fix feature toggle list loading * fix: update column state saving * update local storage hook test
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import React from 'react';
|
|
import { createGlobalState } from 'react-hooks-global-state';
|
|
import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage';
|
|
|
|
type UsePersistentGlobalState<T> = () => [
|
|
value: T,
|
|
setValue: React.Dispatch<React.SetStateAction<T>>
|
|
];
|
|
|
|
/**
|
|
* Create a hook that stores global state (shared across all hook instances).
|
|
* The state is also persisted to localStorage and restored on page load.
|
|
* The localStorage state is not synced between tabs.
|
|
*
|
|
* @deprecated `hooks/useLocalStorage` -- we don't need `react-hooks-global-state`
|
|
*/
|
|
export const createPersistentGlobalStateHook = <T extends object>(
|
|
key: string,
|
|
initialValue: T
|
|
): UsePersistentGlobalState<T> => {
|
|
const container = createGlobalState<{ [key: string]: T }>({
|
|
[key]: getLocalStorageItem<T>(key) ?? initialValue,
|
|
});
|
|
|
|
const setGlobalState = (value: React.SetStateAction<T>) => {
|
|
const prev = container.getGlobalState(key);
|
|
const next = value instanceof Function ? value(prev) : value;
|
|
container.setGlobalState(key, next);
|
|
setLocalStorageItem(key, next);
|
|
};
|
|
|
|
return () => [container.useGlobalState(key)[0], setGlobalState];
|
|
};
|