Redesign settings screen

This commit is contained in:
iamdoubz committed 2026-11-05 17:41:37 -06:00
1 parent a336379354
commit a6ffe5e5aa
1 file changed
+197 -122
+197 -122
View File
@@ -1,6 +1,22 @@
<script lang="ts">
import CalendarDays from '@lucide/svelte/icons/calendar-days';
import Check from '@lucide/svelte/icons/check';
import ChevronRight from '@lucide/svelte/icons/chevron-right';
import Globe from '@lucide/svelte/icons/globe';
import Monitor from '@lucide/svelte/icons/monitor';
import Moon from '@lucide/svelte/icons/moon';
import Palette from '@lucide/svelte/icons/palette';
import Plus from '@lucide/svelte/icons/plus';
import ScrollText from '@lucide/svelte/icons/scroll-text';
import Sun from '@lucide/svelte/icons/sun';
import Trash2 from '@lucide/svelte/icons/trash-2';
import Users from '@lucide/svelte/icons/users';
import type { Component } from 'svelte';
import { api, type Calendar, type Household, type Source } from '../lib/api';
import { app, getTheme, setHousehold, setTheme, toast, type Theme } from '../lib/state.svelte';
import { LAYOUT_ICONS } from '../lib/icons';
import PageHeader from '../lib/ui/PageHeader.svelte';
import Segmented from '../lib/ui/Segmented.svelte';
const h = $derived(app.household!);
let theme = $state<Theme>(getTheme());
@@ -12,14 +28,15 @@
let confirmDelete = $state<string | null>(null);
// Forms hold their own copy; arrays are copied so Cancel leaves the household untouched.
let child = $state({ display_name: '', color: '#3b82f6' });
let calendar = $state({ name: '', color: '#3b82f6', child_ids: [] as number[], source_ids: [] as number[], alarm_minutes: null as number | null });
let child = $state({ display_name: '', color: '#4f46e5' });
let calendar = $state({ name: '', color: '#4f46e5', child_ids: [] as number[], source_ids: [] as number[], alarm_minutes: null as number | null });
let source = $state({
name: '', layout_type: 'month_grid' as Source['layout_type'], double_read: false, ignore_text: '',
applies_filter: '', prompt_notes: '', default_calendar_id: null as number | null, child_ids: [] as number[],
});
const palette = ['#3b82f6', '#e11d48', '#16a34a', '#d97706', '#7c3aed', '#0891b2'];
// Child colors are data the user picks; these defaults are distinct in both themes.
const palette = ['#4f46e5', '#e11d48', '#059669', '#d97706', '#7c3aed', '#0891b2'];
const alarms: [number | null, string][] = [[null, 'No alert'], [15, '15 minutes before'], [60, '1 hour before'], [720, '12 hours before'], [1440, 'The day before']];
const layouts: Record<Source['layout_type'], string> = { month_grid: 'Month calendar grid', menu: 'Lunch menu', list: 'Flyer or list' };
@@ -31,8 +48,9 @@
child = { display_name: c?.display_name ?? '', color: c?.color ?? palette[h.children.length % palette.length] };
} else if (kind === 'calendar') {
const c: Partial<Calendar> = h.calendars.find((x) => x.id === id) ?? {};
calendar = { name: c.name ?? '', color: c.color ?? '#3b82f6', child_ids: [...(c.child_ids ?? h.children.map((k) => k.id))],
source_ids: [...(c.source_ids ?? [])], alarm_minutes: c.alarm_minutes ?? null };
calendar = { name: c.name ?? '', color: c.color ?? palette[h.calendars.length % palette.length],
child_ids: [...(c.child_ids ?? h.children.map((k) => k.id))], source_ids: [...(c.source_ids ?? [])],
alarm_minutes: c.alarm_minutes ?? null };
} else {
const s: Partial<Source> = h.sources.find((x) => x.id === id) ?? {};
source = { name: s.name ?? '', layout_type: s.layout_type ?? 'month_grid', double_read: s.double_read ?? false,
@@ -75,7 +93,6 @@
if (confirmDelete !== key) confirmDelete = key;
else send(path, 'DELETE', undefined, 'Deleted');
}
function toggle(list: number[], id: number, on: boolean) {
const at = list.indexOf(id);
if (on && at < 0) list.push(id);
@@ -83,112 +100,78 @@
}
const names = (ids: number[]) => h.children.filter((c) => ids.includes(c.id)).map((c) => c.display_name).join(', ') || 'no one';
const isEditing = (kind: string, id: number | null) => editing?.kind === kind && editing.id === id;
</script>
{#snippet heading(Icon: Component<any>, title: string, note: string)}
<div class="sec-head">
<span class="sec-icon"><Icon size={20} aria-hidden="true" /></span>
<div>
<h2>{title}</h2>
{#if note}<p class="hint">{note}</p>{/if}
</div>
</div>
{/snippet}
{#snippet actions(path: string, key: string)}
<div class="row spread">
<div class="form-footer">
<div class="row">
<button class="primary" type="submit">Save</button>
<button type="button" onclick={() => (editing = null)}>Cancel</button>
<button class="primary" type="submit"><Check size={18} aria-hidden="true" />Save</button>
<button type="button" class="ghost" onclick={() => (editing = null)}>Cancel</button>
</div>
{#if editing?.id}
<button type="button" class={confirmDelete === key ? 'danger' : 'quiet'} onclick={() => remove(path, key)}>
{confirmDelete === key ? 'Really delete?' : 'Delete…'}
<button type="button" class={confirmDelete === key ? 'danger solid' : 'danger'} onclick={() => remove(path, key)}>
<Trash2 size={18} aria-hidden="true" />{confirmDelete === key ? 'Really delete?' : 'Delete…'}
</button>
{/if}
</div>
{/snippet}
{#snippet childBoxes(group: number[])}
<fieldset>
<legend class="muted small">Children</legend>
{#snippet childChips(group: number[])}
<fieldset class="field">
<legend class="label">Children</legend>
<div class="row">
{#each h.children as c (c.id)}
<label class="check"><input type="checkbox" checked={group.includes(c.id)} onchange={(e) => toggle(group, c.id, e.currentTarget.checked)} /><span class="dot" style:background={c.color}></span>{c.display_name}</label>
<label class="chip-check">
<input type="checkbox" checked={group.includes(c.id)} onchange={(e) => toggle(group, c.id, e.currentTarget.checked)} />
<span class="dot" style:background={c.color}></span>{c.display_name}
</label>
{/each}
</div>
</fieldset>
{/snippet}
<h1>Settings</h1>
<section class="card stack">
<h2 style="margin-top:0">Appearance</h2>
<div class="row" role="radiogroup" aria-label="Theme">
{#each [['system', 'Match this device'], ['light', 'Light'], ['dark', 'Dark']] as [value, label]}
<label class="check"><input type="radio" bind:group={theme} {value} /> {label}</label>
{/each}
{#snippet nameAndColor(value: { name?: string; display_name?: string; color: string }, key: 'name' | 'display_name', label: string, placeholder: string)}
<div class="field">
<label class="label" for="{key}-input">{label}</label>
<div class="name-color">
<input id="{key}-input" bind:value={value[key]} {placeholder} required maxlength="80" />
<input type="color" bind:value={value.color} aria-label="Color" />
</div>
</div>
</section>
{/snippet}
<section class="card stack">
<h2 style="margin-top:0">Time zone</h2>
<p>Events are exported in <strong>{h.timezone}</strong>.</p>
{#if deviceZone && deviceZone !== h.timezone}
<button onclick={() => send('/user', 'PUT', { timezone: deviceZone })}>Use this device's ({deviceZone})</button>
{/if}
</section>
<h2>Children</h2>
<ul class="list">
{#each h.children as c (c.id)}
<li class="card">
{#if editing?.kind === 'child' && editing.id === c.id}
<form onsubmit={saveChild} class="stack">
<div class="row"><input bind:value={child.display_name} aria-label="Name or nickname" required maxlength="80" class="grow" style="width:auto" /><input type="color" bind:value={child.color} aria-label="Color" /></div>
{@render actions(`/children/${c.id}`, `child-${c.id}`)}
</form>
{:else}
<button class="line" onclick={() => edit('child', c.id)}><span class="dot" style:background={c.color}></span>{c.display_name}</button>
{/if}
</li>
{/each}
</ul>
{#if editing?.kind === 'child' && editing.id === null}
<form onsubmit={saveChild} class="card stack">
<div class="row"><input bind:value={child.display_name} placeholder="Nickname or initials" aria-label="Name or nickname" required maxlength="80" class="grow" style="width:auto" /><input type="color" bind:value={child.color} aria-label="Color" /></div>
{@render actions('', '')}
{#snippet childForm()}
<form onsubmit={saveChild} class="edit-form">
{@render nameAndColor(child, 'display_name', 'Nickname or initials', 'Sam')}
{@render actions(`/children/${editing?.id}`, `child-${editing?.id}`)}
</form>
{:else}
<button onclick={() => edit('child', null)}>Add a child</button>
{/if}
<h2>Calendars</h2>
<p class="muted small">A calendar is what you download or subscribe to: one per child, or one for the whole family.</p>
<ul class="list">
{#each h.calendars as c (c.id)}
<li class="card">
{#if editing?.kind === 'calendar' && editing.id === c.id}
{@render calendarForm()}
{:else}
<button class="line" onclick={() => edit('calendar', c.id)}>
<span class="dot" style:background={c.color}></span>
<span><strong>{c.name}</strong><br /><span class="muted small">For {names(c.child_ids)}{c.source_ids.length ? `, ${c.source_ids.length} sources` : ', all sources'}</span></span>
</button>
{/if}
</li>
{/each}
</ul>
{#if editing?.kind === 'calendar' && editing.id === null}
<div class="card">{@render calendarForm()}</div>
{:else}
<button onclick={() => edit('calendar', null)}>Add a calendar</button>
{/if}
{/snippet}
{#snippet calendarForm()}
<form onsubmit={saveCalendar} class="stack">
<div class="row"><input bind:value={calendar.name} placeholder="Calendar name" aria-label="Calendar name" required maxlength="80" class="grow" style="width:auto" /><input type="color" bind:value={calendar.color} aria-label="Color" /></div>
{@render childBoxes(calendar.child_ids)}
<fieldset>
<legend class="muted small">Sources (none checked: all of them)</legend>
<form onsubmit={saveCalendar} class="edit-form">
{@render nameAndColor(calendar, 'name', 'Calendar name', 'Sam — school')}
{@render childChips(calendar.child_ids)}
<fieldset class="field">
<legend class="label">Sources <span class="hint">(none checked means all of them)</span></legend>
<div class="row">
{#each h.sources as s (s.id)}
<label class="check"><input type="checkbox" bind:group={calendar.source_ids} value={s.id} />{s.name}</label>
<label class="chip-check"><input type="checkbox" bind:group={calendar.source_ids} value={s.id} />{s.name}</label>
{/each}
</div>
</fieldset>
<div>
<label for="alarm">Alert</label>
<div class="field">
<label class="label" for="alarm">Alert</label>
<select id="alarm" bind:value={calendar.alarm_minutes}>
{#each alarms as [value, label]}<option {value}>{label}</option>{/each}
</select>
@@ -197,61 +180,153 @@
</form>
{/snippet}
<h2>Sources</h2>
<p class="muted small">Each paper you scan: the district calendar, a class newsletter, a lunch menu, a team schedule.</p>
<ul class="list">
{#each h.sources as s (s.id)}
<li class="card">
{#if editing?.kind === 'source' && editing.id === s.id}
{@render sourceForm()}
{:else}
<button class="line" onclick={() => edit('source', s.id)}>
<span><strong>{s.name}</strong><br /><span class="muted small">{layouts[s.layout_type]} · for {names(s.child_ids)}{s.double_read ? ' · double read' : ''}</span></span>
</button>
{/if}
</li>
{/each}
</ul>
{#if editing?.kind === 'source' && editing.id === null}
<div class="card">{@render sourceForm()}</div>
{:else}
<button onclick={() => edit('source', null)}>Add a source</button>
{/if}
{#snippet sourceForm()}
<form onsubmit={saveSource} class="stack">
<div><label for="src-name">Name</label><input id="src-name" bind:value={source.name} placeholder="Lincoln Elem — District" required maxlength="80" /></div>
<div>
<label for="src-layout">Layout</label>
<form onsubmit={saveSource} class="edit-form">
<div class="field"><label class="label" for="src-name">Name</label><input id="src-name" bind:value={source.name} placeholder="Lincoln Elementary — district" required maxlength="80" /></div>
<div class="field">
<label class="label" for="src-layout">Layout</label>
<select id="src-layout" bind:value={source.layout_type}>
{#each Object.entries(layouts) as [value, label]}<option {value}>{label}</option>{/each}
</select>
</div>
{@render childBoxes(source.child_ids)}
<label class="check"><input type="checkbox" bind:checked={source.double_read} /> Double read (slower, flags more mistakes)</label>
<div>
<label for="src-filter">Only for (optional)</label>
{@render childChips(source.child_ids)}
<label class="check"><input type="checkbox" bind:checked={source.double_read} /> Double read <span class="hint">— slower, but flags more mistakes</span></label>
<div class="field">
<label class="label" for="src-filter">Only for <span class="hint">(optional)</span></label>
<input id="src-filter" bind:value={source.applies_filter} placeholder="Grade 3, Team Blue, K-8" maxlength="200" />
<p class="muted small">Items for other grades or teams start unchecked in review.</p>
<p class="hint">Items for other grades or teams start unchecked in review.</p>
</div>
<div>
<label for="src-ignore">Labels to ignore (comma-separated)</label>
<div class="field">
<label class="label" for="src-ignore">Labels to ignore <span class="hint">(comma-separated)</span></label>
<input id="src-ignore" bind:value={source.ignore_text} placeholder="BHS, Milk" />
</div>
<div>
<label for="src-cal">Usual calendar</label>
<div class="field">
<label class="label" for="src-cal">Usual calendar</label>
<select id="src-cal" bind:value={source.default_calendar_id}>
<option value={null}>None</option>
{#each h.calendars as c (c.id)}<option value={c.id}>{c.name}</option>{/each}
</select>
</div>
<div><label for="src-notes">Notes for reading this source (optional)</label><textarea id="src-notes" bind:value={source.prompt_notes} maxlength="2000" placeholder="'A Day' and 'B Day' are rotation labels, not events."></textarea></div>
<div class="field">
<label class="label" for="src-notes">Notes for reading this source <span class="hint">(optional)</span></label>
<textarea id="src-notes" bind:value={source.prompt_notes} maxlength="2000" placeholder="'A Day' and 'B Day' are rotation labels, not events."></textarea>
</div>
{@render actions(`/sources/${editing?.id}`, `source-${editing?.id}`)}
</form>
{/snippet}
<PageHeader title="Settings" />
<div class="stack-lg">
<section class="card section-card">
{@render heading(Palette, 'Appearance', 'Saved on this device.')}
<Segmented label="Theme" bind:value={theme} options={[
{ value: 'system', label: 'Device', icon: Monitor },
{ value: 'light', label: 'Light', icon: Sun },
{ value: 'dark', label: 'Dark', icon: Moon },
]} />
</section>
<section class="card section-card">
{@render heading(Globe, 'Time zone', 'Times in exported calendars use this zone.')}
<div class="row spread">
<span class="strong">{h.timezone}</span>
{#if deviceZone && deviceZone !== h.timezone}
<button class="sm" onclick={() => send('/user', 'PUT', { timezone: deviceZone })}>Use this device's ({deviceZone})</button>
{/if}
</div>
</section>
<section class="card section-card">
{@render heading(Users, 'Children', 'A nickname or initials is enough.')}
<ul class="items">
{#each h.children as c (c.id)}
<li>
{#if isEditing('child', c.id)}
{@render childForm()}
{:else}
<button class="item" onclick={() => edit('child', c.id)}>
<span class="swatch" style:background={c.color}></span>
<span class="grow item-title">{c.display_name}</span>
<ChevronRight size={18} aria-hidden="true" />
</button>
{/if}
</li>
{/each}
</ul>
{#if isEditing('child', null)}{@render childForm()}{:else}<button class="add" onclick={() => edit('child', null)}><Plus size={18} aria-hidden="true" />Add a child</button>{/if}
</section>
<section class="card section-card">
{@render heading(CalendarDays, 'Calendars', 'What you download into Apple or Google Calendar: one per child, or one for the family.')}
<ul class="items">
{#each h.calendars as c (c.id)}
<li>
{#if isEditing('calendar', c.id)}
{@render calendarForm()}
{:else}
<button class="item" onclick={() => edit('calendar', c.id)}>
<span class="swatch" style:background={c.color}></span>
<span class="grow">
<span class="item-title">{c.name}</span>
<span class="item-sub">For {names(c.child_ids)} · {c.source_ids.length ? `${c.source_ids.length} sources` : 'all sources'}</span>
</span>
<ChevronRight size={18} aria-hidden="true" />
</button>
{/if}
</li>
{/each}
</ul>
{#if isEditing('calendar', null)}{@render calendarForm()}{:else}<button class="add" onclick={() => edit('calendar', null)}><Plus size={18} aria-hidden="true" />Add a calendar</button>{/if}
</section>
<section class="card section-card">
{@render heading(ScrollText, 'Sources', 'Each paper you scan: the district calendar, a newsletter, a lunch menu, a team schedule.')}
<ul class="items">
{#each h.sources as s (s.id)}
{@const Icon = LAYOUT_ICONS[s.layout_type]}
<li>
{#if isEditing('source', s.id)}
{@render sourceForm()}
{:else}
<button class="item" onclick={() => edit('source', s.id)}>
<span class="src-icon"><Icon size={18} aria-hidden="true" /></span>
<span class="grow">
<span class="item-title">{s.name}</span>
<span class="item-sub">{layouts[s.layout_type]} · for {names(s.child_ids)}{s.double_read ? ' · double read' : ''}</span>
</span>
<ChevronRight size={18} aria-hidden="true" />
</button>
{/if}
</li>
{/each}
</ul>
{#if isEditing('source', null)}{@render sourceForm()}{:else}<button class="add" onclick={() => edit('source', null)}><Plus size={18} aria-hidden="true" />Add a source</button>{/if}
</section>
</div>
<style>
.list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 8px; }
.line { width: 100%; justify-content: flex-start; border: 0; background: none; padding: 0; text-align: left; gap: 10px; }
fieldset { border: 0; padding: 0; margin: 0; }
.section-card { display: flex; flex-direction: column; gap: var(--s4); }
.sec-head { display: flex; gap: var(--s3); align-items: flex-start; }
.sec-icon { display: grid; place-items: center; width: 40px; height: 40px; flex: none; border-radius: var(--r-md); background: var(--primary-soft); color: var(--primary-text); }
.sec-head h2 { font-size: var(--fs-md); }
.items { display: flex; flex-direction: column; border-top: 1px solid var(--border); }
.items:empty { display: none; }
.items > li { border-bottom: 1px solid var(--border); }
.item {
width: 100%; min-height: 60px; justify-content: flex-start; gap: var(--s3); padding: var(--s2) var(--s1);
border: 0; border-radius: 0; background: none; text-align: left; white-space: normal; font-weight: 400; color: var(--text-3);
}
.item:hover:not(:disabled) { background: var(--surface-2); }
.item:active:not(:disabled) { transform: none; }
.item-title { display: block; font-weight: 650; color: var(--text); }
.item-sub { display: block; font-size: var(--fs-sm); color: var(--text-2); }
.swatch { width: 28px; height: 28px; border-radius: var(--r-sm); flex: none; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1); }
.src-icon { display: grid; place-items: center; width: 36px; height: 36px; flex: none; border-radius: var(--r-md); background: var(--surface-2); color: var(--text-2); }
.add { align-self: flex-start; }
.edit-form { display: flex; flex-direction: column; gap: var(--s4); padding: var(--s4) 0; }
.name-color { display: flex; gap: var(--s2); }
.form-footer { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s3); }
</style>