import { Loader2, Paperclip, Send, X } from "lucide-react"; import React from "react"; import { FileIcon } from "~/components/file-icon"; // Adjust path import { sendMessage } from "~/lib/api/client/channel"; // Adjust path import { uploadFiles } from "~/lib/api/client/file"; // Adjust path import type { Uuid } from "~/lib/api/types"; // Adjust path import { cn, formatFileSize } from "~/lib/utils"; // Adjust path import TextBox from "./custom-ui/text-box"; // Adjust path, assuming TextBox is in ./custom-ui/ import { Button } from "./ui/button"; // Adjust path import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip"; // Adjust path export interface MessageBoxProps { channelId: string; } export default function MessageBox({ channelId }: MessageBoxProps) { const [text, setText] = React.useState(""); const [attachments, setAttachments] = React.useState([]); const [isLoading, setIsLoading] = React.useState(false); const fileInputRef = React.useRef(null); const textBoxRef = React.useRef(null); const handleSendMessage = async () => { const content = text.trim(); if ((!content && attachments.length === 0) || isLoading) return; setIsLoading(true); try { let uploadedAttachments: Uuid[] = []; if (attachments.length > 0) { uploadedAttachments = await uploadFiles(attachments); } await sendMessage(channelId, text, uploadedAttachments); setText(""); setAttachments([]); setTimeout(() => textBoxRef.current?.focus(), 0); } catch (error) { console.error("Failed to send message:", error); } finally { setIsLoading(false); } }; const addAttachment = () => { if (attachments.length >= 10 || isLoading) return; fileInputRef.current?.click(); }; const onFileChange = (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); if (files.length === 0) return; setAttachments((prev) => { const newAttachments = [...prev, ...files]; return newAttachments.slice(0, 10); }); if (fileInputRef.current) { fileInputRef.current.value = ""; } setTimeout(() => textBoxRef.current?.focus(), 0); }; const removeAttachment = (indexToRemove: number) => { setAttachments((prev) => prev.filter((_, i) => i !== indexToRemove)); setTimeout(() => textBoxRef.current?.focus(), 0); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleSendMessage(); } }; React.useEffect(() => { setText(""); setAttachments([]); setIsLoading(false); setTimeout(() => textBoxRef.current?.focus(), 0); }, [channelId]); const canSend = (text.trim().length > 0 || attachments.length > 0) && !isLoading; const attachmentsRemaining = 10 - attachments.length; return (
{attachments.length > 0 && (
{attachments.map((file, i) => (

{file.name}

{formatFileSize(file.size)}

Remove attachment

))}
)}
{attachments.length >= 10 ? (

Maximum 10 attachments

) : (

Add attachment ({attachmentsRemaining} remaining)

)}
); }