From 8bbb247ba8467f67fc08df868a4e240f95820675 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 20 Oct 2025 15:16:51 +0700 Subject: [PATCH] intercept hard refresh to clear cache --- src/components/dm/DMMessagingInterface.tsx | 4 +- src/components/dm/DMStatusInfo.tsx | 16 ++++---- src/contexts/DMContext.tsx | 45 +++++++++++++++++++--- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/components/dm/DMMessagingInterface.tsx b/src/components/dm/DMMessagingInterface.tsx index ecf169f..b6a725a 100644 --- a/src/components/dm/DMMessagingInterface.tsx +++ b/src/components/dm/DMMessagingInterface.tsx @@ -21,7 +21,7 @@ export const DMMessagingInterface = ({ className }: DMMessagingInterfaceProps) = const [selectedPubkey, setSelectedPubkey] = useState(null); const [statusModalOpen, setStatusModalOpen] = useState(false); const isMobile = useIsMobile(); - const { clearCacheAndReload } = useDMContext(); + const { clearCacheAndRefetch } = useDMContext(); // On mobile, show only one panel at a time const showConversationList = !isMobile || !selectedPubkey; @@ -46,7 +46,7 @@ export const DMMessagingInterface = ({ className }: DMMessagingInterfaceProps) = View loading status, cache info, and connection details - + diff --git a/src/components/dm/DMStatusInfo.tsx b/src/components/dm/DMStatusInfo.tsx index 953d0ed..ccad3c0 100644 --- a/src/components/dm/DMStatusInfo.tsx +++ b/src/components/dm/DMStatusInfo.tsx @@ -9,10 +9,10 @@ import { Separator } from '@/components/ui/separator'; import { useToast } from '@/hooks/useToast'; interface DMStatusInfoProps { - clearCacheAndReload?: () => Promise; + clearCacheAndRefetch?: () => Promise; } -export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => { +export const DMStatusInfo = ({ clearCacheAndRefetch }: DMStatusInfoProps) => { const [isClearing, setIsClearing] = useState(false); const { toast } = useToast(); const { @@ -25,14 +25,14 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => { } = useDMContext(); const handleClearCache = async () => { - if (!clearCacheAndReload) return; + if (!clearCacheAndRefetch) return; setIsClearing(true); try { - await clearCacheAndReload(); + await clearCacheAndRefetch(); toast({ title: 'Cache cleared', - description: 'Reloading messages from relays...', + description: 'Refetching messages from relays...', }); setIsClearing(false); } catch (error) { @@ -177,14 +177,14 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => { {/* Actions */} - {clearCacheAndReload && ( + {clearCacheAndRefetch && ( <>

Cache Management

- Clear all cached messages and reload from relays. This will force a fresh sync. + Clear all cached messages and refetch from relays. This will force a fresh sync.

diff --git a/src/contexts/DMContext.tsx b/src/contexts/DMContext.tsx index d613bff..0055c6f 100644 --- a/src/contexts/DMContext.tsx +++ b/src/contexts/DMContext.tsx @@ -123,7 +123,7 @@ interface DMContextType { sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise; protocolMode: ProtocolMode; scanProgress: ScanProgressState; - clearCacheAndReload: () => Promise; + clearCacheAndRefetch: () => Promise; } const DMContext = createContext(null); @@ -999,8 +999,8 @@ export function DMProvider({ children, config }: DMProviderProps) { } }, [loadPreviousCachedMessages, queryRelaysForMessagesSince, startNIP4Subscription, startNIP17Subscription, enableNIP17]); - // Clear cache and reload - const clearCacheAndReload = useCallback(async () => { + // Clear cache and refetch from relays + const clearCacheAndRefetch = useCallback(async () => { if (!enabled || !userPubkey) return; try { @@ -1079,9 +1079,42 @@ export function DMProvider({ children, config }: DMProviderProps) { previousRelayUrl.current = appConfig.relayUrl; if (relayChanged && enabled && userPubkey && hasInitialLoadCompleted) { - clearCacheAndReload(); + clearCacheAndRefetch(); } - }, [enabled, userPubkey, appConfig.relayUrl, hasInitialLoadCompleted, clearCacheAndReload]); + }, [enabled, userPubkey, appConfig.relayUrl, hasInitialLoadCompleted, clearCacheAndRefetch]); + + // Detect hard refresh shortcut (Ctrl+Shift+R / Cmd+Shift+R) to clear cache + useEffect(() => { + if (!enabled || !userPubkey) return; + + const handleHardRefresh = (e: KeyboardEvent) => { + if ((e.ctrlKey || e.metaKey) && e.shiftKey && (e.key === 'R' || e.key === 'r')) { + try { + sessionStorage.setItem('dm-clear-cache-on-load', 'true'); + } catch (error) { + console.warn('[DM] SessionStorage unavailable, cache won\'t clear on hard refresh:', error); + } + } + }; + + window.addEventListener('keydown', handleHardRefresh); + return () => window.removeEventListener('keydown', handleHardRefresh); + }, [enabled, userPubkey]); + + // Clear cache after hard refresh + useEffect(() => { + if (!enabled || !userPubkey) return; + + try { + const shouldClearCache = sessionStorage.getItem('dm-clear-cache-on-load'); + if (shouldClearCache) { + sessionStorage.removeItem('dm-clear-cache-on-load'); + clearCacheAndRefetch(); + } + } catch (error) { + console.warn('[DM] Could not check sessionStorage for cache clear flag:', error); + } + }, [enabled, userPubkey, clearCacheAndRefetch]); // Conversations summary const conversations = useMemo(() => { @@ -1239,7 +1272,7 @@ export function DMProvider({ children, config }: DMProviderProps) { protocolMode, scanProgress, subscriptions, - clearCacheAndReload, + clearCacheAndRefetch, }; return (