| 27 | // Just like useStateWithDefaultIfNotSet, however the current state is changed whenever a given |
| 28 | // "parent" state is changed. The current state can otherwise be changed independently |
| 29 | export function useStateWithParentTrackingWithDefaultIfNotSet<T>( |
| 30 | parentValue: T, defaultCallback: () => T): [T, (v: T) => void] { |
| 31 | const [lastParentValue, setLastParentValue] = useState(parentValue); |
| 32 | const [explicitValue, setExplicitValue] = useState<T | null>(null); |
| 33 | |
| 34 | if (parentValue !== lastParentValue) { |
| 35 | setLastParentValue(parentValue); |
| 36 | setExplicitValue(parentValue); |
| 37 | return [parentValue, t => setExplicitValue(t)]; |
| 38 | } |
| 39 | |
| 40 | const value: T = explicitValue !== null ? explicitValue : defaultCallback(); |
| 41 | return [value, t => setExplicitValue(t)]; |
| 42 | } |
| 43 | |
| 44 | function getWindowSize() { |
| 45 | return { |