"use client"; import { useState } from "react"; import { MoreHorizontal, Search, Send } from "lucide-react"; import { Avatar } from "./Avatar"; import { people } from "@/lib/demo-data"; type Message = { mine: boolean; body: string; time: string }; type MessageFilter = "focused" | "unread" | "jobs" | "connections" | "inmail" | "starred"; const messageFilters: Array<{ value: MessageFilter; label: string }> = [ { value: "focused", label: "Focused" }, { value: "unread", label: "Unread" }, { value: "jobs", label: "Jobs" }, { value: "connections", label: "Connections" }, { value: "inmail", label: "InMail" }, { value: "starred", label: "Starred" }, ]; const mayaThread: Message[] = [ { mine: false, body: "Hey Maya, your post about design reviews landed with our team.", time: "9:42 AM" }, { mine: true, body: "I’m glad it was useful. We changed the format after a lot of trial and error.", time: "9:48 AM" }, { mine: false, body: "Would you be open to sharing how you structure the pre-read?", time: "9:50 AM" }, ]; const initialMessagesByPerson = people.reduce>((acc, person, index) => { acc[person.id] = index === 0 ? mayaThread : [{ mine: false, body: "Thanks for connecting. Looking forward to staying in touch.", time: "Mon" }]; return acc; }, {}); export function MessagingView() { const [messagesByPerson, setMessagesByPerson] = useState(initialMessagesByPerson); const [draft, setDraft] = useState(""); const [active, setActive] = useState(people[0]); const [searchQuery, setSearchQuery] = useState(""); const [sending, setSending] = useState(false); const [error, setError] = useState(""); const [activeFilter, setActiveFilter] = useState("focused"); const activeMessages = messagesByPerson[active.id] ?? []; const filteredConversations = people.filter((person, index) => { const latestMessage = messagesByPerson[person.id]?.at(-1)?.body ?? ""; const matchesSearch = `${person.name} ${person.headline} ${latestMessage}`.toLowerCase().includes(searchQuery.trim().toLowerCase()); const matchesFilter = activeFilter === "focused" || activeFilter === "connections" || (activeFilter === "unread" && index === 1) || (activeFilter === "starred" && index === 0); return matchesSearch && matchesFilter; }); async function send() { const body = draft.trim(); if (!body || sending) return; setSending(true); setError(""); try { const response = await fetch("/api/messages", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ recipientId: active.id, body }) }); const result = await response.json().catch(() => ({})) as { message?: { id?: string }, error?: string }; if (!response.ok || !result.message?.id) throw new Error(result.error ?? "Unable to send this message."); setMessagesByPerson((items) => ({ ...items, [active.id]: [...(items[active.id] ?? []), { mine: true, body, time: "Now" }], })); setDraft(""); } catch (caught) { setError(caught instanceof Error ? caught.message : "Unable to send this message."); } finally { setSending(false); } } return (
{active.name}{active.headline}

{active.name}

{active.headline}

{activeMessages.map((message, index) => (
{!message.mine && }

{message.body}

))}
{error &&
{error}
}