import { useCallback, useMemo } from 'react'; import { useUrlState } from './useUrlState'; type Unpacked = T extends (infer U)[] ? U : T; export const useUrlStateExclusiveOptions = >(name: string, options?: AT, defaultValue?: Unpacked) => { const lowerCaseOptions = useMemo(() => options?.map(o => o.trim().toLowerCase()), [ options ]); const serializer = useCallback((val: Unpacked | undefined) => { const value = val?.trim().toLowerCase(); if (value) { if (lowerCaseOptions && options) { const index = lowerCaseOptions.indexOf(value); if (index !== -1) { return options[index]; } } else { return val; } } return undefined; }, [ lowerCaseOptions, options ]); const deserializer = useCallback((val: string | undefined) => { const value = val?.trim(); if (value) { if (options && lowerCaseOptions) { const index = lowerCaseOptions.indexOf(value.toLowerCase()); if (index !== -1) { return options[index] as Unpacked; } } else { return value as Unpacked; } } return undefined; }, [ lowerCaseOptions, options ]); return useUrlState>(name, serializer, deserializer, defaultValue); };