Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): split provider/hook files so fast refresh works

Yudhi Armyndharis621161fb6005
diff --git a/dashboard/src/App.tsx b/dashboard/src/App.tsx
index 09455cad4..72703709b 100644
--- a/dashboard/src/App.tsx
+++ b/dashboard/src/App.tsx
@@ -5,7 +5,8 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Loader2 } from 'lucide-react';
import { Layout } from './components/Layout';
import { ToastProvider } from './components/Toast';
-import { RoleProvider, useRole, type UserRole } from './hooks/useRole';
+import { useRole, type UserRole } from './hooks/useRole';
+import { RoleProvider } from './components/RoleProvider';
import { ErrorBoundary } from './components/ErrorBoundary';
import { API_BASE_URL } from './services/api';
import { clearActorState, resolveStartupValidation } from './utils/authLifecycle';
diff --git a/dashboard/src/components/PluginInstances.tsx b/dashboard/src/components/PluginInstances.tsx
index 639f656ab..f3911d4cf 100644
--- a/dashboard/src/components/PluginInstances.tsx
+++ b/dashboard/src/components/PluginInstances.tsx
@@ -12,7 +12,7 @@ import {
import { isValidInstanceId, isValidInstanceSecret, parseInstanceConfig } from '../utils/instanceForm';
import { copyToClipboard } from '../utils/clipboard';
import { Modal } from './Modal';
-import { useToast } from './Toast';
+import { useToast } from '../hooks/useToast';
import './PluginInstances.css';
const emptyForm = { instanceId: '', sessionScope: '', verifyToken: '', secret: '', config: '' };
diff --git a/dashboard/src/components/RoleProvider.tsx b/dashboard/src/components/RoleProvider.tsx
new file mode 100644
index 000000000..bf3587630
--- /dev/null
+++ b/dashboard/src/components/RoleProvider.tsx
@@ -0,0 +1,30 @@
+import { useState, useCallback, type ReactNode } from 'react';
+import type { UserRole, RoleContextType } from '../types/role';
+import { RoleContext } from '../hooks/useRole';
+
+export function RoleProvider({ children }: { children: ReactNode }) {
+ const [role, setRoleState] = useState<UserRole | null>(() => {
+ const saved = localStorage.getItem('openwa_user_role');
+ return (saved as UserRole) || null;
+ });
+
+ const setRole = useCallback((newRole: UserRole | null) => {
+ setRoleState(newRole);
+ if (newRole) {
+ localStorage.setItem('openwa_user_role', newRole);
+ } else {
+ localStorage.removeItem('openwa_user_role');
+ }
+ }, []);
+
+ const value: RoleContextType = {
+ role,
+ setRole,
+ isAdmin: role === 'admin',
+ isOperator: role === 'operator',
+ isViewer: role === 'viewer',
+ canWrite: role === 'admin' || role === 'operator',
+ };
+
+ return <RoleContext.Provider value={value}>{children}</RoleContext.Provider>;
+}
diff --git a/dashboard/src/components/Toast.tsx b/dashboard/src/components/Toast.tsx
index add746405..e7656aab4 100644
--- a/dashboard/src/components/Toast.tsx
+++ b/dashboard/src/components/Toast.tsx
@@ -1,20 +1,9 @@
-import { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
+import { useState, useCallback, type ReactNode } from 'react';
import { useTranslation } from 'react-i18next';
import { CheckCircle, XCircle, AlertCircle, Info, X } from 'lucide-react';
+import { ToastContext, type Toast } from '../hooks/useToast';
import './Toast.css';
-type ToastType = 'success' | 'error' | 'warning' | 'info';
-
-interface Toast {
- id: string;
- type: ToastType;
- 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)}`;
@@ -22,26 +11,6 @@ const createToastId = (): string => crypto.randomUUID?.() ?? `t-${Date.now()}-${
// 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;
- removeToast: (id: string) => void;
- success: (title: string, message?: string) => void;
- error: (title: string, message?: string) => void;
- warning: (title: string, message?: string) => void;
- info: (title: string, message?: string) => void;
-}
-
-const ToastContext = createContext<ToastContextValue | null>(null);
-
-export function useToast() {
- const context = useContext(ToastContext);
- if (!context) {
- throw new Error('useToast must be used within a ToastProvider');
- }
- return context;
-}
-
interface ToastProviderProps {
children: ReactNode;
}
diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsx
index b81f210c9..b75c259b6 100644
--- a/dashboard/src/components/chats/ChatComposer.tsx
+++ b/dashboard/src/components/chats/ChatComposer.tsx
@@ -7,7 +7,7 @@ import { mergeOrAppend, type ChatMessageView } from '../../utils/chatMessages';
import { promoteChatWithSnippet } from '../../utils/chatList';
import { messagesQueryKey, useChatMessagesActions } from '../../hooks/useChatMessages';
import { useRole } from '../../hooks/useRole';
-import { useToast } from '../Toast';
+import { useToast } from '../../hooks/useToast';
import type { ScrollDirection } from '../../utils/scrollDecision';
// Map an attachment MIME type to the neutral MessageType for the optimistic outgoing bubble, so the
diff --git a/dashboard/src/components/chats/StatusComposeModal.tsx b/dashboard/src/components/chats/StatusComposeModal.tsx
index ab578c693..54618e353 100644
--- a/dashboard/src/components/chats/StatusComposeModal.tsx
+++ b/dashboard/src/components/chats/StatusComposeModal.tsx
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { Loader2 } from 'lucide-react';
import { contactApi, sessionApi } from '../../services/api';
import { useCurrentEngineQuery } from '../../hooks/queries';
-import { useToast } from '../Toast';
+import { useToast } from '../../hooks/useToast';
import { Modal } from '../Modal';
// Mirrors @ArrayMaxSize(256) on the send-status DTOs — the picker caps selection client-side so the
diff --git a/dashboard/src/hooks/useRole.tsx b/dashboard/src/hooks/useRole.tsx
index ad8fe341a..4238347ef 100644
--- a/dashboard/src/hooks/useRole.tsx
+++ b/dashboard/src/hooks/useRole.tsx
@@ -1,36 +1,9 @@
-import { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
-import type { UserRole, RoleContextType } from '../types/role';
+import { createContext, useContext } from 'react';
+import type { RoleContextType } from '../types/role';
export type { UserRole, RoleContextType } from '../types/role';
-const RoleContext = createContext<RoleContextType | undefined>(undefined);
-
-export function RoleProvider({ children }: { children: ReactNode }) {
- const [role, setRoleState] = useState<UserRole | null>(() => {
- const saved = localStorage.getItem('openwa_user_role');
- return (saved as UserRole) || null;
- });
-
- const setRole = useCallback((newRole: UserRole | null) => {
- setRoleState(newRole);
- if (newRole) {
- localStorage.setItem('openwa_user_role', newRole);
- } else {
- localStorage.removeItem('openwa_user_role');
- }
- }, []);
-
- const value: RoleContextType = {
- role,
- setRole,
- isAdmin: role === 'admin',
- isOperator: role === 'operator',
- isViewer: role === 'viewer',
- canWrite: role === 'admin' || role === 'operator',
- };
-
- return <RoleContext.Provider value={value}>{children}</RoleContext.Provider>;
-}
+export const RoleContext = createContext<RoleContextType | undefined>(undefined);
export function useRole(): RoleContextType {
const context = useContext(RoleContext);
diff --git a/dashboard/src/hooks/useToast.ts b/dashboard/src/hooks/useToast.ts
new file mode 100644
index 000000000..b0111da32
--- /dev/null
+++ b/dashboard/src/hooks/useToast.ts
@@ -0,0 +1,33 @@
+import { createContext, useContext } from 'react';
+
+export type ToastType = 'success' | 'error' | 'warning' | 'info';
+
+export interface Toast {
+ id: string;
+ type: ToastType;
+ title: string;
+ message?: string;
+ duration?: number;
+ /** Stable, non-rendered key for de-duplicating recurring toasts. Independent of the (translated) title. */
+ dedupeKey?: string;
+}
+
+export interface ToastContextValue {
+ toasts: Toast[];
+ addToast: (toast: Omit<Toast, 'id'>) => void;
+ removeToast: (id: string) => void;
+ success: (title: string, message?: string) => void;
+ error: (title: string, message?: string) => void;
+ warning: (title: string, message?: string) => void;
+ info: (title: string, message?: string) => void;
+}
+
+export const ToastContext = createContext<ToastContextValue | null>(null);
+
+export function useToast() {
+ const context = useContext(ToastContext);
+ if (!context) {
+ throw new Error('useToast must be used within a ToastProvider');
+ }
+ return context;
+}
diff --git a/dashboard/src/pages/ApiKeys.tsx b/dashboard/src/pages/ApiKeys.tsx
index f5c99bcac..490220497 100644
--- a/dashboard/src/pages/ApiKeys.tsx
+++ b/dashboard/src/pages/ApiKeys.tsx
@@ -30,7 +30,7 @@ import {
} from '../hooks/queries';
import { PageHeader } from '../components/PageHeader';
import { Modal } from '../components/Modal';
-import { useToast } from '../components/Toast';
+import { useToast } from '../hooks/useToast';
import { copyToClipboard } from '../utils/clipboard';
import './ApiKeys.css';
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 1f187de0c..ebd3e83da 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -102,7 +102,7 @@ function installFetchStub(): void {
type RTL = typeof import('@testing-library/react');
type ChatsModule = typeof import('./Chats.tsx');
-type RoleModule = typeof import('../hooks/useRole.tsx');
+type RoleModule = typeof import('../components/RoleProvider.tsx');
type ToastModule = typeof import('../components/Toast.tsx');
let rtl: RTL;
@@ -123,7 +123,7 @@ before(async () => {
// handled by the registered loader hooks).
await import('../i18n/index.ts');
rtl = await import('@testing-library/react');
- ({ RoleProvider } = await import('../hooks/useRole.tsx'));
+ ({ RoleProvider } = await import('../components/RoleProvider.tsx'));
({ ToastProvider } = await import('../components/Toast.tsx'));
({ Chats } = await import('./Chats.tsx'));
});
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 879bc2c7d..4d6b88dac 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -37,7 +37,7 @@ import {
} from '../utils/chatMessages';
import { useWebSocket } from '../hooks/useWebSocket';
import { useDocumentTitle } from '../hooks/useDocumentTitle';
-import { useToast } from '../components/Toast';
+import { useToast } from '../hooks/useToast';
import { PageHeader } from '../components/PageHeader';
import { GlobalSearch } from '../components/GlobalSearch';
import { useChatMessages, useChatMessagesActions, messagesQueryKey } from '../hooks/useChatMessages';
diff --git a/dashboard/src/pages/Infrastructure.tsx b/dashboard/src/pages/Infrastructure.tsx
index 1c19af7a6..c1c324b22 100644
--- a/dashboard/src/pages/Infrastructure.tsx
+++ b/dashboard/src/pages/Infrastructure.tsx
@@ -20,7 +20,7 @@ import { useDocumentTitle } from '../hooks/useDocumentTitle';
import { useInfraStatusQuery, useInfraConfigQuery, useEnginesQuery, useCurrentEngineQuery } from '../hooks/queries';
import { PageHeader } from '../components/PageHeader';
import { Modal } from '../components/Modal';
-import { useToast } from '../components/Toast';
+import { useToast } from '../hooks/useToast';
import './Infrastructure.css';
import sqliteIcon from '../assets/icons/sqlite.svg';
diff --git a/dashboard/src/pages/Plugins.tsx b/dashboard/src/pages/Plugins.tsx
index a8c65ef81..c75e535b7 100644
--- a/dashboard/src/pages/Plugins.tsx
+++ b/dashboard/src/pages/Plugins.tsx
@@ -33,7 +33,7 @@ import { useTheme } from '../hooks/useTheme';
import { usePluginsQuery, useSessionsQuery, queryKeys } from '../hooks/queries';
import { PageHeader } from '../components/PageHeader';
import { Modal } from '../components/Modal';
-import { useToast } from '../components/Toast';
+import { useToast } from '../hooks/useToast';
import { PluginInstances } from '../components/PluginInstances';
import './Plugins.css';
diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsx
index 8836b1722..f541f4c58 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -27,7 +27,7 @@ import {
} from '../utils/sessionActions';
import { invalidateSessionQueries, reconcileSessionCache } from '../utils/sessionMutation';
import { canCreateSession, filterSessions, isValidPairingPhone, sessionNameIssues } from '../utils/sessionForm';
-import { useToast } from '../components/Toast';
+import { useToast } from '../hooks/useToast';
import { useWebSocket } from '../hooks/useWebSocket';
import { useRole } from '../hooks/useRole';
import {