okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221downloads and models: live progress, parts counted, choices kept, clear NVFP4 names, More versions list1237d9a5f

main
1.2 KiB23 linesraw
1import { useCallback, useEffect, useState, type Dispatch, type SetStateAction } from 'react';
2
3/**
4 * A piece of screen state that survives leaving the screen. The Models page keeps its choices (version, context, computer use, picture
5 * reading ...) in useState, so going to Monitor and back reset them and a person lost track of what they were downloading. Kept values
6 * live in memory for the whole session (not on disk: a new start begins from the installed model again).
7 */
8const kept = new Map<string, unknown>();
9
10export function useKept<T>(key: string, initial: T): [T, Dispatch<SetStateAction<T>>] {
11  const [value, setValue] = useState<T>(() => (kept.has(key) ? kept.get(key) as T : initial));
12  useEffect(() => { kept.set(key, value); }, [key, value]);
13  const set = useCallback<Dispatch<SetStateAction<T>>>(next => setValue(old => {
14    const result = typeof next === 'function' ? (next as (o: T) => T)(old) : next;
15    kept.set(key, result);
16    return result;
17  }), [key]);
18  return [value, set];
19}
20
21export const keptValue = <T,>(key: string): T | undefined => kept.get(key) as T | undefined;
22/** Forget everything kept (tests, and a changed installation). */
23export const forgetKept = () => kept.clear();