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>
181 lines
6.0 KiB
Svelte
181 lines
6.0 KiB
Svelte
<script lang="ts">
|
|
import type { Snippet } from 'svelte';
|
|
import SettingsGroup from '#lib/components/settings/SettingsGroup.svelte';
|
|
import SettingRow from '#lib/components/settings/SettingRow.svelte';
|
|
import { errMessage } from '#lib/api/errors.js';
|
|
import { pushToast } from '#lib/stores/toast.svelte.js';
|
|
|
|
// One block for a list of outside sources, shared by cover art and tags.
|
|
// Each source is a row: its name and what it
|
|
// supplies, a switch, a key field when it takes one, Save and Test.
|
|
|
|
type Provider = {
|
|
id: string;
|
|
display_name: string;
|
|
requires_api_key: boolean;
|
|
supports: string[];
|
|
enabled: boolean;
|
|
api_key_set: boolean;
|
|
testable: boolean;
|
|
};
|
|
type TestResult = { ok: boolean; duration_ms?: number; error?: string };
|
|
type P = Provider;
|
|
type Query = {
|
|
data?: { providers: P[] };
|
|
isPending: boolean;
|
|
isError: boolean;
|
|
refetch: () => unknown;
|
|
};
|
|
|
|
let {
|
|
title,
|
|
hint,
|
|
noun,
|
|
query,
|
|
save,
|
|
test,
|
|
keyPlaceholder = 'Paste an API key',
|
|
note
|
|
}: {
|
|
title: string;
|
|
hint?: string;
|
|
// What the list is of, for the load error: "cover art sources".
|
|
noun: string;
|
|
query: Query;
|
|
save: (id: string, patch: { enabled?: boolean; api_key?: string }) => Promise<unknown>;
|
|
test: (id: string) => Promise<TestResult>;
|
|
keyPlaceholder?: string;
|
|
// A line under one source's name, when it needs one (where to get a key).
|
|
note?: Snippet<[P]>;
|
|
} = $props();
|
|
|
|
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';
|
|
|
|
// What a source supplies, in words rather than its capability codes.
|
|
const SUPPLIES: Record<string, string> = {
|
|
album_cover: 'album covers',
|
|
artist_thumb: 'artist photos',
|
|
artist_fanart: 'artist backdrops',
|
|
track_tags: 'track tags'
|
|
};
|
|
function supplies(p: P): string {
|
|
const words = p.supports.map((c) => SUPPLIES[c] ?? c.replace(/_/g, ' '));
|
|
return words.length ? words.join(', ').replace(/^./, (c) => c.toUpperCase()) : '';
|
|
}
|
|
|
|
// An admin's edits, kept across refetches until saved.
|
|
let local = $state<Record<string, { enabled: boolean; apiKey: string }>>({});
|
|
let results = $state<Record<string, TestResult>>({});
|
|
let saving = $state<Record<string, boolean>>({});
|
|
let testing = $state<Record<string, boolean>>({});
|
|
|
|
$effect(() => {
|
|
for (const p of query.data?.providers ?? []) {
|
|
if (!local[p.id]) local[p.id] = { enabled: p.enabled, apiKey: '' };
|
|
}
|
|
});
|
|
|
|
function changed(p: P): boolean {
|
|
const l = local[p.id];
|
|
return !!l && (l.enabled !== p.enabled || l.apiKey !== '');
|
|
}
|
|
|
|
async function onSave(p: P) {
|
|
const l = local[p.id];
|
|
if (!l || !changed(p)) return;
|
|
const patch: { enabled?: boolean; api_key?: string } = {};
|
|
if (l.enabled !== p.enabled) patch.enabled = l.enabled;
|
|
if (l.apiKey !== '') patch.api_key = l.apiKey;
|
|
saving[p.id] = true;
|
|
try {
|
|
await save(p.id, patch);
|
|
local[p.id] = { enabled: l.enabled, apiKey: '' };
|
|
} catch (e) {
|
|
pushToast(`Couldn't save ${p.display_name}: ${errMessage(e)}`, 'error');
|
|
} finally {
|
|
saving[p.id] = false;
|
|
}
|
|
}
|
|
|
|
async function onTest(p: P) {
|
|
testing[p.id] = true;
|
|
try {
|
|
results[p.id] = await test(p.id);
|
|
} catch (e) {
|
|
results[p.id] = { ok: false, error: errMessage(e) };
|
|
} finally {
|
|
testing[p.id] = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<SettingsGroup {title} {hint}>
|
|
{#if query.data}
|
|
{#each query.data.providers as p (p.id)}
|
|
{@const l = local[p.id]}
|
|
{@const r = results[p.id]}
|
|
<div data-testid="provider" data-provider={p.id}>
|
|
<SettingRow label={p.display_name} for="src-{p.id}">
|
|
{#snippet hint()}
|
|
{supplies(p)}
|
|
{#if note}<span class="block">{@render note(p)}</span>{/if}
|
|
{/snippet}
|
|
{#if l}
|
|
<input
|
|
id="src-{p.id}"
|
|
type="checkbox"
|
|
role="switch"
|
|
aria-checked={l.enabled}
|
|
bind:checked={l.enabled}
|
|
class="h-4 w-4 cursor-pointer accent-accent"
|
|
/>
|
|
{/if}
|
|
</SettingRow>
|
|
{#if l}
|
|
<div class="flex flex-wrap items-center gap-2 px-4 pb-3">
|
|
{#if p.requires_api_key}
|
|
<input
|
|
type="password"
|
|
aria-label="{p.display_name} API key"
|
|
placeholder={p.api_key_set ? 'Key saved · leave empty to keep it' : keyPlaceholder}
|
|
bind:value={l.apiKey}
|
|
class="w-64 max-w-full rounded-md border border-border bg-background px-3 py-1.5 text-sm"
|
|
/>
|
|
{/if}
|
|
<button
|
|
type="button"
|
|
disabled={!changed(p) || saving[p.id]}
|
|
onclick={() => onSave(p)}
|
|
class={button}>{saving[p.id] ? 'Saving…' : 'Save'}</button
|
|
>
|
|
{#if p.testable}
|
|
<button type="button" disabled={testing[p.id]} onclick={() => onTest(p)} class={button}>
|
|
{testing[p.id] ? 'Testing…' : 'Test'}
|
|
</button>
|
|
{/if}
|
|
{#if r}
|
|
<span class="text-xs {r.ok ? 'text-text-secondary' : 'text-error-fg'}" data-testid="provider-test">
|
|
{#if r.ok}
|
|
Works{r.duration_ms ? ` · answered in ${r.duration_ms} ms` : ''}
|
|
{:else}
|
|
Didn't work: {r.error}
|
|
{/if}
|
|
</span>
|
|
{/if}
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
{/each}
|
|
{:else if query.isPending}
|
|
<p class="px-4 py-3 text-sm text-text-secondary">Loading…</p>
|
|
{:else if query.isError}
|
|
<p class="px-4 py-3 text-sm text-error-fg" data-testid="providers-error">
|
|
Couldn't load the {noun}.
|
|
<button type="button" class="underline hover:no-underline" onclick={() => query.refetch()}>Try again</button>
|
|
</p>
|
|
{:else}
|
|
<p class="px-4 py-3 text-sm text-text-secondary">None available.</p>
|
|
{/if}
|
|
</SettingsGroup>
|