Merge pull request #323 from ArnasDon/fix/whatsapp-config-draft-preserve

fix(settings): preserve WhatsApp credential drafts on tab refocus
This commit is contained in:
Arnas Donauskas 2026-07-01 11:18:59 +03:00 committed by GitHub
commit e60b08ac2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
'use client'; 'use client';
import { useEffect, useState, useCallback } from 'react'; import { useEffect, useRef, useState, useCallback } from 'react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { import {
Eye, Eye,
@ -53,6 +53,13 @@ export function WhatsAppConfig() {
const [connectionStatus, setConnectionStatus] = useState<ConnectionStatus>('unknown'); const [connectionStatus, setConnectionStatus] = useState<ConnectionStatus>('unknown');
const [resetReason, setResetReason] = useState<ResetReason>(null); const [resetReason, setResetReason] = useState<ResetReason>(null);
const [statusMessage, setStatusMessage] = useState<string>(''); const [statusMessage, setStatusMessage] = useState<string>('');
// Guards against re-hydrating the form when the load effect below
// re-runs for reasons unrelated to actually switching accounts —
// e.g. Supabase's onAuthStateChange fires a token refresh (new
// `user` object, profileLoading flips true/false) when the browser
// tab regains focus. Without this, that churn calls fetchConfig()
// again and overwrites whatever the user typed but hadn't saved yet.
const loadedAccountIdRef = useRef<string | null>(null);
const [phoneNumberId, setPhoneNumberId] = useState(''); const [phoneNumberId, setPhoneNumberId] = useState('');
const [wabaId, setWabaId] = useState(''); const [wabaId, setWabaId] = useState('');
@ -164,9 +171,12 @@ export function WhatsAppConfig() {
// once the profile arrives. // once the profile arrives.
if (authLoading || profileLoading) return; if (authLoading || profileLoading) return;
if (!user || !accountId) { if (!user || !accountId) {
loadedAccountIdRef.current = null;
setLoading(false); setLoading(false);
return; return;
} }
if (loadedAccountIdRef.current === accountId) return;
loadedAccountIdRef.current = accountId;
fetchConfig(accountId); fetchConfig(accountId);
}, [authLoading, profileLoading, user, accountId, fetchConfig]); }, [authLoading, profileLoading, user, accountId, fetchConfig]);