Files
gods-eye-view/src/ui/toast.js
T
Sameh Khamis f846aaed4c feat(ui): render the page toast from the status store
ShellFeedback and the scene director show toasts through createToast(),
each on its own timer as before, and the scene chrome renders the toast.
Every show rewrites the text, so a repeated message is announced again.
2026-10-31 19:58:59 -07:00

43 lines
1.3 KiB
JavaScript

import { useStatusStore } from './stores/status.js';
import { applicationPinia } from './vueRuntime.js';
// Counts every show on the page, so a repeated message is announced again.
let shown = 0;
/**
* One owner's use of the page toast, which the scene chrome renders from the
* status store. Each owner keeps its own timer, so a later toast from another
* owner is hidden when this one's time is up, as the shared element was.
* @param {object} options
* @param {number} options.durationMs How long a toast stays up.
* @param {object} [options.store] The status store.
*/
export function createToast({
durationMs,
store = useStatusStore(applicationPinia()),
}) {
let timer = null;
// The text stays while the toast fades out.
const hide = () => {
clearTimeout(timer);
timer = null;
if (store.toast?.visible)
store.publishToast({ ...store.toast, visible: false });
};
return {
/** @param {string} message */
show(message) {
store.publishToast({ text: message, visible: true, shown: ++shown });
clearTimeout(timer);
timer = setTimeout(hide, durationMs);
},
/** Hide the toast now and drop the timer. */
hide,
/** Drop the timer, leaving the toast as it is. */
cancel() {
clearTimeout(timer);
timer = null;
},
};
}