Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(dashboard): de-dupe connection-lost toasts to prevent spam (#293)

Quintona00ef43627c6
diff --git a/dashboard/src/components/Toast.tsx b/dashboard/src/components/Toast.tsx
index 4769dbc41..add746405 100644
--- a/dashboard/src/components/Toast.tsx
+++ b/dashboard/src/components/Toast.tsx
@@ -11,8 +11,17 @@ interface Toast {
title: string;
message?: string;
duration?: number;
+ /** Stable, non-rendered key for de-duplicating recurring toasts. Independent of the (translated) title. */
+ dedupeKey?: string;
}
+// A toast id needs no cryptographic strength; crypto.randomUUID is undefined over plain HTTP on a LAN IP.
+const createToastId = (): string => crypto.randomUUID?.() ?? `t-${Date.now()}-${Math.random().toString(36).slice(2)}`;
+
+// De-dupe sentinel for the "backend unreachable" toast. Kept separate from the displayed title so
+// translating the title never silently breaks the de-dupe.
+const CONNECTION_LOST_DEDUPE_KEY = 'connection-lost';
+
interface ToastContextValue {
toasts: Toast[];
addToast: (toast: Omit<Toast, 'id'>) => void;
@@ -38,6 +47,7 @@ interface ToastProviderProps {
}
export function ToastProvider({ children }: ToastProviderProps) {
+ const { t } = useTranslation();
const [toasts, setToasts] = useState<Toast[]>([]);
const removeToast = useCallback((id: string) => {
@@ -46,10 +56,7 @@ export function ToastProvider({ children }: ToastProviderProps) {
const addToast = useCallback(
(toast: Omit<Toast, 'id'>) => {
- // crypto.randomUUID() is only defined in a secure context (HTTPS / localhost). Over plain
- // HTTP on a LAN IP it is undefined, which previously threw "crypto.randomUUID is not a
- // function" on every toast — fall back to a non-crypto id (a toast key needs no strength).
- const id = crypto.randomUUID?.() ?? `t-${Date.now()}-${Math.random().toString(36).slice(2)}`;
+ const id = createToastId();
const newToast = { ...toast, id };
setToasts(prev => [...prev, newToast]);
@@ -71,9 +78,41 @@ export function ToastProvider({ children }: ToastProviderProps) {
const error = useCallback(
(title: string, message?: string) => {
+ const isConnectionError =
+ message?.toLowerCase().includes('failed to fetch') ||
+ message?.toLowerCase().includes('networkerror') ||
+ message?.toLowerCase().includes('http 502') ||
+ message?.toLowerCase().includes('http 503') ||
+ title.toLowerCase().includes('failed to fetch') ||
+ title.toLowerCase().includes('networkerror');
+
+ if (isConnectionError) {
+ // De-dupe on the stable key (not the translated title) so a downed backend shows one toast.
+ // The auto-dismiss timer is scheduled OUTSIDE the updater: state updaters must be
+ // side-effect-free (React Strict Mode runs them twice, which would schedule two timers).
+ // If this is a duplicate the id is never committed, so removeToast(id) later is a harmless no-op.
+ const id = createToastId();
+ setToasts(prev =>
+ prev.some(t => t.dedupeKey === CONNECTION_LOST_DEDUPE_KEY)
+ ? prev
+ : [
+ ...prev,
+ {
+ id,
+ type: 'error',
+ dedupeKey: CONNECTION_LOST_DEDUPE_KEY,
+ title: t('toast.connectionLost.title'),
+ message: t('toast.connectionLost.message'),
+ duration: 6000,
+ },
+ ],
+ );
+ setTimeout(() => removeToast(id), 6000);
+ return;
+ }
addToast({ type: 'error', title, message, duration: 6000 });
},
- [addToast],
+ [addToast, removeToast, t],
);
const warning = useCallback(
diff --git a/dashboard/src/i18n/locales/ar.json b/dashboard/src/i18n/locales/ar.json
index 0b4bfdd3e..1d37aeb06 100644
--- a/dashboard/src/i18n/locales/ar.json
+++ b/dashboard/src/i18n/locales/ar.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "فُقد الاتصال بالخادم",
+ "message": "تعذّر الوصول إلى الخادم الخلفي. يرجى التحقق مما إذا كان الخادم قيد التشغيل."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "واجهة برمجة واتساب",
diff --git a/dashboard/src/i18n/locales/en.json b/dashboard/src/i18n/locales/en.json
index 7158da66a..acc7a2175 100644
--- a/dashboard/src/i18n/locales/en.json
+++ b/dashboard/src/i18n/locales/en.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "Server Connection Lost",
+ "message": "The backend server is unreachable. Please check if the server is running."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",
diff --git a/dashboard/src/i18n/locales/es.json b/dashboard/src/i18n/locales/es.json
index 43d0dcc2d..1fef3c3e9 100644
--- a/dashboard/src/i18n/locales/es.json
+++ b/dashboard/src/i18n/locales/es.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "Conexión con el servidor perdida",
+ "message": "No se puede acceder al servidor backend. Comprueba si el servidor está en ejecución."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "API de WhatsApp",
diff --git a/dashboard/src/i18n/locales/fr.json b/dashboard/src/i18n/locales/fr.json
index 61e96a543..3dbd63792 100644
--- a/dashboard/src/i18n/locales/fr.json
+++ b/dashboard/src/i18n/locales/fr.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "Connexion au serveur perdue",
+ "message": "Le serveur backend est injoignable. Veuillez vérifier qu'il est en cours d'exécution."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",
diff --git a/dashboard/src/i18n/locales/he.json b/dashboard/src/i18n/locales/he.json
index 641b46531..3707454bc 100644
--- a/dashboard/src/i18n/locales/he.json
+++ b/dashboard/src/i18n/locales/he.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "החיבור לשרת אבד",
+ "message": "לא ניתן להגיע לשרת העורפי. אנא בדקו אם השרת פועל."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "ממשק WhatsApp API",
diff --git a/dashboard/src/i18n/locales/it.json b/dashboard/src/i18n/locales/it.json
index c1c6481b5..71f0ad8fb 100644
--- a/dashboard/src/i18n/locales/it.json
+++ b/dashboard/src/i18n/locales/it.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "Connessione al server persa",
+ "message": "Il server backend non è raggiungibile. Verifica che il server sia in esecuzione."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",
diff --git a/dashboard/src/i18n/locales/te.json b/dashboard/src/i18n/locales/te.json
index 1608f563d..4714871f6 100644
--- a/dashboard/src/i18n/locales/te.json
+++ b/dashboard/src/i18n/locales/te.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "సర్వర్ కనెక్షన్ తెగిపోయింది",
+ "message": "బ్యాకెండ్ సర్వర్‌ను చేరుకోలేకపోయాం. సర్వర్ నడుస్తుందో లేదో తనిఖీ చేయండి."
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",
diff --git a/dashboard/src/i18n/locales/zh-CN.json b/dashboard/src/i18n/locales/zh-CN.json
index 90cb54dd7..fc6e1836a 100644
--- a/dashboard/src/i18n/locales/zh-CN.json
+++ b/dashboard/src/i18n/locales/zh-CN.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "服务器连接已断开",
+ "message": "无法连接到后端服务器。请检查服务器是否正在运行。"
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",
diff --git a/dashboard/src/i18n/locales/zh-HK.json b/dashboard/src/i18n/locales/zh-HK.json
index 12401fb5b..45f5b8319 100644
--- a/dashboard/src/i18n/locales/zh-HK.json
+++ b/dashboard/src/i18n/locales/zh-HK.json
@@ -1,4 +1,10 @@
{
+ "toast": {
+ "connectionLost": {
+ "title": "伺服器連線已中斷",
+ "message": "無法連接到後端伺服器。請檢查伺服器是否正在執行。"
+ }
+ },
"common": {
"appName": "OpenWA",
"appSubtitle": "WhatsApp API",