2026-07-28 12:17:58 +07:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { Bot, MessageCircle, Minimize2 } from "lucide-react";
|
2026-08-01 22:09:02 +07:00
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
2026-07-28 12:17:58 +07:00
|
|
|
import { ChatPanel } from "./chat-panel";
|
2026-08-01 22:09:02 +07:00
|
|
|
import { ChatbotCanvas } from "./chatbot-canvas";
|
|
|
|
|
import { useChatbot } from "./chatbot-context";
|
2026-07-28 12:17:58 +07:00
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
export function ChatbotContainer() {
|
|
|
|
|
const { minimized, setMinimized, chatOpen, setChatOpen } = useChatbot();
|
2026-07-28 12:17:58 +07:00
|
|
|
const [position, setPosition] = useState({ x: 0, y: 0 });
|
|
|
|
|
const [dragging, setDragging] = useState(false);
|
|
|
|
|
const [dragStart, setDragStart] = useState({ x: 0, y: 0 });
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
2026-08-01 22:09:02 +07:00
|
|
|
const handleMouseDown = useCallback(
|
|
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
|
setDragging(true);
|
|
|
|
|
setDragStart({ x: e.clientX - position.x, y: e.clientY - position.y });
|
|
|
|
|
},
|
|
|
|
|
[position],
|
|
|
|
|
);
|
2026-07-28 12:17:58 +07:00
|
|
|
|
2026-08-01 22:09:02 +07:00
|
|
|
const handleMouseMove = useCallback(
|
|
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
|
if (!dragging) return;
|
|
|
|
|
setPosition({ x: e.clientX - dragStart.x, y: e.clientY - dragStart.y });
|
|
|
|
|
},
|
|
|
|
|
[dragging, dragStart],
|
|
|
|
|
);
|
2026-07-28 12:17:58 +07:00
|
|
|
|
|
|
|
|
const handleMouseUp = useCallback(() => setDragging(false), []);
|
|
|
|
|
|
|
|
|
|
// Focus input when chat opens
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (chatOpen) {
|
|
|
|
|
// Small delay for the animation
|
|
|
|
|
const id = setTimeout(() => inputRef.current?.focus(), 150);
|
|
|
|
|
return () => clearTimeout(id);
|
|
|
|
|
}
|
|
|
|
|
}, [chatOpen]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-01 22:09:02 +07:00
|
|
|
// biome-ignore lint/a11y/noStaticElementInteractions: drag container — mouse-move gesture surface, not keyboard-interactive content
|
2026-07-28 12:17:58 +07:00
|
|
|
<div
|
|
|
|
|
className="fixed bottom-4 right-4 z-40 select-none"
|
|
|
|
|
style={{ transform: `translate(${position.x}px, ${position.y}px)` }}
|
|
|
|
|
onMouseMove={handleMouseMove}
|
|
|
|
|
onMouseUp={handleMouseUp}
|
|
|
|
|
onMouseLeave={handleMouseUp}
|
|
|
|
|
>
|
2026-07-28 15:00:29 +07:00
|
|
|
{/* Main chatbot bubble */}
|
2026-07-28 12:17:58 +07:00
|
|
|
<div
|
|
|
|
|
className={`glass-intense rounded-2xl overflow-hidden transition-all duration-200 ${
|
|
|
|
|
minimized ? "w-14 h-14 cursor-pointer" : "w-[220px]"
|
|
|
|
|
}`}
|
|
|
|
|
style={{ height: minimized ? 56 : 320 }}
|
|
|
|
|
>
|
|
|
|
|
{minimized ? (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setMinimized(false)}
|
|
|
|
|
className="w-full h-full flex items-center justify-center"
|
|
|
|
|
onMouseDown={handleMouseDown}
|
2026-07-28 15:00:29 +07:00
|
|
|
aria-label="Open chatbot"
|
2026-07-28 12:17:58 +07:00
|
|
|
>
|
|
|
|
|
<Bot className="size-6 text-primary" />
|
|
|
|
|
</button>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{/* Drag handle + controls */}
|
2026-08-01 22:09:02 +07:00
|
|
|
{/* biome-ignore lint/a11y/noStaticElementInteractions: drag handle — mouse-only gesture, keyboard users use the buttons in this header */}
|
2026-07-28 12:17:58 +07:00
|
|
|
<div
|
|
|
|
|
className="flex items-center justify-between px-3 py-1.5 border-b border-glass-border cursor-grab active:cursor-grabbing"
|
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
|
>
|
|
|
|
|
<span className="text-[10px] font-semibold text-text-secondary tracking-wide uppercase">
|
2026-07-28 15:00:29 +07:00
|
|
|
Chatbot
|
2026-07-28 12:17:58 +07:00
|
|
|
</span>
|
|
|
|
|
<div className="flex items-center gap-1">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setChatOpen(!chatOpen)}
|
|
|
|
|
className="size-5 flex items-center justify-center rounded hover:bg-glass-bg transition-colors"
|
|
|
|
|
aria-label={chatOpen ? "Close chat" : "Open chat"}
|
|
|
|
|
>
|
|
|
|
|
<MessageCircle className="size-3 text-text-secondary/60 hover:text-text-primary" />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setMinimized(true)}
|
|
|
|
|
className="size-5 flex items-center justify-center rounded hover:bg-glass-bg transition-colors"
|
2026-07-28 15:00:29 +07:00
|
|
|
aria-label="Minimize chatbot"
|
2026-07-28 12:17:58 +07:00
|
|
|
>
|
|
|
|
|
{minimized ? (
|
|
|
|
|
<Bot className="size-3 text-text-secondary/60 hover:text-text-primary" />
|
|
|
|
|
) : (
|
|
|
|
|
<Minimize2 className="size-3 text-text-secondary/60 hover:text-text-primary" />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Canvas area */}
|
|
|
|
|
<div className="h-[140px] flex items-center justify-center">
|
2026-07-28 15:00:29 +07:00
|
|
|
<ChatbotCanvas />
|
2026-07-28 12:17:58 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Chat panel (expandable) */}
|
|
|
|
|
<div
|
|
|
|
|
className={`transition-all duration-200 overflow-hidden ${
|
|
|
|
|
chatOpen ? "h-[130px]" : "h-0"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<ChatPanel inputRef={inputRef} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|