Files
minstrel/web/src/lib/components/AcoustIDCard.svelte
T
bvandeusenandClaude Opus 5.5 10d50dccbd docs: web comments say what the app does, not how it came to be (#6277)
Rewrites the web app's comments to rule 32. Task, milestone, rule and
spec ids are gone; regression stories are restated as the trap they
guard (public auth routes, the menu's stopPropagation, universal loads
under adapter-static, metric deltas with their margin). Flutter and
Tauri mentions now name the Android app or a native shell, operator
quotes and dates are dropped, and one library's counts no longer
appear outside test fixtures. Verified with a TypeScript-scanner token
comparison (template literals and regex literals re-scanned): only
comment text changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:42:12 -04:00

238 lines
8.6 KiB
Svelte

<script lang="ts">
import { onMount } from 'svelte';
import {
getAcoustID,
listUnsettledAcoustID,
runAcoustIDLookup,
updateAcoustIDSettings,
type AcoustIDOverview,
type AcoustIDUnsettled
} from '#lib/api/admin.js';
import { errMessage } from '#lib/api/errors.js';
import { pushToast } from '#lib/stores/toast.svelte.js';
import SettingsGroup from '#lib/components/settings/SettingsGroup.svelte';
import SettingRow from '#lib/components/settings/SettingRow.svelte';
// AcoustID recording-id lookup: fills the MusicBrainz ids that untagged
// tracks lack, so the similarity arms can see them. Off until an admin adds
// their own key — a third-party account is never on by default — and it
// never gates anything else:
// when AcoustID is unreachable the card says so and nothing else notices.
let overview = $state<AcoustIDOverview | null>(null);
let enabled = $state(false);
let minScore = $state(0.85);
// Empty means "keep the stored key". The key itself never comes back.
let keyInput = $state('');
let saving = $state(false);
let starting = $state(false);
let loadError = $state(false);
let unsettled = $state<AcoustIDUnsettled[] | null>(null);
const scoreOk = $derived(Number.isFinite(minScore) && minScore >= 0.5 && minScore <= 1);
const dirty = $derived(
!!overview &&
(enabled !== overview.settings.enabled ||
minScore !== overview.settings.min_score ||
keyInput.trim() !== '')
);
const withoutId = $derived(
overview
? overview.coverage.total - overview.coverage.from_tag - overview.coverage.from_acoustid
: 0
);
async function load() {
try {
overview = await getAcoustID();
enabled = overview.settings.enabled;
minScore = overview.settings.min_score;
loadError = false;
} catch {
loadError = true;
}
}
// The figures move a track every second or two during a pass; a failed read
// keeps the last ones rather than blanking the card.
async function refresh() {
try {
overview = await getAcoustID();
} catch {
// Keep what we had.
}
}
onMount(() => {
load();
const timer = setInterval(refresh, 30_000);
return () => clearInterval(timer);
});
async function save() {
if (!scoreOk) return;
saving = true;
try {
const key = keyInput.trim();
await updateAcoustIDSettings({
enabled,
min_score: minScore,
...(key ? { api_key: key } : {})
});
keyInput = '';
pushToast('AcoustID settings saved.');
await load();
} catch (e) {
pushToast(errMessage(e), 'error');
} finally {
saving = false;
}
}
async function forgetKey() {
saving = true;
try {
await updateAcoustIDSettings({ enabled, min_score: minScore, api_key: '' });
pushToast('AcoustID key removed.');
await load();
} catch (e) {
pushToast(errMessage(e), 'error');
} finally {
saving = false;
}
}
async function runNow() {
starting = true;
try {
await runAcoustIDLookup();
pushToast('Looking up untagged tracks.');
await refresh();
} catch (e) {
pushToast(errMessage(e), 'error');
} finally {
starting = false;
}
}
async function toggleUnsettled(e: Event) {
if ((e.currentTarget as HTMLDetailsElement).open && unsettled === null) {
try {
unsettled = await listUnsettledAcoustID();
} catch (err) {
pushToast(errMessage(err), 'error');
}
}
}
const button =
'rounded-md border border-border px-3 py-1.5 text-sm text-text-secondary hover:bg-surface-hover hover:text-text-primary disabled:opacity-50';
const field = 'rounded-md border border-border bg-background px-3 py-1.5 text-sm';
</script>
<SettingsGroup title="AcoustID" id="acoustid" hint="Names untagged tracks from their sound, so radio and Songs-like can place them.">
{#if loadError}
<p class="px-4 py-3 text-sm text-error-fg">
Couldn't load AcoustID settings.
<button type="button" class="underline hover:no-underline" onclick={load}>Try again</button>
</p>
{:else if overview === null}
<p class="px-4 py-3 text-sm text-text-secondary">Loading AcoustID settings…</p>
{:else}
<SettingRow label="Status">
{#if overview.ready && !overview.status.problem}
<span class="text-sm text-text-secondary">{overview.status.running ? 'Looking up' : 'On'}</span>
{:else}
<span class="text-sm text-text-secondary" data-testid="acoustid-state">
{#if overview.ready}Needs attention{:else if !overview.settings.api_key_set}Needs a key{:else}Off{/if}
</span>
{/if}
</SettingRow>
{#if overview.status.problem}
<p class="px-4 py-3 text-sm text-error-fg" data-testid="acoustid-problem">{overview.status.problem}</p>
{/if}
<SettingRow label="Look up untagged tracks" for="acoustid-enabled" hint="Sends audio fingerprints to AcoustID.">
<input id="acoustid-enabled" type="checkbox" role="switch" aria-checked={enabled} bind:checked={enabled} class="h-4 w-4 cursor-pointer accent-accent" />
</SettingRow>
<SettingRow label="API key" for="acoustid-key">
{#snippet hint()}
Free for non-commercial use.
<a href="https://acoustid.org/new-application" target="_blank" rel="noopener noreferrer" class="underline hover:no-underline"
>Register an application</a
>
{/snippet}
<input
id="acoustid-key"
type="password"
autocomplete="off"
bind:value={keyInput}
placeholder={overview.settings.api_key_set ? 'Key saved' : 'Paste key'}
class="{field} w-40 font-mono"
/>
{#if overview.settings.api_key_set}
<button type="button" class={button} disabled={saving} onclick={forgetKey}>Remove</button>
{/if}
</SettingRow>
<SettingRow label="Minimum score" for="acoustid-score" hint="How sure AcoustID must be, 0.5 to 1.">
<input id="acoustid-score" type="number" min="0.5" max="1" step="0.01" bind:value={minScore} class="{field} w-24" />
</SettingRow>
<div class="flex flex-wrap items-center justify-end gap-2 px-4 py-3">
{#if !scoreOk}
<p class="mr-auto text-xs text-error-fg" data-testid="acoustid-problems">Minimum score must be from 0.5 to 1.</p>
{/if}
<button type="button" class={button} disabled={!dirty || saving || !scoreOk} onclick={save}>
{saving ? 'Saving…' : 'Save'}
</button>
</div>
{#if overview.coverage.total > 0}
{@const c = overview.coverage}
<div data-testid="acoustid-coverage">
<SettingRow
label="Recording ids"
hint="{c.from_tag.toLocaleString()} from tags · {c.from_acoustid.toLocaleString()} looked up · {withoutId.toLocaleString()} none"
/>
<SettingRow
label="Without one"
hint="{c.pending.toLocaleString()} waiting · {c.no_match.toLocaleString()} no match · {c.ambiguous.toLocaleString()} ambiguous · {c.failed.toLocaleString()} failed"
>
<button
type="button"
class={button}
disabled={!overview.ready || overview.status.running || starting}
title={overview.ready ? undefined : 'Switch the lookup on and add a key first'}
onclick={runNow}
>
{overview.status.running ? 'Looking up…' : 'Look up now'}
</button>
</SettingRow>
</div>
{#if c.ambiguous + c.failed > 0}
<details class="px-4 py-3" ontoggle={toggleUnsettled}>
<summary class="cursor-pointer text-sm text-text-primary">
Tracks the lookup couldn't settle ({(c.ambiguous + c.failed).toLocaleString()})
</summary>
{#if unsettled === null}
<p class="mt-2 text-sm text-text-secondary">Loading tracks…</p>
{:else}
<ul class="mt-2 divide-y divide-border text-sm" data-testid="acoustid-unsettled">
{#each unsettled as t (t.track_id)}
<li class="flex items-center justify-between gap-4 py-2">
<span class="min-w-0">
<span class="block truncate text-text-primary">{t.title}</span>
<span class="block truncate text-xs text-text-secondary">{t.artist} · {t.album}</span>
</span>
<span class="shrink-0 text-xs text-text-muted" title={t.detail ?? undefined}>
{t.state === 'ambiguous' ? `${t.candidates} recordings` : "couldn't fingerprint"}
</span>
</li>
{/each}
</ul>
{/if}
</details>
{/if}
{/if}
{/if}
</SettingsGroup>