clear cache properly and without hacky window reload

This commit is contained in:
sam committed 2025-10-18 18:54:48 +07:00
1 parent d72fc80cc5
commit 114eb13ce6
3 files changed
+33 -15

No files matched your search

+7 -10
View File
@@ -9,10 +9,10 @@ import { Separator } from '@/components/ui/separator';
import { useToast } from '@/hooks/useToast';
interface DMStatusInfoProps {
clearCache?: () => Promise<void>;
clearCacheAndReload?: () => Promise<void>;
}
export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
const [isClearing, setIsClearing] = useState(false);
const { toast } = useToast();
const {
@@ -25,19 +25,16 @@ export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
} = useDMContext();
const handleClearCache = async () => {
if (!clearCache) return;
if (!clearCacheAndReload) return;
setIsClearing(true);
try {
await clearCache();
await clearCacheAndReload();
toast({
title: 'Cache cleared',
description: 'All cached messages have been removed. Reloading...',
description: 'Reloading messages from relays...',
});
// Give user time to see the toast
setTimeout(() => {
window.location.reload();
}, 1000);
setIsClearing(false);
} catch (error) {
console.error('Error clearing cache:', error);
toast({
@@ -180,7 +177,7 @@ export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
</Card>
{/* Actions */}
{clearCache && (
{clearCacheAndReload && (
<>
<Separator />
<div className="space-y-3">
+24 -3
View File
@@ -122,7 +122,7 @@ interface DMContextType {
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
isNIP17Enabled: boolean;
scanProgress: ScanProgressState;
clearCache: () => Promise<void>;
clearCacheAndReload: () => Promise<void>;
}
const DMContext = createContext<DMContextType | null>(null);
@@ -1073,12 +1073,33 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
}
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
const clearCache = useCallback(async () => {
const clearCacheAndReload = useCallback(async () => {
if (!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;
@@ -1098,7 +1119,7 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
isNIP17Enabled: enableNIP17,
scanProgress,
subscriptions,
clearCache,
clearCacheAndReload,
};
return (
+2 -2
View File
@@ -21,7 +21,7 @@ const Messages = () => {
const [selectedPubkey, setSelectedPubkey] = useState<string | null>(null);
const [statusModalOpen, setStatusModalOpen] = useState(false);
const isMobile = useIsMobile();
const { clearCache } = useDMContext();
const { clearCacheAndReload } = useDMContext();
useSeoMeta({
title: 'Messages',
@@ -66,7 +66,7 @@ const Messages = () => {
View loading status, cache info, and connection details
</DialogDescription>
</DialogHeader>
<DMStatusInfo clearCache={clearCache} />
<DMStatusInfo clearCacheAndReload={clearCacheAndReload} />
</DialogContent>
</Dialog>
</div>