feat(frontend): duplicates view, field-by-field merge dialog, api module

Adds the duplicate-detection UI:

- api/duplicates.ts: getDuplicates / dismissDuplicate / resolveDuplicate, plus
  the cluster and merge-field types.
- /files/duplicates: an offset-paginated list of clusters. Each cluster shows its
  files (auth-loaded thumbnails via a reusable Thumb component); the user clicks a
  file to mark it the survivor, then per other file: Merge, Delete, or "Not a dup"
  (dismiss). The list reloads after each action so it stays consistent with the
  rescan-gated server state.
- DuplicateMergeDialog: a bottom sheet to merge two files field-by-field — each
  scalar from the kept or other file, metadata keep/other/merge, tags & pools
  keep-or-union, with a swap-survivor toggle and an optional trash-the-other box.
- Entry point: a Duplicates action in the files Header next to Trash.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-16 13:08:46 +03:00
parent 96a903aaff
commit dcbe640fae
6 changed files with 947 additions and 1 deletions
+1
View File
@@ -722,6 +722,7 @@
onFilterToggle={() => (filterOpen = !filterOpen)}
onUpload={() => uploader?.open()}
onTrash={() => goto('/files/trash')}
onDuplicates={() => goto('/files/duplicates')}
/>
{#if filterOpen}
@@ -0,0 +1,377 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { api } from '$lib/api/client';
import { getDuplicates, dismissDuplicate, type DuplicateCluster } from '$lib/api/duplicates';
import Thumb from '$lib/components/file/Thumb.svelte';
import DuplicateMergeDialog from '$lib/components/file/DuplicateMergeDialog.svelte';
import type { File } from '$lib/api/types';
const LIMIT = 20;
let clusters = $state<DuplicateCluster[]>([]);
let total = $state(0);
let loading = $state(false);
let initialLoaded = $state(false);
let error = $state('');
let busyKey = $state(''); // cluster currently performing an action
// Which file is the survivor for a given cluster (keyed by its file-id set).
let keepers = $state<Record<string, string>>({});
// Merge dialog state.
let mergeKeep = $state<File | null>(null);
let mergeDiscard = $state<File | null>(null);
$effect(() => {
if (!initialLoaded && !loading) void load();
});
function clusterKey(c: DuplicateCluster): string {
return c.files.map((f) => f.id).join(',');
}
function keeperId(c: DuplicateCluster): string {
return keepers[clusterKey(c)] ?? c.files[0]?.id ?? '';
}
async function load() {
if (loading) return;
loading = true;
error = '';
try {
const res = await getDuplicates(LIMIT, clusters.length);
clusters = [...clusters, ...(res.items ?? [])];
total = res.total ?? clusters.length;
} catch {
error = 'Failed to load duplicates';
} finally {
loading = false;
initialLoaded = true;
}
}
async function reload() {
clusters = [];
keepers = {};
total = 0;
initialLoaded = false;
await load();
}
function setKeeper(c: DuplicateCluster, id: string) {
keepers = { ...keepers, [clusterKey(c)]: id };
}
function openMerge(c: DuplicateCluster, other: File) {
const keep = c.files.find((f) => f.id === keeperId(c));
if (!keep) return;
mergeKeep = keep;
mergeDiscard = other;
}
async function deleteFile(c: DuplicateCluster, id: string) {
if (busyKey) return;
busyKey = clusterKey(c);
try {
await api.post('/files/bulk/delete', { file_ids: [id] });
await reload();
} catch {
error = 'Failed to delete file';
} finally {
busyKey = '';
}
}
async function notDuplicate(c: DuplicateCluster, other: File) {
if (busyKey) return;
busyKey = clusterKey(c);
try {
await dismissDuplicate(keeperId(c), other.id);
await reload();
} catch {
error = 'Failed to dismiss pair';
} finally {
busyKey = '';
}
}
function onMerged() {
mergeKeep = null;
mergeDiscard = null;
void reload();
}
let hasMore = $derived(clusters.length < total);
</script>
<svelte:head>
<title>Duplicates | Tanabata</title>
</svelte:head>
<div class="page">
<header>
<button class="back" onclick={() => goto('/files')} aria-label="Back to files">
<svg width="18" height="18" viewBox="0 0 18 18" fill="none" aria-hidden="true">
<path
d="M11 4l-5 5 5 5"
stroke="currentColor"
stroke-width="1.8"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
<span class="htitle">Duplicates{total ? ` (${total})` : ''}</span>
<button class="refresh" onclick={reload} title="Refresh" aria-label="Refresh">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path
d="M13 8a5 5 0 1 1-1.5-3.5M13 2v3h-3"
stroke="currentColor"
stroke-width="1.6"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
</header>
<main>
{#if error}<p class="error" role="alert">{error}</p>{/if}
{#if initialLoaded && clusters.length === 0 && !error}
<div class="empty">
<p>No duplicates found.</p>
<p class="hint">
The list reflects the last <code>dedup</code> run. New uploads appear after the next rescan.
</p>
</div>
{/if}
{#each clusters as c (clusterKey(c))}
{@const keep = keeperId(c)}
<section class="cluster" class:busy={busyKey === clusterKey(c)}>
<div class="files">
{#each c.files as f (f.id)}
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
<div
class="file"
class:keep={f.id === keep}
role="button"
tabindex="0"
onclick={() => setKeeper(c, f.id)}
title="Click to keep this one"
>
<Thumb id={f.id} size={96} alt={f.original_name ?? ''} />
{#if f.id === keep}<span class="kbadge">Keep</span>{/if}
<span class="fname" title={f.original_name ?? ''}>{f.original_name ?? '—'}</span>
<span class="fmeta">{f.mime_type} · {f.tags?.length ?? 0} tags</span>
</div>
{/each}
</div>
<div class="actions">
{#each c.files.filter((f) => f.id !== keep) as other (other.id)}
<div class="actrow">
<span class="aname" title={other.original_name ?? ''}>{other.original_name ?? '—'}</span>
<button class="abtn" onclick={() => openMerge(c, other)}>Merge</button>
<button class="abtn" onclick={() => deleteFile(c, other.id)}>Delete</button>
<button class="abtn ghost" onclick={() => notDuplicate(c, other)}>Not a dup</button>
</div>
{/each}
</div>
</section>
{/each}
{#if hasMore}
<button class="more" onclick={load} disabled={loading}>
{loading ? 'Loading…' : 'Load more'}
</button>
{:else if loading && !initialLoaded}
<p class="loadingp">Loading…</p>
{/if}
</main>
</div>
{#if mergeKeep && mergeDiscard}
<DuplicateMergeDialog
keep={mergeKeep}
discard={mergeDiscard}
onResolved={onMerged}
onClose={() => {
mergeKeep = null;
mergeDiscard = null;
}}
/>
{/if}
<style>
.page {
display: flex;
flex-direction: column;
height: 100%;
}
header {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
background-color: var(--color-bg-primary);
border-bottom: 1px solid color-mix(in srgb, var(--color-accent) 15%, transparent);
position: sticky;
top: 0;
z-index: 10;
flex-shrink: 0;
}
.back,
.refresh {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 7px;
border: 1px solid color-mix(in srgb, var(--color-accent) 25%, transparent);
background-color: var(--color-bg-elevated);
color: var(--color-text-muted);
cursor: pointer;
}
.back:hover,
.refresh:hover {
color: var(--color-text-primary);
border-color: var(--color-accent);
}
.htitle {
flex: 1;
font-size: 0.95rem;
font-weight: 600;
}
main {
flex: 1;
overflow-y: auto;
padding: 10px 12px calc(72px + env(safe-area-inset-bottom, 0px));
}
.error {
color: var(--color-danger);
font-size: 0.9rem;
text-align: center;
}
.empty {
text-align: center;
color: var(--color-text-muted);
padding: 40px 16px;
}
.empty .hint {
font-size: 0.82rem;
opacity: 0.8;
}
code {
font-family: monospace;
background-color: var(--color-bg-elevated);
padding: 0 4px;
border-radius: 4px;
}
.cluster {
background-color: var(--color-bg-secondary);
border-radius: 12px;
padding: 12px;
margin-bottom: 12px;
}
.cluster.busy {
opacity: 0.55;
pointer-events: none;
}
.files {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 10px;
}
.file {
display: flex;
flex-direction: column;
align-items: center;
gap: 3px;
width: 96px;
cursor: pointer;
border-radius: 10px;
padding: 4px;
border: 2px solid transparent;
}
.file.keep {
border-color: var(--color-accent);
background-color: color-mix(in srgb, var(--color-accent) 10%, transparent);
}
.kbadge {
font-size: 0.68rem;
font-weight: 600;
color: var(--color-accent);
}
.fname {
font-size: 0.74rem;
max-width: 96px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: var(--color-text-primary);
}
.fmeta {
font-size: 0.68rem;
color: var(--color-text-muted);
max-width: 96px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.actions {
display: flex;
flex-direction: column;
gap: 6px;
}
.actrow {
display: flex;
align-items: center;
gap: 6px;
}
.aname {
flex: 1;
min-width: 0;
font-size: 0.78rem;
color: var(--color-text-muted);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.abtn {
padding: 5px 10px;
border-radius: 7px;
border: 1px solid color-mix(in srgb, var(--color-accent) 25%, transparent);
background-color: var(--color-bg-elevated);
color: var(--color-text-primary);
font-size: 0.78rem;
font-family: inherit;
cursor: pointer;
flex-shrink: 0;
}
.abtn:hover {
border-color: var(--color-accent);
}
.abtn.ghost {
color: var(--color-text-muted);
}
.more {
display: block;
width: 100%;
padding: 10px;
border-radius: 8px;
border: 1px solid color-mix(in srgb, var(--color-accent) 25%, transparent);
background-color: var(--color-bg-elevated);
color: var(--color-text-primary);
font-family: inherit;
font-size: 0.85rem;
cursor: pointer;
}
.loadingp {
text-align: center;
color: var(--color-text-muted);
font-size: 0.9rem;
}
</style>