fix email store lazy load
This commit is contained in:
@@ -110,6 +110,7 @@ export default function Home() {
|
|||||||
const [isProtocolAccountSwitching, setIsProtocolAccountSwitching] = useState(false);
|
const [isProtocolAccountSwitching, setIsProtocolAccountSwitching] = useState(false);
|
||||||
const markAsReadTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const markAsReadTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
const lastUndoToastSubmissionRef = useRef<string | null>(null);
|
const lastUndoToastSubmissionRef = useRef<string | null>(null);
|
||||||
|
const initialMailLoadClientRef = useRef<object | null>(null);
|
||||||
const { isAuthenticated, client, logout, checkAuth, switchAccount, activeAccountId, isLoading: authLoading, connectionLost, isRateLimited, rateLimitUntil } = useAuthStore();
|
const { isAuthenticated, client, logout, checkAuth, switchAccount, activeAccountId, isLoading: authLoading, connectionLost, isRateLimited, rateLimitUntil } = useAuthStore();
|
||||||
const { identities } = useIdentityStore();
|
const { identities } = useIdentityStore();
|
||||||
useIdentitySync();
|
useIdentitySync();
|
||||||
@@ -914,18 +915,26 @@ export default function Home() {
|
|||||||
}, [isAuthenticated, client, handleMailtoProtocolRequest]);
|
}, [isAuthenticated, client, handleMailtoProtocolRequest]);
|
||||||
|
|
||||||
// Fallback fetch for paths that didn't go through login()'s prefetch
|
// Fallback fetch for paths that didn't go through login()'s prefetch
|
||||||
// (notably checkAuth on page refresh). The prefetch in auth-store/login()
|
// (notably checkAuth on page refresh). Settings pages can also prefill
|
||||||
// populates mailboxes before this effect first runs, so on the post-login
|
// mailboxes without emails, so bootstrap emails once per client when the
|
||||||
// path this block is a no-op.
|
// mail route mounts even if mailbox data is already present.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAuthenticated && client && mailboxes.length === 0) {
|
if (!isAuthenticated || !client) {
|
||||||
|
initialMailLoadClientRef.current = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (initialMailLoadClientRef.current === client) return;
|
||||||
|
initialMailLoadClientRef.current = client;
|
||||||
|
|
||||||
let retryTimer: ReturnType<typeof setTimeout> | null = null;
|
let retryTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
|
||||||
const loadData = async (attempt = 1) => {
|
const loadData = async (attempt = 1) => {
|
||||||
try {
|
try {
|
||||||
|
const needsMailboxes = useEmailStore.getState().mailboxes.length === 0;
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fetchMailboxes(client),
|
needsMailboxes ? fetchMailboxes(client) : Promise.resolve(),
|
||||||
fetchQuota(client)
|
fetchQuota(client)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -951,6 +960,7 @@ export default function Home() {
|
|||||||
fetchTagCounts(client);
|
fetchTagCounts(client);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error loading email data:', error);
|
console.error('Error loading email data:', error);
|
||||||
|
initialMailLoadClientRef.current = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
loadData();
|
loadData();
|
||||||
@@ -959,8 +969,7 @@ export default function Home() {
|
|||||||
cancelled = true;
|
cancelled = true;
|
||||||
if (retryTimer) clearTimeout(retryTimer);
|
if (retryTimer) clearTimeout(retryTimer);
|
||||||
};
|
};
|
||||||
}
|
}, [isAuthenticated, client, fetchMailboxes, fetchEmails, fetchQuota, fetchTagCounts, refreshScheduledMetadata]);
|
||||||
}, [isAuthenticated, client, mailboxes.length, fetchMailboxes, fetchEmails, fetchQuota, fetchTagCounts, refreshScheduledMetadata]);
|
|
||||||
|
|
||||||
// Push notifications: set up once per client and tear down when the client
|
// Push notifications: set up once per client and tear down when the client
|
||||||
// goes away (logout or account switch). Kept separate from the fetch effect
|
// goes away (logout or account switch). Kept separate from the fetch effect
|
||||||
|
|||||||
Reference in New Issue
Block a user