import { useEffect, useRef, useState } from 'react' import { useDroppable } from '@dnd-kit/react' import { useChatbotStore } from '@/ui/stores/chatbot' import { Bot, ArrowUp, History, Plus, Trash2, Loader2, ArrowLeft } from 'lucide-react' import { cn } from '@/ui/lib/utils' import { m } from '@/ui/paraglide/messages.js' import { ScrollArea } from '@/ui/components/ui/scroll-area' import Markdown from 'react-markdown' import type { ChatMessage } from '@shumai/dtos' interface ChatbotSidebarProps { projectId: string contextAssetId?: string } export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProps) { const { isHistoryMode, setIsHistoryMode, currentSessionId, chatAssets, removeAsset, messages, historySessions, isStreaming, fetchHistorySessions, loadSession, deleteSession, startNewSession, sendMessage, } = useChatbotStore() const [inputText, setInputText] = useState('') const messagesEndRef = useRef(null) const { ref: setDroppableRef, isDropTarget: isOver } = useDroppable({ id: 'chatbot-sidebar', data: { type: 'chatbot-sidebar', }, }) // Auto-scroll messages list to bottom useEffect(() => { if (!isHistoryMode) { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } }, [messages, isStreaming, isHistoryMode]) // Fetch history when entering history mode useEffect(() => { if (isHistoryMode) { fetchHistorySessions() } }, [isHistoryMode, fetchHistorySessions]) const handleSend = () => { if (!inputText.trim() || isStreaming) return sendMessage(inputText, projectId, contextAssetId) setInputText('') } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.nativeEvent.isComposing) return if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() handleSend() } } const getMessageText = (content: unknown): string => { if (typeof content === 'string') return content if (Array.isArray(content)) { return content .map((c) => { if (typeof c === 'string') return c if (c && typeof c === 'object' && 'type' in c && c.type === 'text' && 'text' in c) { return String((c as { text: unknown }).text) } return '' }) .join('') } return '' } const preprocessMarkdown = (text: string): string => { return text.replace(/<@([a-zA-Z0-9_-]+)>/g, '@$1').replace(/^\[([^\]]+)\]:/gm, '\\[$1\\]:') } const renderMessage = (msg: ChatMessage) => { const isUser = msg.role === 'user' const isAssistant = msg.role === 'assistant' const isTool = msg.role === 'toolResult' const isSystem = (msg.role as string) === 'thinking_level_change' || msg.role === 'custom' if (isUser) { return (
{getMessageText(msg.content)}
) } if (isAssistant) { return (
{preprocessMarkdown(getMessageText(msg.content))}
{m.created_by_agent()}
) } if (isTool) { return (
{m.tool_result_with_name({ name: (msg as { toolName?: string }).toolName || 'tool' })}
) } if (isSystem) { const systemContent = 'content' in msg ? msg.content : '' return (
{getMessageText(systemContent)}
) } return null } return (
{/* Header section */}
{isHistoryMode ? ( <>
{m.history()}
) : ( <>
{m.shumai_agent()}
)}
{/* Main body view */} {isHistoryMode ? ( {historySessions.length === 0 ? (

{m.no_history_sessions()}

) : (
{historySessions.map((sess) => { const isActive = sess.id === currentSessionId return (
loadSession(sess.id)} className="flex-1 min-w-0 pr-6">
{m.shumai_agent()}
{new Date(sess.createdAt).toLocaleString()}
) })}
)}
) : ( <> {/* Active Chat view */} {messages.length === 0 ? (

{m.chatbot_drag_drop_hint()}

) : (
{messages.map(renderMessage)} {isStreaming && messages.length > 0 && messages[messages.length - 1].role === 'user' && (
Thinking...
)}
)} {/* Input Area (nested inside the border exactly like comment input) */}
{/* Context assets list at the top inside the border */} {chatAssets.length > 0 && (
{chatAssets.map((asset) => (
{asset.name} {asset.type}
))}
)} {/* Textarea inside the border */}