intercept hard refresh to clear cache

This commit is contained in:
sam committed 2025-10-20 15:16:51 +07:00
1 parent ed80d74824
commit 8bbb247ba8
3 files changed
+49 -16

No files matched your search

+2 -2
View File
@@ -21,7 +21,7 @@ export const DMMessagingInterface = ({ className }: DMMessagingInterfaceProps) =
const [selectedPubkey, setSelectedPubkey] = useState<string | null>(null); const [selectedPubkey, setSelectedPubkey] = useState<string | null>(null);
const [statusModalOpen, setStatusModalOpen] = useState(false); const [statusModalOpen, setStatusModalOpen] = useState(false);
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const { clearCacheAndReload } = useDMContext(); const { clearCacheAndRefetch } = useDMContext();
// On mobile, show only one panel at a time // On mobile, show only one panel at a time
const showConversationList = !isMobile || !selectedPubkey; const showConversationList = !isMobile || !selectedPubkey;
@@ -46,7 +46,7 @@ export const DMMessagingInterface = ({ className }: DMMessagingInterfaceProps) =
View loading status, cache info, and connection details View loading status, cache info, and connection details
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<DMStatusInfo clearCacheAndReload={clearCacheAndReload} /> <DMStatusInfo clearCacheAndRefetch={clearCacheAndRefetch} />
</DialogContent> </DialogContent>
</Dialog> </Dialog>
+8 -8
View File
@@ -9,10 +9,10 @@ import { Separator } from '@/components/ui/separator';
import { useToast } from '@/hooks/useToast'; import { useToast } from '@/hooks/useToast';
interface DMStatusInfoProps { interface DMStatusInfoProps {
clearCacheAndReload?: () => Promise<void>; clearCacheAndRefetch?: () => Promise<void>;
} }
export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => { export const DMStatusInfo = ({ clearCacheAndRefetch }: DMStatusInfoProps) => {
const [isClearing, setIsClearing] = useState(false); const [isClearing, setIsClearing] = useState(false);
const { toast } = useToast(); const { toast } = useToast();
const { const {
@@ -25,14 +25,14 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
} = useDMContext(); } = useDMContext();
const handleClearCache = async () => { const handleClearCache = async () => {
if (!clearCacheAndReload) return; if (!clearCacheAndRefetch) return;
setIsClearing(true); setIsClearing(true);
try { try {
await clearCacheAndReload(); await clearCacheAndRefetch();
toast({ toast({
title: 'Cache cleared', title: 'Cache cleared',
description: 'Reloading messages from relays...', description: 'Refetching messages from relays...',
}); });
setIsClearing(false); setIsClearing(false);
} catch (error) { } catch (error) {
@@ -177,14 +177,14 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
</Card> </Card>
{/* Actions */} {/* Actions */}
{clearCacheAndReload && ( {clearCacheAndRefetch && (
<> <>
<Separator /> <Separator />
<div className="space-y-3"> <div className="space-y-3">
<div className="space-y-1"> <div className="space-y-1">
<p className="text-sm font-medium">Cache Management</p> <p className="text-sm font-medium">Cache Management</p>
<p className="text-xs text-muted-foreground"> <p className="text-xs text-muted-foreground">
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.
</p> </p>
</div> </div>
<Button <Button
@@ -201,7 +201,7 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
) : ( ) : (
<> <>
<RefreshCw className="mr-2 h-4 w-4" /> <RefreshCw className="mr-2 h-4 w-4" />
Clear Cache & Reload Clear Cache & Refetch
</> </>
)} )}
</Button> </Button>
+39 -6
View File
@@ -123,7 +123,7 @@ interface DMContextType {
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>; sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
protocolMode: ProtocolMode; protocolMode: ProtocolMode;
scanProgress: ScanProgressState; scanProgress: ScanProgressState;
clearCacheAndReload: () => Promise<void>; clearCacheAndRefetch: () => Promise<void>;
} }
const DMContext = createContext<DMContextType | null>(null); const DMContext = createContext<DMContextType | null>(null);
@@ -999,8 +999,8 @@ export function DMProvider({ children, config }: DMProviderProps) {
} }
}, [loadPreviousCachedMessages, queryRelaysForMessagesSince, startNIP4Subscription, startNIP17Subscription, enableNIP17]); }, [loadPreviousCachedMessages, queryRelaysForMessagesSince, startNIP4Subscription, startNIP17Subscription, enableNIP17]);
// Clear cache and reload // Clear cache and refetch from relays
const clearCacheAndReload = useCallback(async () => { const clearCacheAndRefetch = useCallback(async () => {
if (!enabled || !userPubkey) return; if (!enabled || !userPubkey) return;
try { try {
@@ -1079,9 +1079,42 @@ export function DMProvider({ children, config }: DMProviderProps) {
previousRelayUrl.current = appConfig.relayUrl; previousRelayUrl.current = appConfig.relayUrl;
if (relayChanged && enabled && userPubkey && hasInitialLoadCompleted) { 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 // Conversations summary
const conversations = useMemo(() => { const conversations = useMemo(() => {
@@ -1239,7 +1272,7 @@ export function DMProvider({ children, config }: DMProviderProps) {
protocolMode, protocolMode,
scanProgress, scanProgress,
subscriptions, subscriptions,
clearCacheAndReload, clearCacheAndRefetch,
}; };
return ( return (