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 [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
</DialogDescription>
</DialogHeader>
<DMStatusInfo clearCacheAndReload={clearCacheAndReload} />
<DMStatusInfo clearCacheAndRefetch={clearCacheAndRefetch} />
</DialogContent>
</Dialog>
+8 -8
View File
@@ -9,10 +9,10 @@ import { Separator } from '@/components/ui/separator';
import { useToast } from '@/hooks/useToast';
interface DMStatusInfoProps {
clearCacheAndReload?: () => Promise<void>;
clearCacheAndRefetch?: () => Promise<void>;
}
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) => {
</Card>
{/* Actions */}
{clearCacheAndReload && (
{clearCacheAndRefetch && (
<>
<Separator />
<div className="space-y-3">
<div className="space-y-1">
<p className="text-sm font-medium">Cache Management</p>
<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>
</div>
<Button
@@ -201,7 +201,7 @@ export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
) : (
<>
<RefreshCw className="mr-2 h-4 w-4" />
Clear Cache & Reload
Clear Cache & Refetch
</>
)}
</Button>
+39 -6
View File
@@ -123,7 +123,7 @@ interface DMContextType {
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
protocolMode: ProtocolMode;
scanProgress: ScanProgressState;
clearCacheAndReload: () => Promise<void>;
clearCacheAndRefetch: () => Promise<void>;
}
const DMContext = createContext<DMContextType | null>(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 (