'use client' import { useState, useEffect, useRef } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { ScrollArea } from '@/components/ui/scroll-area' import { X, Send } from 'lucide-react' interface ChatMessage { id: string from: 'technician' | 'agent' text: string timestamp: Date } interface ChatPanelProps { onClose: () => void sendEvent: (event: Record) => void incomingMessage: Record | null } export function ChatPanel({ onClose, sendEvent, incomingMessage }: ChatPanelProps) { const [messages, setMessages] = useState([]) const [input, setInput] = useState('') const scrollRef = useRef(null) useEffect(() => { if (!incomingMessage) return if (incomingMessage.type === 'chat_message') { setMessages(prev => [...prev, { id: crypto.randomUUID(), from: 'agent', text: incomingMessage.text as string, timestamp: new Date(), }]) } }, [incomingMessage]) useEffect(() => { // Auto-scroll to bottom on new messages if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight } }, [messages]) const send = () => { const text = input.trim() if (!text) return sendEvent({ type: 'chat_message', text }) setMessages(prev => [...prev, { id: crypto.randomUUID(), from: 'technician', text, timestamp: new Date(), }]) setInput('') } const fmt = (d: Date) => d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) return (
Session Chat
{messages.length === 0 && (

Messages sent here are forwarded to the remote machine.

)} {messages.map((m) => (
{m.text}
{fmt(m.timestamp)}
))}
setInput(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') send() }} placeholder="Type a message…" className="h-8 text-xs" />
) }