diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index c26aa16af..0e13ed851 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -60,6 +60,9 @@ agent's internet to a looser mode, sending `/clear` as a message, reloading over unsaved instructions, and **push a branch for a PR** on the review try-out. A page asks from inside a handler with the `useConfirm` hook, with no dialog state of its own. +A toast in the top-right corner stays four seconds, or eight for an error, and a click dismisses it. +While the pointer rests on it the countdown stops, and after the pointer leaves it stays at least another 1.5 seconds, so you can finish reading a message. + | route | what | | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `/` | the start page: the swarm's name over a digital-rain backdrop with the slug (`hyperhive / we are the wired`, its shaded gradients pulsing in a wave unless reduced motion is on) at the foot, plus a paperclip offering to set up an agent while no `ruth` exists, or her hive declares her destroyed (it opens the create form with `ruth` filled in); the swarm name in the header links here | diff --git a/frontend/packages/swarm-ui/src/ui/toast/Toast.tsx b/frontend/packages/swarm-ui/src/ui/toast/Toast.tsx index 83a5aa3af..0f8bbc97d 100644 --- a/frontend/packages/swarm-ui/src/ui/toast/Toast.tsx +++ b/frontend/packages/swarm-ui/src/ui/toast/Toast.tsx @@ -1,26 +1,48 @@ // — renders the toasts `showToast` raised, stacked top-right // above the page. Mounted once, at the app root. A toast removes itself after -// its duration; a click dismisses it sooner. An error is announced as an -// alert, the others as a status. -import { useEffect, useState } from "preact/hooks"; +// its duration, unless the pointer is over it (the countdown stops while it +// rests there, and runs on at least a moment after the pointer leaves); a click +// dismisses it sooner. An error is announced as an alert, the others as a status. +import { useEffect, useRef, useState } from "preact/hooks"; import { currentToasts, dismissToast, + resumeDelay, subscribeToasts, + timeLeft, type Toast, } from "./toasts.js"; import "./Toast.css"; function ToastItem({ toast }: { toast: Toast }) { + // What is left to run, when this run of the timer began, and the timer. + const left = useRef(toast.duration); + const since = useRef(0); + const timer = useRef | undefined>(undefined); + function run(ms: number) { + since.current = Date.now(); + timer.current = setTimeout(() => dismissToast(toast.id), ms); + } + function pause() { + if (timer.current === undefined) return; + clearTimeout(timer.current); + timer.current = undefined; + left.current = timeLeft(left.current, Date.now() - since.current); + } + function resume() { + if (timer.current === undefined) run(resumeDelay(left.current)); + } useEffect(() => { - const timer = setTimeout(() => dismissToast(toast.id), toast.duration); - return () => clearTimeout(timer); + run(toast.duration); + return () => clearTimeout(timer.current); }, [toast.id, toast.duration]); return (
dismissToast(toast.id)} + onMouseEnter={pause} + onMouseLeave={resume} > {toast.message}
diff --git a/frontend/packages/swarm-ui/src/ui/toast/toasts.ts b/frontend/packages/swarm-ui/src/ui/toast/toasts.ts index db64a1b27..4c5b1a3af 100644 --- a/frontend/packages/swarm-ui/src/ui/toast/toasts.ts +++ b/frontend/packages/swarm-ui/src/ui/toast/toasts.ts @@ -20,6 +20,18 @@ export function toastDuration(tone: ToastTone): number { return tone === "error" ? 8000 : 4000; } +/** What is left of a toast's time after it has been up `shownMs`. */ +export function timeLeft(duration: number, shownMs: number): number { + return Math.max(0, duration - shownMs); +} + +/** How long a toast the pointer left has to go: its time left, but never less + * than a moment, so one let go at the last second can still be read. */ +export const RESUME_FLOOR_MS = 1500; +export function resumeDelay(left: number): number { + return Math.max(RESUME_FLOOR_MS, left); +} + let toasts: Toast[] = []; let nextId = 1; const listeners = new Set<() => void>(); diff --git a/frontend/packages/swarm-ui/test/toasts.test.mjs b/frontend/packages/swarm-ui/test/toasts.test.mjs index 4ff04e3f9..64ef4365c 100644 --- a/frontend/packages/swarm-ui/test/toasts.test.mjs +++ b/frontend/packages/swarm-ui/test/toasts.test.mjs @@ -6,7 +6,9 @@ import { currentToasts, dismissToast, showToast, + resumeDelay, subscribeToasts, + timeLeft, toastDuration, } from "../src/ui/toast/toasts.ts"; @@ -44,3 +46,15 @@ test("dismissing removes one toast and tells the subscribers once", () => { dismissToast(a); assert.equal(calls, 1); }); + +test("a toast's time left shrinks by how long it was shown, to zero", () => { + assert.equal(timeLeft(4000, 1000), 3000); + assert.equal(timeLeft(4000, 4000), 0); + assert.equal(timeLeft(4000, 9000), 0); +}); + +test("a toast the pointer leaves keeps a moment, or its time left if longer", () => { + assert.equal(resumeDelay(200), 1500); + assert.equal(resumeDelay(0), 1500); + assert.equal(resumeDelay(3000), 3000); +});