import { useEffect, useState } from "react"; import { useConversationStore } from "../stores/conversation.ts"; import { useAuthStore } from "../stores/auth.ts"; import { NewConversationModal } from "./NewConversationModal.tsx"; function otherMemberName(conv: { members: { id: string; username: string }[] }, currentUserId: string) { const other = conv.members.find((m) => m.id !== currentUserId); return other?.username || "unknown"; } function isSelfDM(conv: { members: { id: string }[] }, currentUserId: string) { return conv.members.length === 1 && conv.members[0].id === currentUserId; } export function ConversationList() { const conversations = useConversationStore((s) => s.conversations); const activeId = useConversationStore((s) => s.activeConversationId); const fetchConversations = useConversationStore((s) => s.fetchConversations); const setActive = useConversationStore((s) => s.setActiveConversation); const createConversation = useConversationStore((s) => s.createConversation); const currentUser = useAuthStore((s) => s.user); const [showNew, setShowNew] = useState(false); useEffect(() => { fetchConversations(); }, [fetchConversations]); const [notesError, setNotesError] = useState(null); const openNotes = async () => { setNotesError(null); // Find existing self-DM or create one const existing = conversations.find((c) => isSelfDM(c, currentUser?.id || "")); if (existing) { setActive(existing.id); return; } try { await createConversation([]); } catch (err) { console.error('Failed to create notes:', err); setNotesError(err instanceof Error ? err.message : 'Failed to create notes'); } }; return (
[DIRECT MESSAGES]
{conversations.length === 0 && (

[no conversations]

)} {conversations.map((conv) => { const self = isSelfDM(conv, currentUser?.id || ""); const name = self ? "Notes" : conv.type === "group_dm" ? conv.name || conv.members.map((m) => m.username).join(", ") : otherMemberName(conv, currentUser?.id || ""); return ( ); })}
{notesError && (
ERR: {notesError}
)} {showNew && setShowNew(false)} />}
); }