import type { CommentInfo, PostAttachmentRequest, PostAttachmentResponse } from '@shumai/dtos' import type { BotInfo } from '@shumai/dtos' import type { UserInfo } from '@shumai/dtos' import { client } from '@/ui/api/client' import { useAnnotationStore } from '@/ui/stores/annotation-store' import type { Annotation } from '@/ui/types' import { useMutation } from '@tanstack/react-query' import { Skeleton } from '../ui/skeleton' import { useMemberStore } from '@/ui/stores/members' import { useTeamContextStore } from '@/ui/stores/team-context' import { ArrowLeft, ArrowUp, Brush, ChevronDown, Clock, FileIcon, Minus, Paperclip, Redo, Square, Undo, XIcon, } from 'lucide-react' import React, { useCallback, useEffect, useRef, useState, type ChangeEvent } from 'react' import { Button } from '../ui/button' import { DrawAnnotation } from '../ui/icons' import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover' import { ProgressCircle } from '../ui/progress-circle' import { formatTimestamp } from '../viewers/utils' type UploadingFile = { id: string // A unique ID for the file, e.g., timestamp + name file: File progress: number attachmentId?: string error?: string } interface ChatInputProps { projectId: string onSendMessage: ( text: string, attachmentIds: string[], annotations?: Annotation[], replyToId?: string | null, second?: number | null, ) => void replyingTo?: CommentInfo | null onCancelReply?: () => void bots?: BotInfo[] initialText?: string hideAnnotationControl?: boolean disableMentions?: boolean currentTime?: number frameRate?: number onTyping?: () => void } const PREDEFINED_COLORS = [ '#ef4444', // Red '#f97316', // Orange '#eab308', // Yellow '#22c55e', // Green '#3b82f6', // Blue '#a855f7', // Purple '#ec4899', // Pink '#000000', // Black '#ffffff', // White ] type MentionEntity = { type: 'user'; data: UserInfo } | { type: 'bot'; data: BotInfo } export const ChatInput = React.forwardRef( ( { projectId, onSendMessage, replyingTo, onCancelReply, bots = [], initialText = '', hideAnnotationControl = false, disableMentions = false, currentTime, frameRate, onTyping, }, ref, ) => { const [uploadingFiles, setUploadingFiles] = useState([]) const [viewingFile, setViewingFile] = useState(null) const [isTimestampEnabled, setIsTimestampEnabled] = useState(true) const { teamId, ensureTeamIdForProject } = useTeamContextStore() const { members: storeMembers, loading: membersLoading, fetchMembers } = useMemberStore() useEffect(() => { if (projectId && !teamId) { ensureTeamIdForProject(projectId) } }, [projectId, teamId, ensureTeamIdForProject]) // Annotation Store const { isDrawing, setIsDrawing, currentTool, setTool, currentColor, setColor, undo, redo, history, historyIndex, annotations, reset: resetAnnotations, } = useAnnotationStore() const [showMentionList, setShowMentionList] = useState(false) const [mentionQuery, setMentionQuery] = useState('') const [highlightedIndex, setHighlightedIndex] = useState(0) const [collapsedSections, setCollapsedSections] = useState({ bots: false, users: false }) const [hasContent, setHasContent] = useState(false) const editorRef = useRef(null) const fileInputRef = useRef(null) const mentionRangeRef = useRef(null) const { mutateAsync: uploadAttachment } = useMutation({ mutationFn: async ({ projectId, data, }: { projectId: string data: PostAttachmentRequest }) => { const res = await client.api.projects[':projectId'].attachments.$post({ param: { projectId: projectId }, json: data, }) if (!res.ok) throw new Error('Failed to create attachment') return (await res.json()) as PostAttachmentResponse }, }) useEffect(() => { if (initialText && editorRef.current) { editorRef.current.innerText = initialText setHasContent(!!initialText.trim()) // Focus and move cursor to end editorRef.current.focus() const range = document.createRange() const sel = window.getSelection() range.selectNodeContents(editorRef.current) range.collapse(false) sel?.removeAllRanges() sel?.addRange(range) } }, [initialText]) // Focus editor when replyingTo becomes truthy useEffect(() => { if (replyingTo && editorRef.current) { editorRef.current.focus() // Focus and move cursor to end const range = document.createRange() const sel = window.getSelection() range.selectNodeContents(editorRef.current) range.collapse(false) sel?.removeAllRanges() sel?.addRange(range) } }, [replyingTo]) // Clean up drawing mode on unmount useEffect(() => { return () => { setIsDrawing(false) } }, [setIsDrawing]) const filteredAgents = [ ...storeMembers .filter( (u) => u.type === 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()), ) .map((u) => ({ type: 'user' as const, data: u })), ...bots .filter((b) => b.name?.toLowerCase().startsWith(mentionQuery.toLowerCase())) .map((b) => ({ type: 'bot' as const, data: b })), ] const filteredHumans = storeMembers .filter( (u) => u.type !== 'agent' && u.name?.toLowerCase().startsWith(mentionQuery.toLowerCase()), ) .map((u) => ({ type: 'user' as const, data: u })) const filteredEntities: MentionEntity[] = [ ...(collapsedSections.bots ? [] : filteredAgents), ...(collapsedSections.users ? [] : filteredHumans), ] useEffect(() => { setHighlightedIndex(0) }, [mentionQuery, showMentionList, collapsedSections]) const handleInput = () => { if (!editorRef.current) return setHasContent(!!editorRef.current.innerText.trim()) onTyping?.() if (disableMentions) { setShowMentionList(false) mentionRangeRef.current = null return } const sel = window.getSelection() if (!sel || sel.rangeCount === 0) return const range = sel.getRangeAt(0) const node = range.startContainer const offset = range.startOffset if (node.nodeType === Node.TEXT_NODE) { const textContent = node.textContent || '' const textBeforeCursor = textContent.slice(0, offset) const lastAtPos = textBeforeCursor.lastIndexOf('@') if (lastAtPos !== -1) { const isStartOfLine = lastAtPos === 0 const charBeforeAt = textBeforeCursor[lastAtPos - 1] const hasSpaceBefore = isStartOfLine || /\s/.test(charBeforeAt) if (hasSpaceBefore) { const query = textBeforeCursor.slice(lastAtPos + 1) if (!/\s/.test(query)) { setMentionQuery(query) setShowMentionList(true) if (teamId) { fetchMembers(teamId, true, true) } // Save the range for later insertion const mentionRange = document.createRange() mentionRange.setStart(node, lastAtPos) mentionRange.setEnd(node, offset) mentionRangeRef.current = mentionRange return } } } } setShowMentionList(false) mentionRangeRef.current = null } const handleSelectEntity = (entity: MentionEntity) => { if (!mentionRangeRef.current || !editorRef.current) return const range = mentionRangeRef.current range.deleteContents() const mentionNode = document.createElement('span') mentionNode.contentEditable = 'false' mentionNode.dataset.type = 'mention' mentionNode.dataset.id = entity.data.id mentionNode.className = 'inline-block bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300 px-1.5 py-0.5 rounded-md text-sm font-medium align-middle select-none mx-0.5' mentionNode.innerText = `@${entity.data.name}` range.insertNode(mentionNode) // Insert a space after the mention const spaceNode = document.createTextNode('\u00A0') mentionNode.after(spaceNode) // Move cursor after the space const newRange = document.createRange() const sel = window.getSelection() newRange.setStartAfter(spaceNode) newRange.collapse(true) sel?.removeAllRanges() sel?.addRange(newRange) setShowMentionList(false) mentionRangeRef.current = null setHasContent(true) editorRef.current.focus() } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.nativeEvent.isComposing) return if (showMentionList && filteredEntities.length > 0) { if (e.key === 'ArrowUp') { e.preventDefault() setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredEntities.length - 1)) return } if (e.key === 'ArrowDown') { e.preventDefault() setHighlightedIndex((prev) => (prev < filteredEntities.length - 1 ? prev + 1 : 0)) return } if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault() handleSelectEntity(filteredEntities[highlightedIndex]) return } if (e.key === 'Escape') { e.preventDefault() setShowMentionList(false) return } } if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleSend() } } const handleFileSelect = useCallback( async (e: ChangeEvent) => { if (!e.target.files) return const newFiles = Array.from(e.target.files) for (const file of newFiles) { const tempId = `${Date.now()}-${file.name}` const newUploadingFile: UploadingFile = { id: tempId, file, progress: 0, } setUploadingFiles((prev) => [...prev, newUploadingFile]) try { const data: PostAttachmentResponse = await uploadAttachment({ projectId, data: { fileName: file.name, contentType: file.type, size: file.size, }, }) if (data.uploadUrl && data.id) { await fetch(data.uploadUrl, { method: 'PUT', body: file, headers: { 'Content-Type': file.type, }, }) setUploadingFiles((prev) => prev.map((uf) => uf.id === tempId ? { ...uf, progress: 100, attachmentId: data.id } : uf, ), ) } } catch (error) { setUploadingFiles((prev) => prev.map((uf) => uf.id === tempId ? { ...uf, error: error instanceof Error ? error.message : 'Upload failed', } : uf, ), ) } } if (fileInputRef.current) { fileInputRef.current.value = '' } }, [projectId, uploadAttachment], ) const removeFile = (id: string) => { setUploadingFiles((prev) => prev.filter((f) => f.id !== id)) } const handleSend = () => { if (!editorRef.current) return const isUploading = uploadingFiles.some((f) => !f.attachmentId && !f.error) if (isUploading) return const successfulAttachmentIds = uploadingFiles .map((f) => f.attachmentId) .filter((id): id is string => !!id) // Serialize content let processedText = '' editorRef.current.childNodes.forEach((node) => { if (node.nodeType === Node.TEXT_NODE) { processedText += node.textContent } else if (node.nodeType === Node.ELEMENT_NODE) { const el = node as HTMLElement if (el.dataset.type === 'mention') { processedText += `<@${el.dataset.id}>` } else if (el.tagName === 'BR') { processedText += '\n' } else if (el.tagName === 'DIV' || el.tagName === 'P') { // contentEditable often uses div/p for new lines processedText += '\n' + el.innerText } else { processedText += el.innerText } } }) processedText = processedText.trim() // Allow sending if annotations exist, even if text is empty if (!processedText && successfulAttachmentIds.length === 0 && annotations.length === 0) return onSendMessage( processedText, successfulAttachmentIds, annotations, replyingTo?.id, isTimestampEnabled ? currentTime : undefined, ) // Reset editor editorRef.current.innerHTML = '' setHasContent(false) setUploadingFiles([]) setViewingFile(null) setIsDrawing(false) resetAnnotations() setShowMentionList(false) if (onCancelReply) onCancelReply() } const isUploading = uploadingFiles.some((f) => !f.attachmentId && !f.error) const hasSuccessfulUploads = uploadingFiles.some((f) => !!f.attachmentId) const canSend = hasContent || hasSuccessfulUploads || annotations.length > 0 const showUpperPart = uploadingFiles.length > 0 || !!replyingTo return (
{showMentionList && (membersLoading || filteredAgents.length > 0 || filteredHumans.length > 0) && (
{membersLoading ? (
{/* Agents Skeleton */}
{/* Members Skeleton */}
) : ( <> {filteredAgents.length > 0 && ( <> {!collapsedSections.bots && filteredAgents.map((item, index) => { const actualIndex = index return ( ) })} )} {filteredHumans.length > 0 && ( <> {!collapsedSections.users && filteredHumans.map((item, index) => { const actualIndex = (collapsedSections.bots ? 0 : filteredAgents.length) + index return ( ) })} )} )}
)} {showUpperPart && (
{replyingTo && (
reply to: {replyingTo.message}
)} {uploadingFiles.length > 0 && (
{uploadingFiles.map((upload) => (
{upload.file.type.startsWith('image/') ? ( preview setViewingFile(upload.file)} className={`w-full h-full object-cover ${ !upload.attachmentId ? 'opacity-50' : '' }`} /> ) : ( )} {isUploading && !upload.attachmentId && !upload.error && (
)}
{upload.file.name}
))}
)}
)}
editorRef.current?.focus()} className="w-full px-4 pt-1 pb-1 cursor-text flow-root max-h-60 overflow-y-auto" > {isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && (
{ e.stopPropagation() editorRef.current?.focus() }} className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-0.5 mt-3 mr-2 rounded-sm text-xs font-mono font-bold flex items-center gap-1 cursor-text" > {formatTimestamp(currentTime, frameRate)}
)}
{isDrawing ? (
{PREDEFINED_COLORS.map((c) => (
) : (
{currentTime !== undefined && frameRate !== undefined && ( )} {!hideAnnotationControl && ( )}
)}
{viewingFile && (
setViewingFile(null)} >
Enlarged view { e.stopPropagation() setViewingFile(null) }} />
{viewingFile.name}
)}
) }, ) ChatInput.displayName = 'ChatInput'