Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the open chat when the UI language changes
Yudhi Armyndharisff2241f06707
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 7170bb8fd..c4e1df987 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -851,6 +851,35 @@ test('a chat list that answers after the user switched sessions does not replace}});+test('changing the UI language keeps the selected session and the open chat', async () => {+ const { screen, fireEvent, within, waitFor, act } = rtl;+ const { default: i18n } = await import('../i18n/index.ts');+ twoSessions = true;+ try {+ const { container } = renderChats();+ await screen.findByText('Main (15551234567)');+ const selector = container.querySelector('select.session-selector') as HTMLSelectElement;+ fireEvent.change(selector, { target: { value: SESSION_2.id } });+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');++ const sessionLoads = countFetchCalls('GET', '/api/sessions');+ await act(async () => {+ await i18n.changeLanguage('de');+ });+ await flush();++ assert.equal(countFetchCalls('GET', '/api/sessions'), sessionLoads, 'the language change reloaded the sessions');+ assert.equal(selector.value, SESSION_2.id, 'the language change reselected the first session');+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the language change closed the chat'));+ } finally {+ await act(async () => {+ await i18n.changeLanguage('en');+ });+ twoSessions = false;+ }+});+/*** The server-side inline-media budget replaces an over-budget payload with `{ omitted: true }`. This* thread requests the largest page size and caches it with staleTime Infinity, so without a fetch ofdiff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 53250028f..4ede921c2 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -274,6 +274,19 @@ export function Chats() {const activePhoneText =activePhoneDisplay ?? (resolvedPhoneQ.data ? formatPhoneForDisplay(resolvedPhoneQ.data) : null);+ // The list loaders below reach the translator and the error toast through a ref, not as+ // dependencies: both change identity on a language switch, which re-ran the session load (it+ // reselected the first session) and, through loadChats, the session-reset effect (it closed the+ // open chat and dropped a staged file or reply).+ const loadErrorRef = useRef({ t, showErrorToast });+ useEffect(() => {+ loadErrorRef.current = { t, showErrorToast };+ });+ const showLoadError = useCallback((key: string, err: unknown) => {+ const current = loadErrorRef.current;+ current.showErrorToast(current.t(key), err instanceof Error ? err.message : undefined);+ }, []);+// 1. Fetch available connected sessions on mountuseEffect(() => {const loadSessions = async () => {@@ -286,13 +299,13 @@ export function Chats() {setSelectedSessionId(readySessions[0].id);}} catch (err) {- showErrorToast(t('chats.errors.loadSessions'), err instanceof Error ? err.message : undefined);+ showLoadError('chats.errors.loadSessions', err);} finally {setLoadingSessions(false);}};void loadSessions();- }, [t, showErrorToast]);+ }, [showLoadError]);// 2. Fetch chats when active session changes. Only the newest call may write: a session switch// does not cancel the list still loading for the session left behind, and a list that lands@@ -310,13 +323,13 @@ export function Chats() {setChats(sorted);} catch (err) {if (request !== chatsRequestRef.current) return;- showErrorToast(t('chats.errors.loadChats'), err instanceof Error ? err.message : undefined);+ showLoadError('chats.errors.loadChats', err);setChats([]);} finally {if (request === chatsRequestRef.current) setLoadingChats(false);}},- [t, showErrorToast],+ [showLoadError],);useEffect(() => {