svelte refactor

This commit is contained in:
Bradley Nelson
2026-06-17 22:30:56 -06:00
parent 89e14f8f9e
commit e6279da5ce
26 changed files with 497 additions and 386 deletions
+15
View File
@@ -0,0 +1,15 @@
/** Error-handling helpers shared across pages and components. */
import { ui } from '$lib/stores/ui.svelte';
/** Normalise an unknown thrown value into a human-readable message. */
export function errorMessage(e: unknown): string {
return e instanceof Error ? e.message : String(e);
}
/**
* Raise an error toast for a caught value — the canonical catch-block handler.
* Replaces the repeated `ui.notify(e instanceof Error ? e.message : String(e), 'error')`.
*/
export function errorToast(e: unknown): void {
ui.notify(errorMessage(e), 'error');
}
+43
View File
@@ -0,0 +1,43 @@
/** Time-formatting helpers shared across views. */
/** Options for {@link relativeTimeAgo}. */
export interface RelativeTimeOptions {
/** Label returned for a null/empty value (default `''`). */
empty?: string;
/** When the value can't be parsed, return it stringified instead of `empty`. */
invalidAsString?: boolean;
}
/**
* Locale-aware relative "time ago" via `Intl.RelativeTimeFormat`.
*
* Accepts an epoch (seconds or milliseconds) or an ISO-8601 string, so the
* grid-card metadata lines (epoch) and the profile/app-password tables (ISO)
* share one implementation.
*/
export function relativeTimeAgo(
value: number | string | null | undefined,
opts: RelativeTimeOptions = {}
): string {
const empty = opts.empty ?? '';
if (value === null || value === undefined || value === '') return empty;
const date =
typeof value === 'number' ? new Date(value < 1e12 ? value * 1000 : value) : new Date(value);
if (Number.isNaN(date.getTime())) return opts.invalidAsString ? String(value) : empty;
const diffSec = Math.round((date.getTime() - Date.now()) / 1000);
const abs = Math.abs(diffSec);
const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' });
const units: Array<[Intl.RelativeTimeFormatUnit, number]> = [
['year', 31536000],
['month', 2592000],
['week', 604800],
['day', 86400],
['hour', 3600],
['minute', 60]
];
for (const [unit, secs] of units) {
if (abs >= secs) return rtf.format(Math.round(diffSec / secs), unit);
}
return rtf.format(diffSec, 'second');
}