From f379a9554811f0892ce9761b1aecdf56050ed017 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 20 Oct 2025 12:47:18 +0700 Subject: [PATCH] clear message cache on relay change --- src/contexts/DMContext.tsx | 81 ++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/src/contexts/DMContext.tsx b/src/contexts/DMContext.tsx index f11c6d3..6b46139 100644 --- a/src/contexts/DMContext.tsx +++ b/src/contexts/DMContext.tsx @@ -2,6 +2,7 @@ import React, { createContext, useContext, useEffect, useState, ReactNode, useCa import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useSendDM } from '@/hooks/useSendDM'; import { useNostr } from '@nostrify/react'; +import { useAppContext } from '@/hooks/useAppContext'; import { validateDMEvent } from '@/lib/dmUtils'; import { LOADING_PHASES, type LoadingPhase, PROTOCOL_MODE, type ProtocolMode } from '@/lib/dmConstants'; import type { NostrEvent } from '@nostrify/nostrify'; @@ -194,9 +195,13 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, const { user } = useCurrentUser(); const { nostr } = useNostr(); const { sendNIP4Message, sendNIP17Message } = useSendDM(); + const { config } = useAppContext(); const userPubkey = useMemo(() => user?.pubkey, [user?.pubkey]); + // Track relay URL to detect changes + const previousRelayUrl = useRef(config.relayUrl); + // Determine if NIP-17 is enabled based on protocol mode const enableNIP17 = protocolMode !== PROTOCOL_MODE.NIP04_ONLY; @@ -989,6 +994,40 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, } }, [loadPreviousCachedMessages, queryRelaysForMessagesSince, startNIP4Subscription, startNIP17Subscription, enableNIP17]); + // Clear cache and reload + const clearCacheAndReload = useCallback(async () => { + if (!enabled || !userPubkey) return; + + try { + // Close existing subscriptions + if (nip4SubscriptionRef.current) { + nip4SubscriptionRef.current.close(); + nip4SubscriptionRef.current = null; + } + if (nip17SubscriptionRef.current) { + nip17SubscriptionRef.current.close(); + nip17SubscriptionRef.current = null; + } + + // Clear IndexedDB cache + const { deleteMessagesFromDB } = await import('@/lib/dmMessageStore'); + await deleteMessagesFromDB(userPubkey); + + // Reset all state + setMessages(new Map()); + setLastSync({ nip4: null, nip17: null }); + setSubscriptions({ isNIP4Connected: false, isNIP17Connected: false }); + setScanProgress({ nip4: null, nip17: null }); + setLoadingPhase(LOADING_PHASES.IDLE); + + // Trigger reload by setting hasInitialLoadCompleted to false + setHasInitialLoadCompleted(false); + } catch (error) { + console.error('[DM] Error clearing cache:', error); + throw error; + } + }, [enabled, userPubkey]); + // Main effect to load messages useEffect(() => { if (!enabled || !userPubkey || hasInitialLoadCompleted || isLoading) return; @@ -1029,6 +1068,15 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, }; }, [enabled]); + // Detect relay changes and reload messages + useEffect(() => { + if (!enabled || !userPubkey || !hasInitialLoadCompleted) return; + if (previousRelayUrl.current !== config.relayUrl) { + previousRelayUrl.current = config.relayUrl; + clearCacheAndReload(); + } + }, [enabled, userPubkey, config.relayUrl, hasInitialLoadCompleted, clearCacheAndReload]); + // Conversations summary const conversations = useMemo(() => { const conversationsList: ConversationSummary[] = []; @@ -1172,39 +1220,6 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, } }, [enabled, userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]); - const clearCacheAndReload = useCallback(async () => { - if (!enabled || !userPubkey) return; - - try { - // Close existing subscriptions - if (nip4SubscriptionRef.current) { - nip4SubscriptionRef.current.close(); - nip4SubscriptionRef.current = null; - } - if (nip17SubscriptionRef.current) { - nip17SubscriptionRef.current.close(); - nip17SubscriptionRef.current = null; - } - - // Clear IndexedDB cache - const { deleteMessagesFromDB } = await import('@/lib/dmMessageStore'); - await deleteMessagesFromDB(userPubkey); - - // Reset all state - setMessages(new Map()); - setLastSync({ nip4: null, nip17: null }); - setSubscriptions({ isNIP4Connected: false, isNIP17Connected: false }); - setScanProgress({ nip4: null, nip17: null }); - setLoadingPhase(LOADING_PHASES.IDLE); - - // Trigger reload by setting hasInitialLoadCompleted to false - setHasInitialLoadCompleted(false); - } catch (error) { - console.error('[DM] Error clearing cache:', error); - throw error; - } - }, [enabled, userPubkey]); - const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS); const contextValue: DMContextType = {