"use client"; import { useEffect, useState } from "react"; import { Image as ImageIcon, MessageCircle, Newspaper, Send, ThumbsUp, Video } from "lucide-react"; import { Avatar } from "./Avatar"; import { currentUser, posts as seedPosts } from "@/lib/demo-data"; import { useCurrentUser } from "@/lib/client-session"; type FeedPost = typeof seedPosts[number]; export function FeedComposer({ onPost }: { onPost: (content: string, id: string) => void }) { const sessionUser = useCurrentUser(); const [open, setOpen] = useState(false); const [content, setContent] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(""); async function submit() { const value = content.trim(); if (!value || submitting) return; setSubmitting(true); setError(""); try { const response = await fetch("/api/posts", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ content: value }) }); const result = await response.json().catch(() => ({})) as { post?: { id?: string }, error?: string }; if (!response.ok || !result.post?.id) throw new Error(result.error ?? "Unable to publish this post."); onPost(value, result.post.id); setContent(""); setOpen(false); } catch (caught) { setError(caught instanceof Error ? caught.message : "Unable to publish this post."); } finally { setSubmitting(false); } } return (
{open && (