import { useCallback, useMemo } from 'react' import type { SensitiveInfoOptions } from '../sensitive-info.nitro' import useAsync, { type UseAsyncResult } from './useAsync' import useStableOptions from './useStableOptions' /** * Shared "stable options → strip skip → memoize → useAsync" recipe used by every parameterized * data-fetching hook. Keeps `useHasSecret`/`useSecretItem` (and friends) down to a single call * site and ensures the abort/skip semantics stay consistent across the public surface. * * @internal */ const useAsyncQuery = ( runner: (request: SensitiveInfoOptions) => Promise, defaults: Required> & Partial>, operation: string, options: O | undefined, hint?: string ): UseAsyncResult => { const stable = useStableOptions(defaults as Partial, options) const { skip } = stable const requestOptions = useMemo(() => { const { skip: _skip, ...rest } = stable return rest as SensitiveInfoOptions }, [stable]) const run = useCallback( () => runner(requestOptions), [runner, requestOptions] ) return useAsync(run, operation, { skip, hint }) } export default useAsyncQuery