Files
quality_analyse/web/src/pageCache.tsx
T

20 lines
834 B
TypeScript

import { createContext, useContext, useEffect, useRef } from 'react';
export const WORKSPACE_INVALIDATED = 'quality-workspace-invalidated';
export const PageCacheContext = createContext({ active: true, initialSearch: undefined as string | undefined, refreshKey: 0, onLocationChange: (_search: string) => {} });
export const usePageCache = () => useContext(PageCacheContext);
/** Revalidate retained maintenance data on a real revision change, not on every visit. */
export function useMaintenanceRefresh(reload: () => void) {
const { active, refreshKey } = usePageCache();
const seen = useRef(refreshKey);
const latest = useRef(reload);
latest.current = reload;
useEffect(() => {
if (active && seen.current !== refreshKey) {
seen.current = refreshKey;
latest.current();
}
}, [active, refreshKey]);
}