2026-08-15 17:53:48 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-15 20:03:55 +07:00
|
|
|
import {
|
2026-08-17 21:29:37 +07:00
|
|
|
Bot,
|
2026-08-26 03:37:14 +07:00
|
|
|
MessageSquare,
|
|
|
|
|
Send,
|
2026-08-17 21:29:37 +07:00
|
|
|
Trash2,
|
|
|
|
|
X,
|
|
|
|
|
} from "lucide-react";
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-08-26 03:37:14 +07:00
|
|
|
import { useGSAP } from "@gsap/react";
|
|
|
|
|
import gsap from "gsap";
|
2026-08-17 21:29:37 +07:00
|
|
|
import { Avatar, Button, GlassPanel, toast } from "@/components/primitives";
|
|
|
|
|
import { MarkdownLite } from "@/components/shared";
|
2026-08-15 17:53:48 +07:00
|
|
|
import { useChatbotUserId } from "@/hooks/use-chatbot-user";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { chatbotApi } from "@/lib/api";
|
2026-08-15 17:53:48 +07:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
gsap.registerPlugin(useGSAP);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
interface Msg {
|
2026-08-17 21:29:37 +07:00
|
|
|
id: string;
|
2026-08-15 17:53:48 +07:00
|
|
|
role: "user" | "bot";
|
|
|
|
|
content: string;
|
2026-08-17 21:29:37 +07:00
|
|
|
ts: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatTime(ts: number): string {
|
|
|
|
|
try {
|
|
|
|
|
return new Date(ts).toLocaleTimeString([], {
|
|
|
|
|
hour: "2-digit",
|
|
|
|
|
minute: "2-digit",
|
|
|
|
|
});
|
|
|
|
|
} catch {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
export function Chatbot() {
|
|
|
|
|
const userId = useChatbotUserId();
|
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const [msgs, setMsgs] = useState<Msg[]>([]);
|
|
|
|
|
const [input, setInput] = useState("");
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2026-08-26 03:37:14 +07:00
|
|
|
const panelRef = useRef<HTMLDivElement>(null);
|
2026-08-17 21:29:37 +07:00
|
|
|
const bottomRef = useRef<HTMLDivElement>(null);
|
2026-08-15 17:53:48 +07:00
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
useGSAP(
|
|
|
|
|
() => {
|
|
|
|
|
if (!open || !panelRef.current) return;
|
|
|
|
|
gsap.fromTo(
|
|
|
|
|
panelRef.current,
|
|
|
|
|
{ opacity: 0, y: 12, scale: 0.98 },
|
|
|
|
|
{ opacity: 1, y: 0, scale: 1, duration: 0.22, ease: "power2.out" },
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
{ dependencies: [open] },
|
|
|
|
|
);
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open || !userId) return;
|
|
|
|
|
chatbotApi
|
|
|
|
|
.getHistory(userId)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
setMsgs(
|
2026-08-15 20:03:55 +07:00
|
|
|
res.history.slice(-12).flatMap((h) => [
|
2026-08-17 21:29:37 +07:00
|
|
|
{
|
|
|
|
|
id: `${h.id}-u`,
|
|
|
|
|
role: "user" as const,
|
|
|
|
|
content: h.user_message,
|
|
|
|
|
ts: Date.parse(h.created_at) || Date.now(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: `${h.id}-b`,
|
|
|
|
|
role: "bot" as const,
|
|
|
|
|
content: h.bot_response,
|
|
|
|
|
ts: Date.parse(h.created_at) || Date.now(),
|
|
|
|
|
},
|
2026-08-15 20:03:55 +07:00
|
|
|
]),
|
2026-08-15 17:53:48 +07:00
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
}, [open, userId]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-08-26 03:37:14 +07:00
|
|
|
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
|
|
|
|
|
}, [msgs, loading]);
|
2026-08-17 21:29:37 +07:00
|
|
|
|
|
|
|
|
const send = async (text: string) => {
|
|
|
|
|
if (!text.trim() || loading || !userId) return;
|
2026-08-15 17:53:48 +07:00
|
|
|
setInput("");
|
2026-08-17 21:29:37 +07:00
|
|
|
setMsgs((m) => [
|
|
|
|
|
...m,
|
|
|
|
|
{ id: `u-${Date.now()}`, role: "user", content: text, ts: Date.now() },
|
|
|
|
|
]);
|
2026-08-15 17:53:48 +07:00
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await chatbotApi.send(text, undefined, userId);
|
2026-08-17 21:29:37 +07:00
|
|
|
setMsgs((m) => [
|
|
|
|
|
...m,
|
|
|
|
|
{
|
|
|
|
|
id: `b-${Date.now()}`,
|
|
|
|
|
role: "bot",
|
|
|
|
|
content: res.response,
|
|
|
|
|
ts: Date.parse(res.timestamp) || Date.now(),
|
|
|
|
|
},
|
|
|
|
|
]);
|
2026-08-15 17:53:48 +07:00
|
|
|
} catch (e) {
|
|
|
|
|
toast({ title: "Chat error", description: String(e), tone: "vermilion" });
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
const clearAll = async () => {
|
|
|
|
|
if (!userId) return;
|
|
|
|
|
setMsgs([]);
|
|
|
|
|
try {
|
|
|
|
|
await chatbotApi.clearHistory(userId);
|
|
|
|
|
} catch {
|
|
|
|
|
/* best-effort */
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label="Open assistant"
|
|
|
|
|
onClick={() => setOpen((o) => !o)}
|
2026-08-26 03:37:14 +07:00
|
|
|
className="fixed right-4 bottom-5 z-50 flex size-10 items-center justify-center rounded-full border border-white/[0.12] bg-[#0f1011] text-[#f7f8f8] shadow-2xl transition-all duration-150 hover:scale-105 hover:border-[#7170ff] hover:bg-[#191a1b]"
|
2026-08-15 17:53:48 +07:00
|
|
|
>
|
2026-08-26 03:37:14 +07:00
|
|
|
{open ? <X className="size-4" /> : <MessageSquare className="size-4 text-[#7170ff]" />}
|
2026-08-15 17:53:48 +07:00
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
{open && (
|
2026-08-26 03:37:14 +07:00
|
|
|
<div
|
|
|
|
|
ref={panelRef}
|
|
|
|
|
className="fixed right-4 bottom-18 z-50 flex h-[min(65dvh,480px)] w-[min(92vw,350px)] flex-col rounded-[10px] border border-white/[0.08] bg-[#0f1011]/95 p-0 shadow-2xl backdrop-blur-xl"
|
2026-08-15 17:53:48 +07:00
|
|
|
>
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="flex items-center justify-between border-b border-white/[0.06] px-3.5 py-2.5">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<span className="flex size-6 items-center justify-center rounded-md bg-[#7170ff]/15 text-[#7170ff]">
|
|
|
|
|
<Bot className="size-3.5" />
|
|
|
|
|
</span>
|
|
|
|
|
<div>
|
|
|
|
|
<div className="text-xs font-semibold text-[#f7f8f8]">
|
|
|
|
|
Linear Assistant
|
|
|
|
|
</div>
|
|
|
|
|
<div className="font-mono text-[9px] text-[#62666d]">
|
|
|
|
|
active context
|
|
|
|
|
</div>
|
2026-08-15 20:03:55 +07:00
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
2026-08-17 21:29:37 +07:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={clearAll}
|
2026-08-26 03:37:14 +07:00
|
|
|
className="rounded-[5px] p-1 text-[#62666d] hover:bg-white/[0.05] hover:text-[#d0d6e0]"
|
2026-08-17 21:29:37 +07:00
|
|
|
>
|
2026-08-26 03:37:14 +07:00
|
|
|
<Trash2 className="size-3.5" />
|
2026-08-17 21:29:37 +07:00
|
|
|
</button>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="flex-1 space-y-2.5 overflow-y-auto px-3.5 py-3">
|
2026-08-17 21:29:37 +07:00
|
|
|
{msgs.length === 0 && !loading && (
|
2026-08-26 03:37:14 +07:00
|
|
|
<div className="py-8 text-center text-xs text-[#62666d]">
|
|
|
|
|
Ask anything about telemetry, moderation, or media.
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
)}
|
2026-08-17 21:29:37 +07:00
|
|
|
{msgs.map((m) => (
|
2026-08-15 20:03:55 +07:00
|
|
|
<div
|
2026-08-17 21:29:37 +07:00
|
|
|
key={m.id}
|
2026-08-15 20:03:55 +07:00
|
|
|
className={cn(
|
2026-08-26 03:37:14 +07:00
|
|
|
"flex flex-col text-xs",
|
|
|
|
|
m.role === "user" ? "items-end" : "items-start",
|
2026-08-15 20:03:55 +07:00
|
|
|
)}
|
|
|
|
|
>
|
2026-08-26 03:37:14 +07:00
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"max-w-[85%] rounded-[6px] px-2.5 py-1.5",
|
|
|
|
|
m.role === "user"
|
|
|
|
|
? "bg-[#5e6ad2] text-white"
|
|
|
|
|
: "border border-white/[0.06] bg-white/[0.03] text-[#d0d6e0]",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{m.role === "bot" ? (
|
|
|
|
|
<MarkdownLite content={m.content} />
|
|
|
|
|
) : (
|
|
|
|
|
<span>{m.content}</span>
|
|
|
|
|
)}
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
2026-08-26 03:37:14 +07:00
|
|
|
<span className="font-mono mt-0.5 text-[9px] text-[#62666d]">
|
|
|
|
|
{formatTime(m.ts)}
|
|
|
|
|
</span>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
))}
|
2026-08-17 21:29:37 +07:00
|
|
|
<div ref={bottomRef} />
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
|
2026-08-26 03:37:14 +07:00
|
|
|
<form
|
|
|
|
|
onSubmit={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
send(input);
|
|
|
|
|
}}
|
|
|
|
|
className="flex items-center gap-1.5 border-t border-white/[0.06] p-2"
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
2026-08-15 17:53:48 +07:00
|
|
|
value={input}
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
2026-08-26 03:37:14 +07:00
|
|
|
placeholder="Ask command..."
|
|
|
|
|
className="flex-1 rounded-[5px] border border-white/[0.08] bg-white/[0.02] px-2.5 py-1.5 text-xs text-[#f7f8f8] placeholder:text-[#62666d] focus:border-[#7170ff] focus:outline-none"
|
2026-08-15 17:53:48 +07:00
|
|
|
/>
|
2026-08-26 03:37:14 +07:00
|
|
|
<Button type="submit" size="sm" variant="primary">
|
|
|
|
|
<Send className="size-3" />
|
2026-08-15 17:53:48 +07:00
|
|
|
</Button>
|
2026-08-26 03:37:14 +07:00
|
|
|
</form>
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|