mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Add notification toasts and surface persistence failures instead of failing silently
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
.region {
|
||||
position: fixed;
|
||||
top: var(--space-5);
|
||||
right: var(--space-5);
|
||||
/* Above the confirm backdrop (z 1000) so a failure stays visible and
|
||||
dismissible even with a confirmation open; top-right won't block the
|
||||
centered dialog. */
|
||||
z-index: 1100;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-3);
|
||||
width: min(24rem, calc(100vw - 2 * var(--space-5)));
|
||||
/* The region is only a positioner; individual toasts re-enable pointer events. */
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.toast {
|
||||
pointer-events: auto;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--space-3);
|
||||
padding: var(--space-4);
|
||||
background: var(--layer-02);
|
||||
/* Design language → Toasts: 1px border in the support colour, square. The
|
||||
accent (per-kind) is set by the modifier classes below. */
|
||||
border: var(--border-width) solid var(--toast-accent);
|
||||
border-left-width: 3px;
|
||||
border-radius: var(--radius);
|
||||
box-shadow: 0 2px 12px rgb(0 0 0 / 0.3);
|
||||
animation: toast-in var(--dur-moderate) var(--ease);
|
||||
}
|
||||
|
||||
.error {
|
||||
--toast-accent: var(--support-error);
|
||||
}
|
||||
.warning {
|
||||
--toast-accent: var(--support-warning);
|
||||
}
|
||||
.success {
|
||||
--toast-accent: var(--support-success);
|
||||
}
|
||||
.info {
|
||||
--toast-accent: var(--support-info);
|
||||
}
|
||||
|
||||
@keyframes toast-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateX(8px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
|
||||
.body {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
.title {
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.message {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
line-height: 1.4;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.detail {
|
||||
margin-top: var(--space-1);
|
||||
}
|
||||
|
||||
.detailSummary {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.detailText {
|
||||
margin: var(--space-2) 0 0;
|
||||
padding: var(--space-3);
|
||||
font-family: 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, monospace;
|
||||
font-size: 12px;
|
||||
line-height: 1.4;
|
||||
color: var(--text);
|
||||
background: var(--layer-01);
|
||||
border: var(--border-width) solid var(--border);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.detailMeta {
|
||||
margin: var(--space-2) 0 0;
|
||||
font-size: 11px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.close {
|
||||
flex: none;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
transition: background var(--dur-fast) var(--ease);
|
||||
}
|
||||
|
||||
.close:hover {
|
||||
background: var(--layer-01);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.close:focus-visible {
|
||||
outline: 2px solid var(--focus);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Toaster — renders the NotificationStore as a stack of toasts (spec §10,
|
||||
* design language → Toasts). Mounted once at the app root, beside ConfirmDialog.
|
||||
*
|
||||
* The non-blocking counterpart to ConfirmDialog: top-right, stacked newest-last,
|
||||
* each dismissible. Error/warning toasts persist until dismissed (Carbon: a
|
||||
* critical message shouldn't vanish on a timer); success/info auto-dismiss. A
|
||||
* failure that carries diagnostic `detail` exposes it under a collapsed
|
||||
* "Technical details" disclosure — available to report, without shouting.
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import {
|
||||
useNotificationStore,
|
||||
type Notification,
|
||||
type NotificationKind,
|
||||
} from '../stores/NotificationStore';
|
||||
import styles from './Toaster.module.css';
|
||||
|
||||
/** Auto-dismiss delay for the non-critical kinds (ms). Errors/warnings persist. */
|
||||
const AUTO_DISMISS_MS = 6000;
|
||||
|
||||
/** Kinds that fade on their own; errors and warnings wait for the user. */
|
||||
const AUTO_DISMISS: Record<NotificationKind, boolean> = {
|
||||
success: true,
|
||||
info: true,
|
||||
warning: false,
|
||||
error: false,
|
||||
};
|
||||
|
||||
const KIND_CLASS: Record<NotificationKind, string> = {
|
||||
error: styles.error,
|
||||
warning: styles.warning,
|
||||
success: styles.success,
|
||||
info: styles.info,
|
||||
};
|
||||
|
||||
function Toast({ notification }: { notification: Notification }) {
|
||||
const dismiss = useNotificationStore((s) => s.dismiss);
|
||||
const { id, kind, title, message, detail } = notification;
|
||||
|
||||
// Positive/informational toasts clear themselves after a beat; the close
|
||||
// button still works for an early dismissal.
|
||||
useEffect(() => {
|
||||
if (!AUTO_DISMISS[kind]) return;
|
||||
const timer = setTimeout(() => dismiss(id), AUTO_DISMISS_MS);
|
||||
return () => clearTimeout(timer);
|
||||
}, [id, kind, dismiss]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`${styles.toast} ${KIND_CLASS[kind]}`}
|
||||
// Errors/warnings interrupt assistive tech (assertive); the rest are polite.
|
||||
role={kind === 'error' || kind === 'warning' ? 'alert' : 'status'}
|
||||
>
|
||||
<div className={styles.body}>
|
||||
<p className={styles.title}>{title}</p>
|
||||
<p className={styles.message}>{message}</p>
|
||||
{detail !== undefined && (
|
||||
<details className={styles.detail}>
|
||||
<summary className={styles.detailSummary}>Technical details</summary>
|
||||
<pre className={styles.detailText}>{detail}</pre>
|
||||
<p className={styles.detailMeta}>Astrolabe v{__APP_VERSION__}</p>
|
||||
</details>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className={styles.close}
|
||||
aria-label="Dismiss notification"
|
||||
onClick={() => dismiss(id)}
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function Toaster() {
|
||||
const notifications = useNotificationStore((s) => s.notifications);
|
||||
if (notifications.length === 0) return null;
|
||||
return (
|
||||
<div className={styles.region} role="region" aria-label="Notifications">
|
||||
{notifications.map((n) => (
|
||||
<Toast key={n.id} notification={n} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user