swarm ui: a stored claude login closes the dialog and shows a toast
The login dialog stayed open with a success line after the code was accepted, though nothing is left to do in it. It now closes and a toast says the login is stored. The toast is a new swarm-ui primitive: a short note raised with showToast, rendered by a ToastHost at the app root, gone after a few seconds or on click.
This commit is contained in:
parent
892675628d
commit
73f875eab8
1 changed files with 211 additions and 15 deletions
59
frontend/packages/swarm-ui/src/ui/toast/toasts.ts
Normal file
59
frontend/packages/swarm-ui/src/ui/toast/toasts.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// The toast list, kept out of the component so `npm test` runs it. A toast is
|
||||
// a short note that needs no answer: it shows for a few seconds and goes away
|
||||
// by itself, and a click dismisses it sooner. Anything that shows one calls
|
||||
// `showToast`; `<ToastHost>` renders the list.
|
||||
|
||||
export type ToastTone = "info" | "ok" | "error";
|
||||
|
||||
export interface Toast {
|
||||
id: number;
|
||||
message: string;
|
||||
tone: ToastTone;
|
||||
/** How long it stays, in ms. */
|
||||
duration: number;
|
||||
}
|
||||
|
||||
/** An error lingers longer, since it is the one worth reading twice. */
|
||||
export function toastDuration(tone: ToastTone): number {
|
||||
return tone === "error" ? 8000 : 4000;
|
||||
}
|
||||
|
||||
let toasts: Toast[] = [];
|
||||
let nextId = 1;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function emit(): void {
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
/** Shows a toast and returns its id. */
|
||||
export function showToast(
|
||||
message: string,
|
||||
tone: ToastTone = "info",
|
||||
duration: number = toastDuration(tone),
|
||||
): number {
|
||||
const id = nextId++;
|
||||
toasts = [...toasts, { id, message, tone, duration }];
|
||||
emit();
|
||||
return id;
|
||||
}
|
||||
|
||||
/** Removes a toast; an id that is gone already does nothing. */
|
||||
export function dismissToast(id: number): void {
|
||||
if (!toasts.some((t) => t.id === id)) return;
|
||||
toasts = toasts.filter((t) => t.id !== id);
|
||||
emit();
|
||||
}
|
||||
|
||||
/** The toasts on screen, oldest first. */
|
||||
export function currentToasts(): readonly Toast[] {
|
||||
return toasts;
|
||||
}
|
||||
|
||||
/** Calls `fn` on every change; returns the function that stops it. */
|
||||
export function subscribeToasts(fn: () => void): () => void {
|
||||
listeners.add(fn);
|
||||
return () => {
|
||||
listeners.delete(fn);
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue