import React from 'react'; export function useControllableState({ value, defaultValue, onChange, }: { value: T | undefined; defaultValue: T; onChange?: ((nextValue: T) => void) | undefined; }) { const [uncontrolledValue, setUncontrolledValue] = React.useState(defaultValue); const isControlled = value !== undefined; const currentValue = isControlled ? value : uncontrolledValue; const setValue = React.useCallback( (nextValue: T) => { if (!isControlled) { setUncontrolledValue(nextValue); } onChange?.(nextValue); }, [isControlled, onChange], ); return [currentValue, setValue] as const; }