Files
2025-08-02 16:30:27 +02:00

36 lines
870 B
TypeScript

import { createContext, useContext } from 'react';
import type { Section } from './SectionRow';
export type Cache = {
topLevelSections: Section[];
_query: Record<string, unknown>;
_fetchTopLevelSections: () => void;
};
type CacheContext = [Cache, (p: Cache | ((prev: Cache) => Cache)) => void];
const CacheContext = createContext<CacheContext>([
{
topLevelSections: [],
_query: {},
_fetchTopLevelSections: () => {},
},
() => {},
]);
export const Provider = CacheContext.Provider;
export function useCache<Key extends undefined>(): CacheContext;
export function useCache<Key extends keyof Cache>(key: Key): Cache[Key];
export function useCache<Key extends keyof Cache>(key?: Key) {
const cacheContext = useContext(CacheContext);
if (key === undefined) {
return cacheContext;
}
return cacheContext[0][key];
}