dev-preprod-prod logic
This commit is contained in:
@@ -3,12 +3,30 @@
|
||||
// @PURPOSE: Manages toast notifications using a Svelte writable store.
|
||||
// @LAYER: UI-State
|
||||
|
||||
import { writable } from 'svelte/store';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
// [DEF:toasts:Data]
|
||||
// @PURPOSE: Writable store containing the list of active toasts.
|
||||
export const toasts = writable([]);
|
||||
// [/DEF:toasts:Data]
|
||||
export const toasts = writable([]);
|
||||
// [/DEF:toasts:Data]
|
||||
|
||||
const TOAST_DEDUP_WINDOW_MS = 1200;
|
||||
const recentToastByKey = new Map();
|
||||
|
||||
function buildToastKey(message, type) {
|
||||
return `${String(type || 'info')}::${String(message || '')}`;
|
||||
}
|
||||
|
||||
function shouldSkipDuplicateToast(message, type) {
|
||||
const key = buildToastKey(message, type);
|
||||
const now = Date.now();
|
||||
const previousAt = recentToastByKey.get(key);
|
||||
if (previousAt && now - previousAt < TOAST_DEDUP_WINDOW_MS) {
|
||||
return true;
|
||||
}
|
||||
recentToastByKey.set(key, now);
|
||||
return false;
|
||||
}
|
||||
|
||||
// [DEF:addToast:Function]
|
||||
// @PURPOSE: Adds a new toast message.
|
||||
@@ -17,12 +35,16 @@ export const toasts = writable([]);
|
||||
// @PARAM: message (string) - The message text.
|
||||
// @PARAM: type (string) - The type of toast (info, success, error).
|
||||
// @PARAM: duration (number) - Duration in ms before the toast is removed.
|
||||
export function addToast(message, type = 'info', duration = 3000) {
|
||||
const id = Math.random().toString(36).substr(2, 9);
|
||||
console.log(`[toasts.addToast][Action] Adding toast context={{'id': '${id}', 'type': '${type}', 'message': '${message}'}}`);
|
||||
toasts.update(all => [...all, { id, message, type }]);
|
||||
setTimeout(() => removeToast(id), duration);
|
||||
}
|
||||
export function addToast(message, type = 'info', duration = 3000) {
|
||||
if (shouldSkipDuplicateToast(message, type)) {
|
||||
console.log(`[toasts.addToast][Action] Duplicate skipped context={{'type': '${type}', 'message': '${message}'}}`);
|
||||
return;
|
||||
}
|
||||
const id = Math.random().toString(36).substr(2, 9);
|
||||
console.log(`[toasts.addToast][Action] Adding toast context={{'id': '${id}', 'type': '${type}', 'message': '${message}'}}`);
|
||||
toasts.update(all => [...all, { id, message, type }]);
|
||||
setTimeout(() => removeToast(id), duration);
|
||||
}
|
||||
// [/DEF:addToast:Function]
|
||||
|
||||
// [DEF:removeToast:Function]
|
||||
@@ -35,4 +57,4 @@ function removeToast(id) {
|
||||
toasts.update(all => all.filter(t => t.id !== id));
|
||||
}
|
||||
// [/DEF:removeToast:Function]
|
||||
// [/DEF:toasts_module:Module]
|
||||
// [/DEF:toasts_module:Module]
|
||||
|
||||
Reference in New Issue
Block a user