Repository Public
1"use client";2import { usePathname, useRouter, useSearchParams } from "next/navigation";3import { useCallback, useState } from "react";4import { useI18n } from "@/i18n";5import { TextField } from "@/ui";67/** Filters kept in the URL query (shareable, survives reload). Empty values are removed. */8export function useUrlState(): { get: (key: string) => string; set: (patch: Record<string, string | null>) => void; clear: (keys: readonly string[]) => void } {9 const params = useSearchParams();10 const pathname = usePathname();11 const router = useRouter();12 const set = useCallback(13 (patch: Record<string, string | null>) => {14 const next = new URLSearchParams(params.toString());15 for (const [k, v] of Object.entries(patch)) {16 if (v === null || v === "") next.delete(k);17 else next.set(k, v);18 }19 const qs = next.toString();20 router.replace(qs ? `${pathname}?${qs}` : pathname, { scroll: false });21 },22 [params, pathname, router],23 );24 return { get: (key) => params.get(key) ?? "", set, clear: (keys) => set(Object.fromEntries(keys.map((k) => [k, null]))) };25}2627/** Text filter committed on Enter or when focus leaves the field (no request per keystroke). */28export function CommitField({ label, value, onCommit, placeholder, mono }: { label: string; value: string; onCommit: (v: string) => void; placeholder?: string; mono?: boolean }) {29 const [draft, setDraft] = useState<{ base: string; text: string }>({ base: value, text: value });30 // when the committed value changes from outside (reset, navigation), the draft follows it31 const text = draft.base === value ? draft.text : value;32 const commit = () => {33 if (text.trim() !== value) onCommit(text.trim());34 };35 return (36 <form onSubmit={(e) => { e.preventDefault(); commit(); }} onBlur={commit}>37 <TextField label={label} type="search" icon="search" value={text} placeholder={placeholder} mono={mono ?? false} onChange={(v) => setDraft({ base: value, text: v })} />38 </form>39 );40}4142/** Relative time with the absolute local time (and zone) as title. */43export function When({ at }: { at: string | number }) {44 const { fmt } = useI18n();45 return <time className="num nowrap" dateTime={new Date(at).toISOString()} title={fmt.dateTime(at, { seconds: true })}>{fmt.relative(at)}</time>;46}47