Files
minstrel/web/src/lib/components/NotificationSettings.svelte
T
bvandeusenandClaude Opus 5.5 be384c8efe
release / go (push) Failing after 1m10s
release / web (push) Failing after 10s
release / android (push) Failing after 1m2s
release / govulncheck (push) Successful in 17s
release / integration (push) Successful in 2m31s
release / Build signed APK (push) Failing after 1m43s
release / Build + push container image (push) Skipped
web: svelte-query 6 — queries are reactive objects read directly, not stores (#8532)
@tanstack/svelte-query 6 is built on runes:
- createQuery and createInfiniteQuery take their options as a function
  and return a reactive object that components read as query.data.
- createMutation takes its options the same way.

What this commit changes:
- Every query creator in src/lib/api now takes its arguments as getters
  and reads them inside the options, so a query follows the values it
  depends on. Before, each component rebuilt it in a $derived.
- Constant arguments are passed as getters too, so the rule has no
  exceptions.
- Components call a creator once at init: const query =
  createArtistQuery(() => id). The $derived(create…) and $store pairs
  are gone.
- A query that loads only on demand (a chosen genre or year, the
  Handled view) has an enabled getter. The page keeps its `cond ? q :
  null` alias, because a switched-off query reports isPending and the
  page expects null when nothing is chosen.
- Tests: the query mocks return plain objects. liveQuery
  (test-utils/liveQuery.svelte.ts) gives a test a result it can change
  mid-test. Argument checks call the getter the page passed.

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

142 lines
5.1 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { useQueryClient } from '@tanstack/svelte-query';
import {
NOTIFICATION_KIND_LABELS,
createNotificationSettingsQuery,
putNotificationSettings,
type NotificationChannel,
type NotificationKindSetting,
type NotificationSettingChange,
type NotificationSettings
} from '#lib/api/notifications.js';
import { qk } from '#lib/api/queries.js';
import { user } from '#lib/auth/store.svelte.js';
import { pushToast } from '#lib/stores/toast.svelte.js';
// Per-user notification settings: one row per kind, a toggle per
// channel. Toggles are optimistic; a failed save puts the switch back.
const client = useQueryClient();
const settingsQ = createNotificationSettingsQuery();
const settings = $derived(settingsQ.data as NotificationSettings | undefined);
const forEveryone = $derived((settings?.kinds ?? []).filter((k) => !k.admin_only));
const forAdmins = $derived((settings?.kinds ?? []).filter((k) => k.admin_only));
const channels: { key: NotificationChannel; label: string }[] = [
{ key: 'inbox', label: 'Inbox' },
{ key: 'phone', label: 'Phone' },
{ key: 'email', label: 'Email' }
];
// Bumped by every local change, so a save resolving after a newer toggle
// does not overwrite it, and a failed save only reverts when nothing newer
// happened.
let generation = 0;
function commit(next: NotificationSettings) {
client.setQueryData(qk.notificationSettings(), next);
}
function withChannel(
k: NotificationKindSetting,
channel: NotificationChannel,
value: boolean
): NotificationKindSetting {
const next = { ...k };
next[channel] = value;
return next;
}
async function toggle(row: NotificationKindSetting, channel: NotificationChannel) {
if (!settings) return;
const prev = settings;
const value = !row[channel];
const gen = ++generation;
commit({
...prev,
kinds: prev.kinds.map((k) => (k.kind === row.kind ? withChannel(k, channel, value) : k))
});
const change: NotificationSettingChange = { kind: row.kind };
change[channel] = value;
try {
const saved = await putNotificationSettings([change]);
if (gen === generation) commit(saved);
} catch {
if (gen === generation) commit(prev);
pushToast("Couldn't save that setting. Try again in a moment.", 'error');
}
}
// Phone and email ride on the inbox: with the inbox off, nothing is stored
// to send. Their own choices are kept and come back when the inbox does.
function disabled(row: NotificationKindSetting, channel: NotificationChannel): boolean {
if (channel === 'inbox') return false;
if (!row.inbox) return true;
return channel === 'email' && !settings?.email_available;
}
function label(kind: string): string {
return NOTIFICATION_KIND_LABELS[kind] ?? kind;
}
</script>
{#snippet row(r: NotificationKindSetting)}
<span class="py-1.5 text-text-primary">{label(r.kind)}</span>
{#each channels as c (c.key)}
<span class="flex justify-center">
<input
type="checkbox"
class="h-4 w-4 cursor-pointer disabled:cursor-not-allowed disabled:opacity-40"
aria-label="{label(r.kind)}: {c.label}"
checked={r[c.key] && !disabled(r, c.key)}
disabled={disabled(r, c.key)}
onchange={() => toggle(r, c.key)}
/>
</span>
{/each}
{/snippet}
<!-- Settings › Notifications: one table, its column labels once
, the admins' library-health kinds under their own heading in
the same columns. -->
<section id="notifications" class="rounded-lg border border-border bg-surface">
<header class="px-4 pt-3 pb-2">
<h2 class="text-base font-medium text-text-primary">Notifications</h2>
<p class="text-xs text-text-secondary">What reaches you, and where. Phone and email follow the inbox.</p>
</header>
<div class="border-t border-border px-4 py-3">
{#if !settings}
<p class="text-sm text-text-secondary">Loading…</p>
{:else}
<div class="grid grid-cols-[1fr_repeat(3,3.5rem)] items-center gap-y-1 text-sm">
<span></span>
{#each channels as c (c.key)}
<span class="text-center text-xs text-text-secondary">{c.label}</span>
{/each}
{#each forEveryone as r (r.kind)}
{@render row(r)}
{/each}
{#if forAdmins.length > 0}
<h3 class="col-span-4 pt-3 text-xs font-medium uppercase tracking-wide text-text-secondary">Library health</h3>
{#each forAdmins as r (r.kind)}
{@render row(r)}
{/each}
{/if}
</div>
{#if !settings.email_available}
<p class="mt-3 text-xs text-text-secondary" data-testid="email-unavailable">
{#if settings.email_unavailable_reason === 'no_address'}
Email is off: <a href="/settings/account#profile" class="underline hover:text-text-primary">add an email address in your profile</a>.
{:else if user.value?.is_admin}
Email is off: <a href="/admin/email" class="underline hover:text-text-primary">SMTP isn't set up</a>.
{:else}
Email is off: this server doesn't send email.
{/if}
</p>
{/if}
{/if}
</div>
</section>