From b132e4847da5e0ee2dcd5392cb2c16443d228476 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 18 Oct 2025 18:19:47 +0700 Subject: [PATCH] wip chat ui --- src/AppRouter.tsx | 2 + src/components/dm/ChatArea.tsx | 272 +++++++++++++++++++++++++ src/components/dm/ConversationList.tsx | 165 +++++++++++++++ src/components/dm/DMStatusInfo.tsx | 217 ++++++++++++++++++++ src/contexts/DMContext.tsx | 14 ++ src/pages/Messages.tsx | 106 ++++++++++ 6 files changed, 776 insertions(+) create mode 100644 src/components/dm/ChatArea.tsx create mode 100644 src/components/dm/ConversationList.tsx create mode 100644 src/components/dm/DMStatusInfo.tsx create mode 100644 src/pages/Messages.tsx diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 2db53ee..c6532c2 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -2,6 +2,7 @@ import { BrowserRouter, Route, Routes } from "react-router-dom"; import { ScrollToTop } from "./components/ScrollToTop"; import Index from "./pages/Index"; +import Messages from "./pages/Messages"; import { NIP19Page } from "./pages/NIP19Page"; import NotFound from "./pages/NotFound"; @@ -11,6 +12,7 @@ export function AppRouter() { } /> + } /> {/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */} } /> {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */} diff --git a/src/components/dm/ChatArea.tsx b/src/components/dm/ChatArea.tsx new file mode 100644 index 0000000..afc1323 --- /dev/null +++ b/src/components/dm/ChatArea.tsx @@ -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 ( +
+
+ {message.error ? ( +

🔒 Failed to decrypt

+ ) : ( +

+ {message.decryptedContent} +

+ )} +
+ + {formatTime(message.created_at)} + + {message.isSending && ( + + )} +
+
+
+ ); +} + +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 ( +
+ {onBack && ( + + )} + + + + {initials} + + +
+

{displayName}

+ {metadata?.nip05 && ( +

{metadata.nip05}

+ )} +
+
+ ); +} + +function EmptyState() { + return ( +
+
+

Select a conversation to start messaging

+

+ Your messages are encrypted and stored locally +

+
+
+ ); +} + +function ChatAreaSkeleton() { + return ( +
+
+ +
+ + +
+
+ +
+ {[1, 2, 3].map((i) => ( +
+ +
+ ))} +
+ +
+ +
+
+ ); +} + +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(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) => { + if (e.key === 'Enter' && !e.shiftKey) { + e.preventDefault(); + handleSend(); + } + }; + + if (!pubkey) { + return ( + + + + ); + } + + if (!user) { + return ( + +
+

Please log in to view messages

+
+
+ ); + } + + return ( + + + + + {messages.length === 0 ? ( +
+
+

No messages yet

+

Send a message to start the conversation

+
+
+ ) : ( +
+ {messages.map((message) => ( + + ))} +
+ )} +
+ +
+
+ + {isNIP17Enabled ? "NIP-17 (Private)" : "NIP-04 (Legacy)"} + + {totalCount > 0 && ( + + {totalCount} message{totalCount !== 1 ? 's' : ''} + + )} +
+ +
+