MCPcopy
hub / github.com/midrender/revideo / useStorage

Function useStorage

packages/ui/src/hooks/useStorage.ts:4–27  ·  view source on GitHub ↗
(
  id: string,
  initialState: T = null,
)

Source from the content-addressed store, hash-verified

2import {useApplication} from '../contexts';
3
4export function useStorage<T>(
5 id: string,
6 initialState: T = null,
7): [T, (newState: T) => void, boolean] {
8 const name = useApplication().project.name;
9 const key = `${name}-${id}`;
10 const [savedState, wasLoaded] = useMemo(() => {
11 const savedState = localStorage.getItem(key);
12 return savedState ? [JSON.parse(savedState), true] : [initialState, false];
13 }, [key]);
14 const [state, setState] = useState<T>(savedState);
15
16 const updateState = useCallback(
17 (newState: T) => {
18 if (key) {
19 localStorage.setItem(key, JSON.stringify(newState));
20 }
21 setState(newState);
22 },
23 [setState, key],
24 );
25
26 return [state, updateState, wasLoaded];
27}

Callers 5

ResizeableLayoutFunction · 0.90
ConsoleFunction · 0.90
TimelineFunction · 0.90
ExpandableFunction · 0.90
EditorPreviewFunction · 0.90

Calls 2

useApplicationFunction · 0.90
parseMethod · 0.45

Tested by

no test coverage detected