diff --git a/web/src/lib/components/AcoustIDCard.svelte b/web/src/lib/components/AcoustIDCard.svelte index 88fbbf1d..d5b245b1 100644 --- a/web/src/lib/components/AcoustIDCard.svelte +++ b/web/src/lib/components/AcoustIDCard.svelte @@ -12,6 +12,7 @@ import { pushToast } from '#lib/stores/toast.svelte.js'; import SettingsGroup from '#lib/components/settings/SettingsGroup.svelte'; import SettingRow from '#lib/components/settings/SettingRow.svelte'; + import SettingsLoadStatus from '#lib/components/settings/SettingsLoadStatus.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 @@ -131,13 +132,8 @@ - {#if loadError} -

- Couldn't load AcoustID settings. - -

- {:else if overview === null} -

Loading AcoustID settings…

+ {#if loadError || overview === null} + {:else} {#if overview.ready && !overview.status.problem} diff --git a/web/src/lib/components/FingerprintSettingsCard.svelte b/web/src/lib/components/FingerprintSettingsCard.svelte index 14b0cf19..c21665de 100644 --- a/web/src/lib/components/FingerprintSettingsCard.svelte +++ b/web/src/lib/components/FingerprintSettingsCard.svelte @@ -1,14 +1,13 @@ - {#if loadError} -

- Couldn't load fingerprinting settings. - -

- {:else if form === null} -

Loading…

+ {#if f.loadError || f.form === null} + {:else} - + - + - + % - + - + {#if lengthChanged} - {settings.detected_client_ip || 'unknown'} + {f.saved.detected_client_ip || 'unknown'} - {settings.remote_addr || 'unknown'} + {f.saved.remote_addr || 'unknown'} - {settings.forwarded_chain || 'none'} + {f.saved.forwarded_chain || 'none'} diff --git a/web/src/lib/components/NetworkSettingsCard.test.ts b/web/src/lib/components/NetworkSettingsCard.test.ts index 0dfba26a..3f35a904 100644 --- a/web/src/lib/components/NetworkSettingsCard.test.ts +++ b/web/src/lib/components/NetworkSettingsCard.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test, vi, beforeEach } from 'vitest'; import { render, screen, fireEvent, waitFor } from '@testing-library/svelte'; import NetworkSettingsCard from './NetworkSettingsCard.svelte'; +import { pushToast } from '#lib/stores/toast.svelte.js'; const getNetworkSettings = vi.fn(); const updateNetworkSettings = vi.fn(); @@ -97,6 +98,26 @@ describe('NetworkSettingsCard', () => { expect(await screen.findByText(/Count your proxies/)).toBeTruthy(); }); + test('a refused save says why, in the server\'s words', async () => { + getNetworkSettings.mockResolvedValue(settings()); + updateNetworkSettings.mockRejectedValue({ + code: 'invalid_setting', + message: 'trusted proxy hops must be from 0 to 10' + }); + render(NetworkSettingsCard); + + const input = await screen.findByRole('spinbutton'); + await fireEvent.input(input, { target: { value: '2' } }); + const save = screen.getByRole('button', { name: /Save/ }); + await waitFor(() => expect(save).not.toBeDisabled()); + await fireEvent.click(save); + + await waitFor(() => expect(pushToast).toHaveBeenCalled()); + const [text, kind] = vi.mocked(pushToast).mock.calls[0]; + expect(kind).toBe('error'); + expect(text).toContain('trusted proxy hops must be from 0 to 10'); + }); + test('offers a retry when loading fails', async () => { getNetworkSettings.mockRejectedValue(new Error('boom')); render(NetworkSettingsCard); diff --git a/web/src/lib/components/NotificationEmailCard.svelte b/web/src/lib/components/NotificationEmailCard.svelte index ddbe36d5..e544a1c1 100644 --- a/web/src/lib/components/NotificationEmailCard.svelte +++ b/web/src/lib/components/NotificationEmailCard.svelte @@ -1,33 +1,27 @@ - {#if loadError} -

- Couldn't load the email schedule. - -

- {:else if form === null} -

Loading the email schedule…

+ {#if f.loadError || f.form === null} + {:else} - {#each hours as h (h)} {/each} - + minutes
-
{/if} diff --git a/web/src/lib/components/PublicAddressCard.svelte b/web/src/lib/components/PublicAddressCard.svelte index feb2ed72..0bbc8f23 100644 --- a/web/src/lib/components/PublicAddressCard.svelte +++ b/web/src/lib/components/PublicAddressCard.svelte @@ -1,61 +1,33 @@ - {#if loadError} -

- Couldn't load the public address. - -

- {:else if saved === null} -

Loading…

+ {#if f.loadError || f.saved === null} + {:else} - - {#if here && value.trim() !== here} + {#if here && f.form?.trim() !== here} - + {/if} - {#if !saved} + {#if !f.saved?.public_url}

Not set, so password-reset emails are not being sent. diff --git a/web/src/lib/components/settings/SettingsLoadStatus.svelte b/web/src/lib/components/settings/SettingsLoadStatus.svelte new file mode 100644 index 00000000..5434dbc0 --- /dev/null +++ b/web/src/lib/components/settings/SettingsLoadStatus.svelte @@ -0,0 +1,14 @@ + + +{#if failed} +

+ Couldn't load {what}. + +

+{:else} +

Loading…

+{/if} diff --git a/web/src/lib/settings/settingsForm.svelte.ts b/web/src/lib/settings/settingsForm.svelte.ts new file mode 100644 index 00000000..0dfbfba6 --- /dev/null +++ b/web/src/lib/settings/settingsForm.svelte.ts @@ -0,0 +1,90 @@ +import { onMount } from 'svelte'; +import { errMessage } from '#lib/api/errors.js'; +import { pushToast } from '#lib/stores/toast.svelte.js'; + +// The load → edit → save cycle every admin settings card runs: fetch the +// stored settings when the card mounts, hold an editable copy, say when the copy differs, save it +// and adopt what the server stored, and toast the outcome. A failed save +// toasts the server's reason (errMessage), so a refused value says why. + +export type SettingsForm = { + /** The settings as the server last returned them; null until loaded. */ + readonly saved: S | null; + /** The editable copy; bind inputs to it. Null until loaded. */ + form: F | null; + readonly loadError: boolean; + readonly saving: boolean; + /** Whether the form differs from what is stored. */ + readonly dirty: boolean; + load(): Promise; + save(): Promise; +}; + +export function createSettingsForm(opts: { + load: () => Promise; + save: (form: F) => Promise; + /** The editable copy of stored settings. Default: a shallow copy. */ + toForm?: (saved: S) => F; + /** Default: the form differs from toForm(saved), compared as JSON. */ + isDirty?: (saved: S, form: F) => boolean; + /** Checked before a save; false leaves the form as it is, unsent. */ + canSave?: (form: F) => boolean; + savedMessage: string | ((saved: S) => string); + /** The toast when a save fails with no message of its own. */ + failMessage?: string; + onSaved?: (saved: S) => void | Promise; +}): SettingsForm { + const toForm = opts.toForm ?? ((s: S) => ({ ...(s as object) }) as F); + const isDirty = + opts.isDirty ?? ((s: S, f: F) => JSON.stringify(toForm(s)) !== JSON.stringify(f)); + + let saved = $state(null); + let form = $state(null); + let loadError = $state(false); + let saving = $state(false); + const dirty = $derived(saved !== null && form !== null && isDirty(saved, form)); + + function adopt(s: S) { + saved = s; + form = toForm(s); + } + + async function load() { + try { + adopt(await opts.load()); + loadError = false; + } catch { + loadError = true; + } + } + + async function save() { + if (form === null || (opts.canSave && !opts.canSave(form))) return; + saving = true; + try { + const s = await opts.save($state.snapshot(form) as F); + adopt(s); + pushToast(typeof opts.savedMessage === 'function' ? opts.savedMessage(s) : opts.savedMessage); + await opts.onSaved?.(s); + } catch (e) { + pushToast(errMessage(e, opts.failMessage), 'error'); + } finally { + saving = false; + } + } + + onMount(() => { + void load(); + }); + + return { + get saved() { return saved; }, + get form() { return form; }, + set form(v) { form = v; }, + get loadError() { return loadError; }, + get saving() { return saving; }, + get dirty() { return dirty; }, + load, + save + }; +}