"use client"; import { useRef, useState, useCallback } from "react"; import { ArrowUp, Loader2, Maximize2, Minimize2 } from "lucide-react"; import { ContentEditor, type ContentEditorRef, useFileDropZone, FileDropOverlay } from "../../editor"; import { FileUploadButton } from "@multica/ui/components/common/file-upload-button"; import { Tooltip, TooltipTrigger, TooltipContent } from "@multica/ui/components/ui/tooltip"; import { ActorAvatar } from "../../common/actor-avatar"; import { useFileUpload } from "@multica/core/hooks/use-file-upload"; import { api } from "@multica/core/api"; import { cn } from "@multica/ui/lib/utils"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- interface ReplyInputProps { issueId: string; placeholder?: string; avatarType: string; avatarId: string; onSubmit: (content: string, attachmentIds?: string[]) => Promise; size?: "sm" | "default"; } // --------------------------------------------------------------------------- // ReplyInput // --------------------------------------------------------------------------- function ReplyInput({ issueId, placeholder = "Leave a reply...", avatarType, avatarId, onSubmit, size = "default", }: ReplyInputProps) { const editorRef = useRef(null); const [isEmpty, setIsEmpty] = useState(true); const [isExpanded, setIsExpanded] = useState(false); const [submitting, setSubmitting] = useState(false); const uploadMapRef = useRef>(new Map()); const { uploadWithToast } = useFileUpload(api); const { isDragOver, dropZoneProps } = useFileDropZone({ onDrop: (files) => files.forEach((f) => editorRef.current?.uploadFile(f)), }); const handleUpload = useCallback(async (file: File) => { const result = await uploadWithToast(file, { issueId }); if (result) { uploadMapRef.current.set(result.link, result.id); } return result; }, [uploadWithToast, issueId]); const handleSubmit = async () => { const content = editorRef.current?.getMarkdown()?.replace(/(\n\s*)+$/, "").trim(); if (!content || submitting) return; // Only send attachment IDs for uploads still present in the content. const activeIds: string[] = []; for (const [url, id] of uploadMapRef.current) { if (content.includes(url)) activeIds.push(id); } setSubmitting(true); try { await onSubmit(content, activeIds.length > 0 ? activeIds : undefined); editorRef.current?.clearContent(); setIsEmpty(true); uploadMapRef.current.clear(); } finally { setSubmitting(false); } }; const avatarSize = size === "sm" ? 22 : 28; return (
setIsEmpty(!md.trim())} onSubmit={handleSubmit} onUploadFile={handleUpload} debounceMs={100} currentIssueId={issueId} />
{ setIsExpanded((v) => !v); editorRef.current?.focus(); }} className="inline-flex h-6 w-6 items-center justify-center rounded-sm text-muted-foreground opacity-70 hover:opacity-100 hover:bg-accent/60 transition-all cursor-pointer" > {isExpanded ? : } } /> {isExpanded ? "Collapse" : "Expand"} editorRef.current?.uploadFile(file)} />
{isDragOver && }
); } export { ReplyInput, type ReplyInputProps };