Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

792 B
Raw Blame
import { useCallback } from 'react';
import { useSearchParams } from 'react-router-dom';
import { readChoice, withParam } from '@/lib/urlState';

export interface UrlStateOptions {
  // typing replaces the history entry; a tab or page change adds one so Back returns to it
  history?: 'push' | 'replace';
}

export function useUrlChoice<T extends string>(key: string, choices: readonly T[], fallback: T, { history = 'push' }: UrlStateOptions = {}) {
  const [params, setParams] = useSearchParams();
  const value = readChoice(params, key, choices, fallback);
  const setValue = useCallback(
    (next: T) => setParams(current => withParam(current, key, next, fallback), { replace: history === 'replace' }),
    [setParams, key, fallback, history],
  );
  return [value, setValue] as const;
}