import { useState } from 'react'; import { RefreshCw, Database, Wifi, CheckCircle2, Loader2 } from 'lucide-react'; import { useDMContext } from '@/contexts/DMContext'; import { LOADING_PHASES } from '@/lib/dmConstants'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Separator } from '@/components/ui/separator'; import { useToast } from '@/hooks/useToast'; interface DMStatusInfoProps { clearCacheAndRefetch?: () => Promise; } export const DMStatusInfo = ({ clearCacheAndRefetch }: DMStatusInfoProps) => { const [isClearing, setIsClearing] = useState(false); const { toast } = useToast(); const { loadingPhase, subscriptions, scanProgress, isDoingInitialLoad, lastSync, conversations, } = useDMContext(); const handleClearCache = async () => { if (!clearCacheAndRefetch) return; setIsClearing(true); try { await clearCacheAndRefetch(); toast({ title: 'Cache cleared', description: 'Refetching messages from relays...', }); setIsClearing(false); } catch (error) { console.error('Error clearing cache:', error); toast({ title: 'Error', description: 'Failed to clear cache. Please try again.', variant: 'destructive', }); setIsClearing(false); } }; const getLoadingPhaseInfo = () => { switch (loadingPhase) { case LOADING_PHASES.IDLE: return { label: 'Idle', description: 'Not yet initialized', icon: Loader2, color: 'text-muted-foreground' }; case LOADING_PHASES.CACHE: return { label: 'Loading from cache', description: 'Reading cached messages...', icon: Database, color: 'text-blue-500' }; case LOADING_PHASES.RELAYS: return { label: 'Loading from relays', description: 'Fetching messages from Nostr relays...', icon: Wifi, color: 'text-yellow-500' }; case LOADING_PHASES.SUBSCRIPTIONS: return { label: 'Connecting subscriptions', description: 'Setting up real-time message sync...', icon: RefreshCw, color: 'text-orange-500' }; case LOADING_PHASES.READY: return { label: 'Ready', description: 'All systems operational', icon: CheckCircle2, color: 'text-green-500' }; default: return { label: 'Unknown', description: 'Status unknown', icon: Loader2, color: 'text-muted-foreground' }; } }; const phaseInfo = getLoadingPhaseInfo(); const PhaseIcon = phaseInfo.icon; const formatTimestamp = (timestamp: number | null) => { if (!timestamp) return 'Never'; const date = new Date(timestamp * 1000); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffMins = Math.floor(diffMs / 60000); if (diffMins < 1) return 'Just now'; if (diffMins < 60) return `${diffMins}m ago`; if (diffMins < 1440) return `${Math.floor(diffMins / 60)}h ago`; return date.toLocaleDateString(); }; return (
{/* Loading Phase */}

{phaseInfo.label}

{isDoingInitialLoad && ( Initial Load )}

{phaseInfo.description}

{/* Scan Progress */} {(scanProgress.nip4 !== null || scanProgress.nip17 !== null) && (

Scanning Messages

{scanProgress.nip4 !== null && (
NIP-4 (Legacy) {scanProgress.nip4.current} events

{scanProgress.nip4.status}

)} {scanProgress.nip17 !== null && (
NIP-17 (Private) {scanProgress.nip17.current} events

{scanProgress.nip17.status}

)}
)} {/* Subscriptions */}

Real-time Subscriptions

NIP-4 (Legacy DMs) {subscriptions.isNIP4Connected ? 'Connected' : 'Disconnected'}
NIP-17 (Private DMs) {subscriptions.isNIP17Connected ? 'Connected' : 'Disconnected'}
{/* Cache Info */}

Cache Information

Conversations {conversations.length}
Last NIP-4 sync {formatTimestamp(lastSync.nip4)}
Last NIP-17 sync {formatTimestamp(lastSync.nip17)}
{/* Actions */} {clearCacheAndRefetch && ( <>

Cache Management

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

)}
); };