okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1237d9a5f
main
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 >>] { 11const [ value , setValue ] = useState < T >(() => ( kept . has ( key ) ?kept . get ( key ) as T :initial )); 12useEffect (() => { kept . set ( key , value ); }, [ key , value ]); 13const set = useCallback < Dispatch < SetStateAction < T >>>( next => setValue ( old => { 14const result = typeof next === 'function' ?( next as ( o :T ) => T )( old ) :next ; 15kept . set ( key , result ); 16return result ; 17}), [ key ]); 18return [ 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 ();