ui: Day 3 — global toast system + first error-toast wiring on DictationPage
THE PROBLEM (Codex review + architecture-review.md §1, §14):
Multiple critical paths discard errors silently — `let _ = insert_transcript(...)`,
`.catch(() => {})`, inline `error = ...` state that not every page surfaces. ND
users in particular need explicit feedback when something fails; silent
failure is worse than no feature.
SHIPPED:
src/lib/stores/toasts.svelte.js (new):
- Minimal in-house toast store (~80 lines, no svelte-french-toast dep)
- Severity → palette: info/success → moss, warn → signal, error → ember
- Auto-dismiss durations per severity (success 3s, info 4s, warn 6s,
error sticky)
- `invokeWithToast(invoke, command, args, errorTitle?)` helper wraps a
Tauri invoke and toasts on failure while still throwing for callers
that need to handle the error themselves
src/lib/components/ToastViewport.svelte (new):
- Reads from the toasts store
- Bottom-right stack, max-width clamp on small screens
- aria-live=polite + role=alert on error toasts (screen-reader friendly)
- Slide-in animation honours `html.reduce-motion` for the existing
preferences toggle
- Severity left-border in brand colours via existing CSS variables
src/routes/+layout.svelte:
- Mounts <ToastViewport /> once at the app root so toasts work from any
page or window
src/lib/pages/DictationPage.svelte:
- First error-toast wiring: failures starting recording now show a
sticky toast in addition to the inline `error` panel
- Replaces a previously easy-to-miss failure-mode
NEXT (Day 4 batch):
- Wire toasts into FilesPage, HistoryPage, SettingsPage, ModelDownloader
(all currently swallow errors)
- Add `add_transcript`, `list_transcripts`, `update_transcript` (closes the
long-standing TODO Codex flagged), `delete_transcript` Tauri commands
wrapping the existing storage layer
- Switch addToHistory to dual-write (localStorage + SQLite) so the
canonical store catches up with the actual transcript flow
- FTS5 transcripts_fts virtual table + search command
- Wrap multi-row writes (decompose_and_store) in DB transactions
This commit is contained in:
83
src/lib/stores/toasts.svelte.js
Normal file
83
src/lib/stores/toasts.svelte.js
Normal file
@@ -0,0 +1,83 @@
|
||||
// Minimal toast store. Roll-our-own (no svelte-french-toast etc) so Kon
|
||||
// stays lean. Toasts auto-dismiss after `duration` ms unless duration is 0
|
||||
// (sticky) or unless the user clicks the close button.
|
||||
//
|
||||
// Severity → colour:
|
||||
// info → moss (informational, default 4s)
|
||||
// success → moss (operation succeeded, default 3s)
|
||||
// warn → signal (degraded but not failed, default 6s)
|
||||
// error → ember (operation failed, sticky until dismissed)
|
||||
//
|
||||
// Usage from a component:
|
||||
// import { toasts } from '$lib/stores/toasts.svelte.js';
|
||||
// toasts.error('Could not start recording', 'Selected microphone disappeared.');
|
||||
// toasts.success('Saved');
|
||||
|
||||
let nextId = 1;
|
||||
|
||||
function defaultDuration(severity) {
|
||||
switch (severity) {
|
||||
case 'success': return 3000;
|
||||
case 'info': return 4000;
|
||||
case 'warn': return 6000;
|
||||
case 'error': return 0; // sticky
|
||||
default: return 4000;
|
||||
}
|
||||
}
|
||||
|
||||
function createToastsStore() {
|
||||
// $state requires a class field or top-level let in Svelte 5; we expose
|
||||
// `items` via a getter on the singleton.
|
||||
let items = $state([]);
|
||||
|
||||
function show(severity, title, body) {
|
||||
const id = nextId++;
|
||||
const duration = defaultDuration(severity);
|
||||
const toast = { id, severity, title, body: body ?? '', duration };
|
||||
items.push(toast);
|
||||
if (duration > 0) {
|
||||
setTimeout(() => dismiss(id), duration);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
function dismiss(id) {
|
||||
const ix = items.findIndex(t => t.id === id);
|
||||
if (ix >= 0) items.splice(ix, 1);
|
||||
}
|
||||
|
||||
function dismissAll() {
|
||||
items.length = 0;
|
||||
}
|
||||
|
||||
return {
|
||||
get items() { return items; },
|
||||
info: (title, body) => show('info', title, body),
|
||||
success: (title, body) => show('success', title, body),
|
||||
warn: (title, body) => show('warn', title, body),
|
||||
error: (title, body) => show('error', title, body),
|
||||
dismiss,
|
||||
dismissAll,
|
||||
};
|
||||
}
|
||||
|
||||
export const toasts = createToastsStore();
|
||||
|
||||
// Helper: wrap a Tauri invoke and toast on failure. Returns the result on
|
||||
// success, throws on failure (so callers that need to handle the error
|
||||
// can still try/catch).
|
||||
//
|
||||
// import { invoke } from '@tauri-apps/api/core';
|
||||
// import { invokeWithToast } from '$lib/stores/toasts.svelte.js';
|
||||
// const result = await invokeWithToast('start_native_capture', { deviceName });
|
||||
//
|
||||
// Optional `errorTitle` overrides the default ("Action failed").
|
||||
export async function invokeWithToast(invokeFn, command, args, errorTitle) {
|
||||
try {
|
||||
return await invokeFn(command, args);
|
||||
} catch (err) {
|
||||
const msg = typeof err === 'string' ? err : (err?.message ?? String(err));
|
||||
toasts.error(errorTitle ?? 'Action failed', msg);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user