import type { PlacesLibraryDuplicate } from '@trek/shared'; import { useCallback, useEffect, useState } from 'react'; import { OnlineOnlyError } from '../../repo/onlineOnly'; import { placeIdentitiesRepo } from '../../repo/placeIdentitiesRepo'; import type { TranslationFn } from '../../types'; import { getApiErrorMessage } from '../../utils/apiError'; import { useToast } from '../shared/Toast'; /** A pair's own name: its two keys in a fixed order, so either way round it is one pair. */ export function pairKey({ a, b }: Pick): string { return a.key <= b.key ? `${a.key}|${b.key}` : `${b.key}|${a.key}`; } /** * Places that look like the same place (PLC-13), behind the desktop dialog and the * phone sheet: the pairs the server suggests, a merge that keeps the picked side's * name and position and can be taken back, and "not the same" so a pair stays gone. * Nothing here writes into a trip or a list; the merge only groups the user's view. */ export function usePlaceDuplicates({ active, onChanged, t, }: { active: boolean; onChanged: () => void; t: TranslationFn; }) { const toast = useToast(); const [pairs, setPairs] = useState([]); const [hasMore, setHasMore] = useState(false); const [busy, setBusy] = useState(null); const [lastMerge, setLastMerge] = useState<{ id: number; name: string } | null>(null); const [seq, setSeq] = useState(0); // The request that last answered; loading while it is not the current one. const [settled, setSettled] = useState(null); useEffect(() => { if (!active) return; const ctrl = new AbortController(); placeIdentitiesRepo .duplicates({}, ctrl.signal) .then((res) => { if (ctrl.signal.aborted) return; setPairs(res.duplicates); setHasMore(res.next_cursor != null); }) .catch((err: unknown) => { if (ctrl.signal.aborted) return; setPairs([]); // Offline the count is simply not shown; any other failure says so. if (!(err instanceof OnlineOnlyError)) toast.error(getApiErrorMessage(err, t('common.error'))); }) .finally(() => { if (!ctrl.signal.aborted) setSettled(seq); }); return () => ctrl.abort(); }, [active, seq, toast, t]); const reload = useCallback(() => setSeq((n) => n + 1), []); const loading = active && settled !== seq; const run = async (pair: PlacesLibraryDuplicate, action: () => Promise) => { const key = pairKey(pair); if (busy) return; setBusy(key); try { await action(); setPairs((prev) => prev.filter((p) => pairKey(p) !== key)); onChanged(); } catch (err) { toast.error(getApiErrorMessage(err, t('common.error'))); } finally { setBusy(null); } }; /** Keeps `leading`'s name and position; the other side joins it. */ const merge = (pair: PlacesLibraryDuplicate, leading: 'a' | 'b') => run(pair, async () => { const lead = pair[leading]; const other = leading === 'a' ? pair.b : pair.a; const res = await placeIdentitiesRepo.merge(lead.key, other.key); setLastMerge({ id: res.merge_id, name: lead.name }); }); const dismiss = (pair: PlacesLibraryDuplicate) => run(pair, () => placeIdentitiesRepo.dismiss(pair.a.key, pair.b.key)); const undo = async () => { if (!lastMerge || busy) return; setBusy('undo'); try { await placeIdentitiesRepo.undoMerge(lastMerge.id); setLastMerge(null); onChanged(); reload(); } catch (err) { toast.error(getApiErrorMessage(err, t('common.error'))); } finally { setBusy(null); } }; return { pairs, hasMore, loading, busy, lastMerge, merge, dismiss, undo, reload }; }