mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 01:12:16 +00:00
e539860530
The Sonar quality gate rated new code D. Preserve focus and keyboard interaction through native controls and shared dialog behavior. Replace ambiguous regex scans with bounded parsing and make numeric coercion and initialization explicit.
109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
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<PlacesLibraryDuplicate, 'a' | 'b'>): 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<PlacesLibraryDuplicate[]>([]);
|
|
const [hasMore, setHasMore] = useState(false);
|
|
const [busy, setBusy] = useState<string | null>(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<number | null>(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<void>) => {
|
|
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 };
|
|
}
|