import useLocalState from "@/state/state"; import type { Poast } from "@/types/trill"; import Sigil from "@/components/Sigil"; import { useState, useEffect, useRef, type FormEvent } from "react"; import Snippets, { ReplySnippet } from "./Snippets"; import toast from "react-hot-toast"; import Icon from "@/components/Icon"; import { wait } from "@/logic/utils"; function Composer({ isAnon }: { isAnon?: boolean }) { const { api, composerData, addNotification, setComposerData } = useLocalState( (s) => ({ api: s.api, composerData: s.composerData, addNotification: s.addNotification, setComposerData: s.setComposerData, }), ); const our = api!.airlock.our!; const [input, setInput] = useState(""); const [isExpanded, setIsExpanded] = useState(false); const [isLoading, setLoading] = useState(false); const inputRef = useRef(null); useEffect(() => { if (composerData) { setIsExpanded(true); if ( composerData.type === "reply" && composerData.post && "trill" in composerData.post ) { const author = composerData.post.trill.author; setInput(`${author} `); } // Auto-focus input when composer opens setTimeout(() => { inputRef.current?.focus(); }, 100); // Small delay to ensure the composer is rendered } }, [composerData]); async function poast(e: FormEvent) { e.preventDefault(); // TODO // const parent = replying ? replying : null; // const tokens = tokenize(input); // const post: SentPoast = { // host: parent ? parent.host : our, // author: our, // thread: parent ? parent.thread : null, // parent: parent ? parent.id : null, // contents: input, // read: openLock, // write: openLock, // tags: input.match(HASHTAGS_REGEX) || [], // }; // TODO make it user choosable setLoading(true); const res = composerData?.type === "reply" && "trill" in composerData.post ? api!.addReply( input, composerData.post.trill.host, composerData.post.trill.id, composerData.post.trill.thread || composerData.post.trill.id, ) : composerData?.type === "quote" && "trill" in composerData.post ? api!.addQuote(input, { ship: composerData.post.trill.host, id: composerData.post.trill.id, }) : !composerData ? api!.addPost(input) : wait(500); const ares = await res; if (ares) { // // Check for mentions in the post (ship names starting with ~) const mentions = input.match(/~[a-z-]+/g); if (mentions) { mentions.forEach((mention) => { if (mention !== our) { // Don't notify self-mentions addNotification({ type: "mention", from: our, message: `You mentioned ${mention} in a post`, }); } }); } // If this is a reply, add notification if ( composerData?.type === "reply" && composerData.post && "trill" in composerData.post ) { if (composerData.post.trill.author !== our) { addNotification({ type: "reply", from: our, message: `You replied to ${composerData.post.trill.author}'s post`, postId: composerData.post.trill.id, }); } } setInput(""); setComposerData(null); // Clear composer data after successful post toast.success("post sent"); setIsExpanded(false); } } const placeHolder = composerData?.type === "reply" ? "Write your reply..." : composerData?.type === "quote" ? "Add your thoughts..." : isAnon ? "> be me" : "What's going on in Urbit"; const clearComposer = (e: React.MouseEvent) => { e.preventDefault(); setComposerData(null); setInput(""); setIsExpanded(false); }; return (
{/* Reply snippets appear above input */} {composerData && composerData.type === "reply" && (
Replying to
)} {/* Quote context header above input (without snippet) */} {composerData && composerData.type === "quote" && (
Quote posting
)}
setInput(e.currentTarget.value)} onFocus={() => setIsExpanded(true)} placeholder={placeHolder} />
{/* Quote snippets appear below input */} {composerData && composerData.type === "quote" && (
)}
); } export default Composer;