Add settings screen

This commit is contained in:
iamdoubz committed 2026-11-05 16:08:42 -06:00
1 parent 03207f7bfe
commit c2016e1c7a
1 file changed
+257
+257
View File
@@ -0,0 +1,257 @@
<script lang="ts">
import { api, type Calendar, type Household, type Source } from '../lib/api';
import { app, getTheme, setHousehold, setTheme, toast, type Theme } from '../lib/state.svelte';
const h = $derived(app.household!);
let theme = $state<Theme>(getTheme());
$effect(() => setTheme(theme));
const deviceZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
type Editing = { kind: 'child' | 'calendar' | 'source'; id: number | null } | null;
let editing = $state<Editing>(null);
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 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'];
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' };
function edit(kind: 'child' | 'calendar' | 'source', id: number | null) {
confirmDelete = null;
editing = { kind, id };
if (kind === 'child') {
const c = h.children.find((x) => x.id === id);
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 };
} 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,
ignore_text: (s.ignore_labels ?? []).join(', '), applies_filter: s.applies_filter ?? '',
prompt_notes: s.prompt_notes ?? '', default_calendar_id: s.default_calendar_id ?? null,
child_ids: [...(s.child_ids ?? [])] };
}
}
async function send(path: string, method: string, json?: unknown, done = 'Saved') {
try {
setHousehold(await api<Household>(path, { method, json }));
editing = null;
confirmDelete = null;
toast(done);
} catch (e: any) {
toast(e.message);
}
}
function saveChild(e: SubmitEvent) {
e.preventDefault();
const id = editing?.id;
send(id ? `/children/${id}` : '/children', id ? 'PUT' : 'POST', child);
}
function saveCalendar(e: SubmitEvent) {
e.preventDefault();
const id = editing?.id;
send(id ? `/calendars/${id}` : '/calendars', id ? 'PUT' : 'POST', calendar);
}
function saveSource(e: SubmitEvent) {
e.preventDefault();
const id = editing?.id;
const { ignore_text, ...rest } = source;
const body = { ...rest, ignore_labels: ignore_text.split(',').map((s) => s.trim()).filter(Boolean),
applies_filter: rest.applies_filter.trim() || null, prompt_notes: rest.prompt_notes.trim() || null };
send(id ? `/sources/${id}` : '/sources', id ? 'PUT' : 'POST', body);
}
function remove(path: string, key: string) {
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);
if (!on && at >= 0) list.splice(at, 1);
}
const names = (ids: number[]) => h.children.filter((c) => ids.includes(c.id)).map((c) => c.display_name).join(', ') || 'no one';
</script>
{#snippet actions(path: string, key: string)}
<div class="row spread">
<div class="row">
<button class="primary" type="submit">Save</button>
<button type="button" 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>
{/if}
</div>
{/snippet}
{#snippet childBoxes(group: number[])}
<fieldset>
<legend class="muted small">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>
{/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}
</div>
</section>
<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('', '')}
</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 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>
<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>
{/each}
</div>
</fieldset>
<div>
<label for="alarm">Alert</label>
<select id="alarm" bind:value={calendar.alarm_minutes}>
{#each alarms as [value, label]}<option {value}>{label}</option>{/each}
</select>
</div>
{@render actions(`/calendars/${editing?.id}`, `calendar-${editing?.id}`)}
</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>
<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>
<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>
</div>
<div>
<label for="src-ignore">Labels to ignore (comma-separated)</label>
<input id="src-ignore" bind:value={source.ignore_text} placeholder="BHS, Milk" />
</div>
<div>
<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>
{@render actions(`/sources/${editing?.id}`, `source-${editing?.id}`)}
</form>
{/snippet}
<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; }
</style>