|
| 1 | +import { usePage } from '@inertiajs/react'; |
| 2 | +import { useStream } from '@laravel/stream-react'; |
| 3 | +import { MessageCircle, X } from 'lucide-react'; |
| 4 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 5 | +import { stream } from '@/actions/App/Http/Controllers/ChatController'; |
| 6 | +import { ChatInput } from '@/components/chat/chat-input'; |
| 7 | +import { MessageList } from '@/components/chat/message-list'; |
| 8 | +import { Button } from '@/components/ui/button'; |
| 9 | +import { cn } from '@/lib/utils'; |
| 10 | +import type { ChatMessage, SharedData } from '@/types'; |
| 11 | + |
| 12 | +const CONVERSATION_MARKER_REGEX = /\n?<<conversation:([a-f0-9-]*)>>/g; |
| 13 | +const CONVERSATION_EXTRACT_REGEX = /<<conversation:([a-f0-9-]+)>>/; |
| 14 | + |
| 15 | +export function FloatingChat() { |
| 16 | + const { auth } = usePage<SharedData>().props; |
| 17 | + |
| 18 | + const [open, setOpen] = useState(false); |
| 19 | + const [messages, setMessages] = useState<ChatMessage[]>([]); |
| 20 | + const [streamingContent, setStreamingContent] = useState(''); |
| 21 | + const conversationIdRef = useRef<string | null>(null); |
| 22 | + const accumulatedRef = useRef(''); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 26 | + if (e.key === 'Escape') { |
| 27 | + setOpen(false); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + if (open) { |
| 32 | + document.addEventListener('keydown', handleKeyDown); |
| 33 | + } |
| 34 | + |
| 35 | + return () => document.removeEventListener('keydown', handleKeyDown); |
| 36 | + }, [open]); |
| 37 | + |
| 38 | + const { send, isFetching, isStreaming } = useStream(stream.url(), { |
| 39 | + onData: (chunk: string) => { |
| 40 | + accumulatedRef.current += chunk; |
| 41 | + setStreamingContent(accumulatedRef.current.replace(CONVERSATION_MARKER_REGEX, '')); |
| 42 | + }, |
| 43 | + onFinish: () => { |
| 44 | + const fullContent = accumulatedRef.current; |
| 45 | + const match = fullContent.match(CONVERSATION_EXTRACT_REGEX); |
| 46 | + const cleanContent = fullContent.replace(CONVERSATION_MARKER_REGEX, '').trim(); |
| 47 | + |
| 48 | + if (cleanContent) { |
| 49 | + const assistantMessage: ChatMessage = { |
| 50 | + id: `assistant-${Date.now()}`, |
| 51 | + role: 'assistant', |
| 52 | + content: cleanContent, |
| 53 | + created_at: new Date().toISOString(), |
| 54 | + }; |
| 55 | + |
| 56 | + setMessages((prev) => [...prev, assistantMessage]); |
| 57 | + } |
| 58 | + |
| 59 | + setStreamingContent(''); |
| 60 | + accumulatedRef.current = ''; |
| 61 | + |
| 62 | + if (match) { |
| 63 | + conversationIdRef.current = match[1]; |
| 64 | + } |
| 65 | + }, |
| 66 | + onError: () => { |
| 67 | + setStreamingContent(''); |
| 68 | + accumulatedRef.current = ''; |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const handleSend = useCallback( |
| 73 | + (message: string) => { |
| 74 | + const userMessage: ChatMessage = { |
| 75 | + id: `user-${Date.now()}`, |
| 76 | + role: 'user', |
| 77 | + content: message, |
| 78 | + created_at: new Date().toISOString(), |
| 79 | + }; |
| 80 | + |
| 81 | + setMessages((prev) => [...prev, userMessage]); |
| 82 | + setStreamingContent(''); |
| 83 | + accumulatedRef.current = ''; |
| 84 | + |
| 85 | + send({ |
| 86 | + message, |
| 87 | + conversation_id: conversationIdRef.current, |
| 88 | + }); |
| 89 | + }, |
| 90 | + [send], |
| 91 | + ); |
| 92 | + |
| 93 | + if (!auth.user) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + <Button |
| 100 | + size="icon" |
| 101 | + className="fixed right-6 bottom-6 z-50 h-14 w-14 rounded-full shadow-lg" |
| 102 | + onClick={() => setOpen(true)} |
| 103 | + > |
| 104 | + <MessageCircle className="h-6 w-6" /> |
| 105 | + <span className="sr-only">Open chat</span> |
| 106 | + </Button> |
| 107 | + |
| 108 | + {open && ( |
| 109 | + <div |
| 110 | + className="fixed inset-0 z-50 bg-black/80" |
| 111 | + onClick={() => setOpen(false)} |
| 112 | + /> |
| 113 | + )} |
| 114 | + |
| 115 | + <div |
| 116 | + className={cn( |
| 117 | + 'bg-background fixed inset-y-0 right-0 z-50 flex w-full flex-col border-l shadow-lg transition-transform duration-300 ease-in-out sm:max-w-md', |
| 118 | + open ? 'translate-x-0' : 'translate-x-full', |
| 119 | + )} |
| 120 | + > |
| 121 | + <div className="flex items-center justify-between border-b px-4 py-3"> |
| 122 | + <div> |
| 123 | + <h2 className="text-foreground font-semibold">Chat</h2> |
| 124 | + <p className="text-muted-foreground text-sm">Pregúntale a nuestro asistente de IA cualquier cosa.</p> |
| 125 | + </div> |
| 126 | + <Button variant="ghost" size="icon" onClick={() => setOpen(false)}> |
| 127 | + <X className="h-4 w-4" /> |
| 128 | + <span className="sr-only">Cerrar</span> |
| 129 | + </Button> |
| 130 | + </div> |
| 131 | + <MessageList messages={messages} streamingContent={streamingContent || undefined} /> |
| 132 | + <ChatInput onSend={handleSend} disabled={isFetching || isStreaming} /> |
| 133 | + </div> |
| 134 | + </> |
| 135 | + ); |
| 136 | +} |
0 commit comments