Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import { useCallback, useState } from 'react';
import { WebviewHost } from '../webview-host';
/**
* React hook for webview state persistence. Wraps WebviewHost.getState/setState
* and provides React-style state management that persists across webview lifecycle.
*
* @example
* ```tsx
* function MyComponent() {
* const [state, setState] = useWebviewState({ count: 0 });
*
* return (
* <button onClick={() => setState({ count: state.count + 1 })}>
* Count: {state.count}
* </button>
* );
* }
* ```
*
* @param initialState The initial state to use if no persisted state exists.
* @returns A tuple of [state, setState] similar to React's useState.
*/
export function useWebviewState<T>(initialState: T): [T, (newState: T | ((prev: T) => T)) => void] {
const [state, setStateInternal] = useState<T>(() => {
const persisted = WebviewHost.getState();
return persisted ?? initialState;
});
const setState = useCallback((newState: T | ((prev: T) => T)) => {
setStateInternal(prev => {
const next = typeof newState === 'function'
? (newState as (prev: T) => T)(prev)
: newState;
WebviewHost.setState(next);
return next;
});
}, []);
return [state, setState];
}
|