mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
wip chat ui
This commit is contained in:
1 parent
1d42551b36
commit
b132e4847d
6 files changed
+776
No files matched your search
@@ -2,6 +2,7 @@ import { BrowserRouter, Route, Routes } from "react-router-dom";
|
|||||||
import { ScrollToTop } from "./components/ScrollToTop";
|
import { ScrollToTop } from "./components/ScrollToTop";
|
||||||
|
|
||||||
import Index from "./pages/Index";
|
import Index from "./pages/Index";
|
||||||
|
import Messages from "./pages/Messages";
|
||||||
import { NIP19Page } from "./pages/NIP19Page";
|
import { NIP19Page } from "./pages/NIP19Page";
|
||||||
import NotFound from "./pages/NotFound";
|
import NotFound from "./pages/NotFound";
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ export function AppRouter() {
|
|||||||
<ScrollToTop />
|
<ScrollToTop />
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<Index />} />
|
<Route path="/" element={<Index />} />
|
||||||
|
<Route path="/messages" element={<Messages />} />
|
||||||
{/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */}
|
{/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */}
|
||||||
<Route path="/:nip19" element={<NIP19Page />} />
|
<Route path="/:nip19" element={<NIP19Page />} />
|
||||||
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
|
{/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */}
|
||||||
|
|||||||
@@ -0,0 +1,272 @@
|
|||||||
|
import { useState, useRef, useEffect } from 'react';
|
||||||
|
import { useConversationMessages, useDMContext } from '@/contexts/DMContext';
|
||||||
|
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||||
|
import { useAuthor } from '@/hooks/useAuthor';
|
||||||
|
import { genUserName } from '@/lib/genUserName';
|
||||||
|
import { MESSAGE_PROTOCOL } from '@/lib/dmConstants';
|
||||||
|
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Card } from '@/components/ui/card';
|
||||||
|
import { Textarea } from '@/components/ui/textarea';
|
||||||
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||||
|
import { Skeleton } from '@/components/ui/skeleton';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { ArrowLeft, Send, Loader2 } from 'lucide-react';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
interface ChatAreaProps {
|
||||||
|
pubkey: string | null;
|
||||||
|
onBack?: () => void;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function MessageBubble({
|
||||||
|
message,
|
||||||
|
isFromCurrentUser
|
||||||
|
}: {
|
||||||
|
message: {
|
||||||
|
id: string;
|
||||||
|
decryptedContent?: string;
|
||||||
|
error?: string;
|
||||||
|
created_at: number;
|
||||||
|
isSending?: boolean;
|
||||||
|
};
|
||||||
|
isFromCurrentUser: boolean;
|
||||||
|
}) {
|
||||||
|
const formatTime = (timestamp: number) => {
|
||||||
|
const date = new Date(timestamp * 1000);
|
||||||
|
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("flex mb-4", isFromCurrentUser ? "justify-end" : "justify-start")}>
|
||||||
|
<div className={cn(
|
||||||
|
"max-w-[70%] rounded-lg px-4 py-2",
|
||||||
|
isFromCurrentUser
|
||||||
|
? "bg-primary text-primary-foreground"
|
||||||
|
: "bg-muted"
|
||||||
|
)}>
|
||||||
|
{message.error ? (
|
||||||
|
<p className="text-sm italic opacity-70">🔒 Failed to decrypt</p>
|
||||||
|
) : (
|
||||||
|
<p className="text-sm whitespace-pre-wrap break-words">
|
||||||
|
{message.decryptedContent}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<div className="flex items-center gap-2 mt-1">
|
||||||
|
<span className={cn(
|
||||||
|
"text-xs opacity-70",
|
||||||
|
isFromCurrentUser ? "text-primary-foreground" : "text-muted-foreground"
|
||||||
|
)}>
|
||||||
|
{formatTime(message.created_at)}
|
||||||
|
</span>
|
||||||
|
{message.isSending && (
|
||||||
|
<Loader2 className="h-3 w-3 animate-spin opacity-70" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ChatHeader({ pubkey, onBack }: { pubkey: string; onBack?: () => void }) {
|
||||||
|
const author = useAuthor(pubkey);
|
||||||
|
const metadata = author.data?.metadata;
|
||||||
|
|
||||||
|
const displayName = metadata?.name || genUserName(pubkey);
|
||||||
|
const avatarUrl = metadata?.picture;
|
||||||
|
const initials = displayName.slice(0, 2).toUpperCase();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-4 border-b flex items-center gap-3">
|
||||||
|
{onBack && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={onBack}
|
||||||
|
className="md:hidden"
|
||||||
|
>
|
||||||
|
<ArrowLeft className="h-5 w-5" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Avatar className="h-10 w-10">
|
||||||
|
<AvatarImage src={avatarUrl} alt={displayName} />
|
||||||
|
<AvatarFallback>{initials}</AvatarFallback>
|
||||||
|
</Avatar>
|
||||||
|
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<h2 className="font-semibold truncate">{displayName}</h2>
|
||||||
|
{metadata?.nip05 && (
|
||||||
|
<p className="text-xs text-muted-foreground truncate">{metadata.nip05}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function EmptyState() {
|
||||||
|
return (
|
||||||
|
<div className="h-full flex items-center justify-center p-8">
|
||||||
|
<div className="text-center text-muted-foreground max-w-sm">
|
||||||
|
<p className="text-sm">Select a conversation to start messaging</p>
|
||||||
|
<p className="text-xs mt-2">
|
||||||
|
Your messages are encrypted and stored locally
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ChatAreaSkeleton() {
|
||||||
|
return (
|
||||||
|
<div className="h-full flex flex-col">
|
||||||
|
<div className="p-4 border-b flex items-center gap-3">
|
||||||
|
<Skeleton className="h-10 w-10 rounded-full" />
|
||||||
|
<div className="flex-1 space-y-2">
|
||||||
|
<Skeleton className="h-4 w-32" />
|
||||||
|
<Skeleton className="h-3 w-24" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-1 p-4 space-y-4">
|
||||||
|
{[1, 2, 3].map((i) => (
|
||||||
|
<div key={i} className={cn("flex", i % 2 === 0 ? "justify-end" : "justify-start")}>
|
||||||
|
<Skeleton className="h-16 w-64 rounded-lg" />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="p-4 border-t">
|
||||||
|
<Skeleton className="h-20 w-full" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ChatArea({ pubkey, onBack, className }: ChatAreaProps) {
|
||||||
|
const { user } = useCurrentUser();
|
||||||
|
const { sendMessage, isNIP17Enabled } = useDMContext();
|
||||||
|
const { messages, totalCount } = useConversationMessages(pubkey || '');
|
||||||
|
|
||||||
|
const [messageText, setMessageText] = useState('');
|
||||||
|
const [isSending, setIsSending] = useState(false);
|
||||||
|
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
// Auto-scroll to bottom when new messages arrive
|
||||||
|
useEffect(() => {
|
||||||
|
if (scrollAreaRef.current) {
|
||||||
|
const scrollContainer = scrollAreaRef.current.querySelector('[data-radix-scroll-area-viewport]');
|
||||||
|
if (scrollContainer) {
|
||||||
|
scrollContainer.scrollTop = scrollContainer.scrollHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [messages.length]);
|
||||||
|
|
||||||
|
const handleSend = async () => {
|
||||||
|
if (!messageText.trim() || !pubkey || !user) return;
|
||||||
|
|
||||||
|
setIsSending(true);
|
||||||
|
try {
|
||||||
|
await sendMessage({
|
||||||
|
recipientPubkey: pubkey,
|
||||||
|
content: messageText.trim(),
|
||||||
|
protocol: isNIP17Enabled ? MESSAGE_PROTOCOL.NIP17 : MESSAGE_PROTOCOL.NIP04,
|
||||||
|
});
|
||||||
|
setMessageText('');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to send message:', error);
|
||||||
|
} finally {
|
||||||
|
setIsSending(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||||
|
if (e.key === 'Enter' && !e.shiftKey) {
|
||||||
|
e.preventDefault();
|
||||||
|
handleSend();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!pubkey) {
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full", className)}>
|
||||||
|
<EmptyState />
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full flex items-center justify-center", className)}>
|
||||||
|
<div className="text-center text-muted-foreground">
|
||||||
|
<p className="text-sm">Please log in to view messages</p>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full flex flex-col", className)}>
|
||||||
|
<ChatHeader pubkey={pubkey} onBack={onBack} />
|
||||||
|
|
||||||
|
<ScrollArea ref={scrollAreaRef} className="flex-1 p-4">
|
||||||
|
{messages.length === 0 ? (
|
||||||
|
<div className="h-full flex items-center justify-center">
|
||||||
|
<div className="text-center text-muted-foreground">
|
||||||
|
<p className="text-sm">No messages yet</p>
|
||||||
|
<p className="text-xs mt-1">Send a message to start the conversation</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
{messages.map((message) => (
|
||||||
|
<MessageBubble
|
||||||
|
key={message.id}
|
||||||
|
message={message}
|
||||||
|
isFromCurrentUser={message.pubkey === user.pubkey}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</ScrollArea>
|
||||||
|
|
||||||
|
<div className="p-4 border-t">
|
||||||
|
<div className="flex items-center gap-2 mb-2">
|
||||||
|
<Badge variant={isNIP17Enabled ? "default" : "secondary"} className="text-xs">
|
||||||
|
{isNIP17Enabled ? "NIP-17 (Private)" : "NIP-04 (Legacy)"}
|
||||||
|
</Badge>
|
||||||
|
{totalCount > 0 && (
|
||||||
|
<span className="text-xs text-muted-foreground">
|
||||||
|
{totalCount} message{totalCount !== 1 ? 's' : ''}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Textarea
|
||||||
|
value={messageText}
|
||||||
|
onChange={(e) => setMessageText(e.target.value)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
placeholder="Type a message... (Enter to send, Shift+Enter for new line)"
|
||||||
|
className="min-h-[80px] resize-none"
|
||||||
|
disabled={isSending}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
onClick={handleSend}
|
||||||
|
disabled={!messageText.trim() || isSending}
|
||||||
|
size="icon"
|
||||||
|
className="h-[80px] w-[80px]"
|
||||||
|
>
|
||||||
|
{isSending ? (
|
||||||
|
<Loader2 className="h-5 w-5 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<Send className="h-5 w-5" />
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
import { useDMContext } from '@/contexts/DMContext';
|
||||||
|
import { useAuthor } from '@/hooks/useAuthor';
|
||||||
|
import { genUserName } from '@/lib/genUserName';
|
||||||
|
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { Card } from '@/components/ui/card';
|
||||||
|
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||||
|
import { Skeleton } from '@/components/ui/skeleton';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
interface ConversationListProps {
|
||||||
|
selectedPubkey: string | null;
|
||||||
|
onSelectConversation: (pubkey: string) => void;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ConversationItem({
|
||||||
|
pubkey,
|
||||||
|
isSelected,
|
||||||
|
onClick,
|
||||||
|
lastMessage,
|
||||||
|
lastActivity,
|
||||||
|
hasNIP17Messages
|
||||||
|
}: {
|
||||||
|
pubkey: string;
|
||||||
|
isSelected: boolean;
|
||||||
|
onClick: () => void;
|
||||||
|
lastMessage: { decryptedContent?: string; error?: string } | null;
|
||||||
|
lastActivity: number;
|
||||||
|
hasNIP17Messages: boolean;
|
||||||
|
}) {
|
||||||
|
const author = useAuthor(pubkey);
|
||||||
|
const metadata = author.data?.metadata;
|
||||||
|
|
||||||
|
const displayName = metadata?.name || genUserName(pubkey);
|
||||||
|
const avatarUrl = metadata?.picture;
|
||||||
|
const initials = displayName.slice(0, 2).toUpperCase();
|
||||||
|
|
||||||
|
// Format timestamp
|
||||||
|
const formatTime = (timestamp: number) => {
|
||||||
|
const now = Date.now() / 1000;
|
||||||
|
const diff = now - timestamp;
|
||||||
|
|
||||||
|
if (diff < 60) return 'Just now';
|
||||||
|
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
|
||||||
|
if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
|
||||||
|
if (diff < 604800) return `${Math.floor(diff / 86400)}d ago`;
|
||||||
|
return new Date(timestamp * 1000).toLocaleDateString();
|
||||||
|
};
|
||||||
|
|
||||||
|
const lastMessagePreview = lastMessage?.error
|
||||||
|
? '🔒 Encrypted message'
|
||||||
|
: lastMessage?.decryptedContent?.slice(0, 50) || 'No messages yet';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
onClick={onClick}
|
||||||
|
className={cn(
|
||||||
|
"w-full text-left p-3 rounded-lg transition-colors hover:bg-accent",
|
||||||
|
isSelected && "bg-accent"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<div className="flex items-start gap-3">
|
||||||
|
<Avatar className="h-10 w-10 flex-shrink-0">
|
||||||
|
<AvatarImage src={avatarUrl} alt={displayName} />
|
||||||
|
<AvatarFallback>{initials}</AvatarFallback>
|
||||||
|
</Avatar>
|
||||||
|
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="flex items-center justify-between gap-2 mb-1">
|
||||||
|
<span className="font-medium text-sm truncate">{displayName}</span>
|
||||||
|
<span className="text-xs text-muted-foreground flex-shrink-0">
|
||||||
|
{formatTime(lastActivity)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<p className="text-sm text-muted-foreground truncate flex-1">
|
||||||
|
{lastMessagePreview}
|
||||||
|
</p>
|
||||||
|
{hasNIP17Messages && (
|
||||||
|
<Badge variant="secondary" className="text-xs flex-shrink-0">
|
||||||
|
NIP-17
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ConversationListSkeleton() {
|
||||||
|
return (
|
||||||
|
<div className="space-y-2 p-4">
|
||||||
|
{[1, 2, 3, 4, 5].map((i) => (
|
||||||
|
<div key={i} className="flex items-start gap-3 p-3">
|
||||||
|
<Skeleton className="h-10 w-10 rounded-full flex-shrink-0" />
|
||||||
|
<div className="flex-1 space-y-2">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<Skeleton className="h-4 w-24" />
|
||||||
|
<Skeleton className="h-3 w-12" />
|
||||||
|
</div>
|
||||||
|
<Skeleton className="h-3 w-full" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ConversationList({
|
||||||
|
selectedPubkey,
|
||||||
|
onSelectConversation,
|
||||||
|
className
|
||||||
|
}: ConversationListProps) {
|
||||||
|
const { conversations, isLoading } = useDMContext();
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full", className)}>
|
||||||
|
<ConversationListSkeleton />
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conversations.length === 0) {
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full flex items-center justify-center p-8", className)}>
|
||||||
|
<div className="text-center text-muted-foreground">
|
||||||
|
<p className="text-sm">No conversations yet</p>
|
||||||
|
<p className="text-xs mt-1">Start a new conversation to get started</p>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card className={cn("h-full flex flex-col", className)}>
|
||||||
|
<div className="p-4 border-b">
|
||||||
|
<h2 className="font-semibold text-lg">Messages</h2>
|
||||||
|
<p className="text-xs text-muted-foreground mt-1">
|
||||||
|
{conversations.length} conversation{conversations.length !== 1 ? 's' : ''}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ScrollArea className="flex-1">
|
||||||
|
<div className="p-2 space-y-1">
|
||||||
|
{conversations.map((conversation) => (
|
||||||
|
<ConversationItem
|
||||||
|
key={conversation.pubkey}
|
||||||
|
pubkey={conversation.pubkey}
|
||||||
|
isSelected={selectedPubkey === conversation.pubkey}
|
||||||
|
onClick={() => onSelectConversation(conversation.pubkey)}
|
||||||
|
lastMessage={conversation.lastMessage}
|
||||||
|
lastActivity={conversation.lastActivity}
|
||||||
|
hasNIP17Messages={conversation.hasNIP17Messages}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</ScrollArea>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
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 {
|
||||||
|
clearCache?: () => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DMStatusInfo = ({ clearCache }: DMStatusInfoProps) => {
|
||||||
|
const [isClearing, setIsClearing] = useState(false);
|
||||||
|
const { toast } = useToast();
|
||||||
|
const {
|
||||||
|
loadingPhase,
|
||||||
|
subscriptions,
|
||||||
|
scanProgress,
|
||||||
|
isDoingInitialLoad,
|
||||||
|
lastSync,
|
||||||
|
conversations,
|
||||||
|
} = useDMContext();
|
||||||
|
|
||||||
|
const handleClearCache = async () => {
|
||||||
|
if (!clearCache) return;
|
||||||
|
|
||||||
|
setIsClearing(true);
|
||||||
|
try {
|
||||||
|
await clearCache();
|
||||||
|
toast({
|
||||||
|
title: 'Cache cleared',
|
||||||
|
description: 'All cached messages have been removed. Reloading...',
|
||||||
|
});
|
||||||
|
// Give user time to see the toast
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.reload();
|
||||||
|
}, 1000);
|
||||||
|
} 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 (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{/* Loading Phase */}
|
||||||
|
<Card>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<div className="flex items-start gap-3">
|
||||||
|
<PhaseIcon className={`h-5 w-5 ${phaseInfo.color} ${loadingPhase !== LOADING_PHASES.READY ? 'animate-pulse' : ''}`} />
|
||||||
|
<div className="flex-1 space-y-1">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<p className="font-medium">{phaseInfo.label}</p>
|
||||||
|
{isDoingInitialLoad && (
|
||||||
|
<Badge variant="secondary" className="text-xs">
|
||||||
|
Initial Load
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-muted-foreground">{phaseInfo.description}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Scan Progress */}
|
||||||
|
{(scanProgress.nip4 || scanProgress.nip17) && (
|
||||||
|
<Card>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<p className="text-sm font-medium">Scanning Messages</p>
|
||||||
|
{scanProgress.nip4 && (
|
||||||
|
<div className="space-y-1">
|
||||||
|
<div className="flex items-center justify-between text-xs">
|
||||||
|
<span className="text-muted-foreground">NIP-4 (Legacy)</span>
|
||||||
|
<span className="text-muted-foreground">{scanProgress.nip4.current} events</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-muted-foreground">{scanProgress.nip4.status}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{scanProgress.nip17 && (
|
||||||
|
<div className="space-y-1">
|
||||||
|
<div className="flex items-center justify-between text-xs">
|
||||||
|
<span className="text-muted-foreground">NIP-17 (Private)</span>
|
||||||
|
<span className="text-muted-foreground">{scanProgress.nip17.current} events</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-muted-foreground">{scanProgress.nip17.status}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Subscriptions */}
|
||||||
|
<Card>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<p className="text-sm font-medium">Real-time Subscriptions</p>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-sm text-muted-foreground">NIP-4 (Legacy DMs)</span>
|
||||||
|
<Badge variant={subscriptions.nip4 ? 'default' : 'secondary'}>
|
||||||
|
{subscriptions.nip4 ? 'Connected' : 'Disconnected'}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-sm text-muted-foreground">NIP-17 (Private DMs)</span>
|
||||||
|
<Badge variant={subscriptions.nip17 ? 'default' : 'secondary'}>
|
||||||
|
{subscriptions.nip17 ? 'Connected' : 'Disconnected'}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Cache Info */}
|
||||||
|
<Card>
|
||||||
|
<CardContent className="pt-6">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<p className="text-sm font-medium">Cache Information</p>
|
||||||
|
<div className="space-y-2 text-sm">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-muted-foreground">Conversations</span>
|
||||||
|
<span className="font-medium">{conversations.length}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-muted-foreground">Last NIP-4 sync</span>
|
||||||
|
<span className="font-medium">{formatTimestamp(lastSync.nip4)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-muted-foreground">Last NIP-17 sync</span>
|
||||||
|
<span className="font-medium">{formatTimestamp(lastSync.nip17)}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Actions */}
|
||||||
|
{clearCache && (
|
||||||
|
<>
|
||||||
|
<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.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={handleClearCache}
|
||||||
|
disabled={isClearing}
|
||||||
|
variant="outline"
|
||||||
|
className="w-full"
|
||||||
|
>
|
||||||
|
{isClearing ? (
|
||||||
|
<>
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
Clearing...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<RefreshCw className="mr-2 h-4 w-4" />
|
||||||
|
Clear Cache & Reload
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
@@ -122,6 +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>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DMContext = createContext<DMContextType | null>(null);
|
const DMContext = createContext<DMContextType | null>(null);
|
||||||
@@ -1066,6 +1067,18 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
|
|||||||
}
|
}
|
||||||
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
||||||
|
|
||||||
|
const clearCache = useCallback(async () => {
|
||||||
|
if (!userPubkey) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { deleteMessagesFromDB } = await import('@/lib/dmMessageStore');
|
||||||
|
await deleteMessagesFromDB(userPubkey);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[DM] Error clearing cache:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}, [userPubkey]);
|
||||||
|
|
||||||
const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS);
|
const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS);
|
||||||
|
|
||||||
const contextValue: DMContextType = {
|
const contextValue: DMContextType = {
|
||||||
@@ -1079,6 +1092,7 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
|
|||||||
isNIP17Enabled: enableNIP17,
|
isNIP17Enabled: enableNIP17,
|
||||||
scanProgress,
|
scanProgress,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
|
clearCache,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
import { useState } from 'react';
|
||||||
|
import { Info } from 'lucide-react';
|
||||||
|
import { useSeoMeta } from '@unhead/react';
|
||||||
|
import { ConversationList } from '@/components/dm/ConversationList';
|
||||||
|
import { ChatArea } from '@/components/dm/ChatArea';
|
||||||
|
import { DMStatusInfo } from '@/components/dm/DMStatusInfo';
|
||||||
|
import { useDMContext } from '@/contexts/DMContext';
|
||||||
|
import { useIsMobile } from '@/hooks/useIsMobile';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@/components/ui/dialog';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const Messages = () => {
|
||||||
|
const [selectedPubkey, setSelectedPubkey] = useState<string | null>(null);
|
||||||
|
const [statusModalOpen, setStatusModalOpen] = useState(false);
|
||||||
|
const isMobile = useIsMobile();
|
||||||
|
const { clearCache } = useDMContext();
|
||||||
|
|
||||||
|
useSeoMeta({
|
||||||
|
title: 'Messages',
|
||||||
|
description: 'Private encrypted messaging on Nostr',
|
||||||
|
});
|
||||||
|
|
||||||
|
// On mobile, show only one panel at a time
|
||||||
|
const showConversationList = !isMobile || !selectedPubkey;
|
||||||
|
const showChatArea = !isMobile || selectedPubkey;
|
||||||
|
|
||||||
|
const handleSelectConversation = (pubkey: string) => {
|
||||||
|
setSelectedPubkey(pubkey);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleBack = () => {
|
||||||
|
setSelectedPubkey(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-background">
|
||||||
|
<div className="container mx-auto p-4 h-screen flex flex-col">
|
||||||
|
{/* Header with status button */}
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<h1 className="text-2xl font-bold">Messages</h1>
|
||||||
|
|
||||||
|
{/* Status Modal */}
|
||||||
|
<Dialog open={statusModalOpen} onOpenChange={setStatusModalOpen}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="h-9 w-9"
|
||||||
|
aria-label="View messaging status"
|
||||||
|
>
|
||||||
|
<Info className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent className="max-w-md">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Messaging Status</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
View loading status, cache info, and connection details
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DMStatusInfo clearCache={clearCache} />
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-1 flex gap-4 overflow-hidden">
|
||||||
|
{/* Conversation List - Left Sidebar */}
|
||||||
|
<div className={cn(
|
||||||
|
"md:w-80 md:flex-shrink-0",
|
||||||
|
isMobile && !showConversationList && "hidden",
|
||||||
|
isMobile && showConversationList && "w-full"
|
||||||
|
)}>
|
||||||
|
<ConversationList
|
||||||
|
selectedPubkey={selectedPubkey}
|
||||||
|
onSelectConversation={handleSelectConversation}
|
||||||
|
className="h-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Chat Area - Right Panel */}
|
||||||
|
<div className={cn(
|
||||||
|
"flex-1 md:min-w-0",
|
||||||
|
isMobile && !showChatArea && "hidden",
|
||||||
|
isMobile && showChatArea && "w-full"
|
||||||
|
)}>
|
||||||
|
<ChatArea
|
||||||
|
pubkey={selectedPubkey}
|
||||||
|
onBack={isMobile ? handleBack : undefined}
|
||||||
|
className="h-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Messages;
|
||||||
Reference in new issue
Block a user