({ children, around = commit => commit(), keepParams = [] }: {
children: (window: ProjectWindow<C>) => ReactNode;
around?: (commit: () => void) => void;
/** Parameter der ganzen Seite, die ein Wechsel in die neue Adresse mitnimmt (A5). */
keepParams?: readonly string[];
})
| 75 | interface Shown<C> { key: number; project: string; controller: AbortController; carried?: C } |
| 76 | |
| 77 | export function ProjectWindows<C>({ children, around = commit => commit(), keepParams = [] }: { |
| 78 | children: (window: ProjectWindow<C>) => ReactNode; |
| 79 | around?: (commit: () => void) => void; |
| 80 | /** Parameter der ganzen Seite, die ein Wechsel in die neue Adresse mitnimmt (A5). */ |
| 81 | keepParams?: readonly string[]; |
| 82 | }): JSX.Element { |
| 83 | const [shown, setShown] = useState<Shown<C>>(() => ({ key: 0, project: projectOfLocation(), controller: new AbortController() })); |
| 84 | const current = useRef(shown); current.current = shown; |
| 85 | const carry = useRef<C | undefined>(undefined); |
| 86 | const show = useCallback((project: string) => { |
| 87 | const previous = current.current; |
| 88 | if (project === previous.project) return; |
| 89 | const next: Shown<C> = { key: previous.key + 1, project, controller: new AbortController(), ...carry.current === undefined ? {} : { carried: carry.current } }; |
| 90 | current.current = next; |
| 91 | around(() => flushSync(() => setShown(next))); |
| 92 | previous.controller.abort(); |
| 93 | }, [around]); |
| 94 | const keep = useRef(keepParams); keep.current = keepParams; |
| 95 | const open = useCallback((project: string) => { |
| 96 | if (project === current.current.project) return; |
| 97 | window.history.pushState(null, '', projectSwitchHref(project, window.location.search, keep.current)); |
| 98 | show(project); |
| 99 | }, [show]); |
| 100 | const report = useCallback((value: C) => { carry.current = value; }, []); |
| 101 | useEffect(() => { |
| 102 | const back = () => show(projectOfLocation()); |
| 103 | window.addEventListener('popstate', back); |
| 104 | return () => window.removeEventListener('popstate', back); |
| 105 | }, [show]); |
| 106 | return <>{children({ key: shown.key, project: shown.project, signal: shown.controller.signal, carried: shown.carried, open, report })}</>; |
| 107 | } |
nothing calls this directly
no test coverage detected