mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
clear cache properly and without hacky window reload
This commit is contained in:
1 parent
d72fc80cc5
commit
114eb13ce6
3 files changed
+33
-15
No files matched your search
@@ -9,10 +9,10 @@ import { Separator } from '@/components/ui/separator';
|
|||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
|
||||||
interface DMStatusInfoProps {
|
interface DMStatusInfoProps {
|
||||||
clearCache?: () => Promise<void>;
|
clearCacheAndReload?: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
|
export const DMStatusInfo = ({ clearCacheAndReload }: DMStatusInfoProps) => {
|
||||||
const [isClearing, setIsClearing] = useState(false);
|
const [isClearing, setIsClearing] = useState(false);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const {
|
const {
|
||||||
@@ -25,19 +25,16 @@ export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
|
|||||||
} = useDMContext();
|
} = useDMContext();
|
||||||
|
|
||||||
const handleClearCache = async () => {
|
const handleClearCache = async () => {
|
||||||
if (!clearCache) return;
|
if (!clearCacheAndReload) return;
|
||||||
|
|
||||||
setIsClearing(true);
|
setIsClearing(true);
|
||||||
try {
|
try {
|
||||||
await clearCache();
|
await clearCacheAndReload();
|
||||||
toast({
|
toast({
|
||||||
title: 'Cache cleared',
|
title: 'Cache cleared',
|
||||||
description: 'All cached messages have been removed. Reloading...',
|
description: 'Reloading messages from relays...',
|
||||||
});
|
});
|
||||||
// Give user time to see the toast
|
setIsClearing(false);
|
||||||
setTimeout(() => {
|
|
||||||
window.location.reload();
|
|
||||||
}, 1000);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error clearing cache:', error);
|
console.error('Error clearing cache:', error);
|
||||||
toast({
|
toast({
|
||||||
@@ -180,7 +177,7 @@ export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
|
|||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Actions */}
|
{/* Actions */}
|
||||||
{clearCache && (
|
{clearCacheAndReload && (
|
||||||
<>
|
<>
|
||||||
<Separator />
|
<Separator />
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ interface DMContextType {
|
|||||||
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
|
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
|
||||||
isNIP17Enabled: boolean;
|
isNIP17Enabled: boolean;
|
||||||
scanProgress: ScanProgressState;
|
scanProgress: ScanProgressState;
|
||||||
clearCache: () => Promise<void>;
|
clearCacheAndReload: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DMContext = createContext<DMContextType | null>(null);
|
const DMContext = createContext<DMContextType | null>(null);
|
||||||
@@ -1073,12 +1073,33 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
|
|||||||
}
|
}
|
||||||
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
||||||
|
|
||||||
const clearCache = useCallback(async () => {
|
const clearCacheAndReload = useCallback(async () => {
|
||||||
if (!userPubkey) return;
|
if (!userPubkey) return;
|
||||||
|
|
||||||
try {
|
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');
|
const { deleteMessagesFromDB } = await import('@/lib/dmMessageStore');
|
||||||
await deleteMessagesFromDB(userPubkey);
|
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) {
|
} catch (error) {
|
||||||
console.error('[DM] Error clearing cache:', error);
|
console.error('[DM] Error clearing cache:', error);
|
||||||
throw error;
|
throw error;
|
||||||
@@ -1098,7 +1119,7 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
|
|||||||
isNIP17Enabled: enableNIP17,
|
isNIP17Enabled: enableNIP17,
|
||||||
scanProgress,
|
scanProgress,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
clearCache,
|
clearCacheAndReload,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const Messages = () => {
|
|||||||
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 { clearCache } = useDMContext();
|
const { clearCacheAndReload } = useDMContext();
|
||||||
|
|
||||||
useSeoMeta({
|
useSeoMeta({
|
||||||
title: 'Messages',
|
title: 'Messages',
|
||||||
@@ -66,7 +66,7 @@ const Messages = () => {
|
|||||||
View loading status, cache info, and connection details
|
View loading status, cache info, and connection details
|
||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DMStatusInfo clearCache={clearCache} />
|
<DMStatusInfo clearCacheAndReload={clearCacheAndReload} />
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user