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,
|
|
|
|
|
Check,
|
|
|
|
|
Copy,
|
|
|
|
|
MessageCircle,
|
|
|
|
|
RotateCw,
|
|
|
|
|
Trash2,
|
|
|
|
|
X,
|
|
|
|
|
} from "lucide-react";
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function TypingDots() {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center gap-1 py-1">
|
|
|
|
|
{[0, 1, 2].map((i) => (
|
|
|
|
|
<span
|
|
|
|
|
key={i}
|
|
|
|
|
className="size-1.5 rounded-full bg-ink-faint animate-bounce"
|
|
|
|
|
style={{ animationDelay: `${i * 0.15}s`, animationDuration: "0.9s" }}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
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-17 21:29:37 +07:00
|
|
|
const [failed, setFailed] = useState<string | null>(null);
|
|
|
|
|
const [copiedId, setCopiedId] = useState<string | null>(null);
|
2026-08-15 17:53:48 +07:00
|
|
|
const listRef = useRef<HTMLDivElement>(null);
|
2026-08-17 21:29:37 +07:00
|
|
|
const taRef = useRef<HTMLTextAreaElement>(null);
|
|
|
|
|
const bottomRef = useRef<HTMLDivElement>(null);
|
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]);
|
|
|
|
|
|
2026-08-17 21:29:37 +07:00
|
|
|
// Auto-scroll to newest whenever the thread or typing state changes.
|
|
|
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: scroll must fire on thread/typing changes even though the body only reads the ref
|
2026-08-15 17:53:48 +07:00
|
|
|
useEffect(() => {
|
2026-08-17 21:29:37 +07:00
|
|
|
bottomRef.current?.scrollIntoView({ behavior: "smooth", block: "end" });
|
|
|
|
|
}, [msgs, loading, open]);
|
2026-08-15 17:53:48 +07:00
|
|
|
|
2026-08-17 21:29:37 +07:00
|
|
|
// Auto-grow the composer.
|
|
|
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: height recompute is keyed to input changes; body only reads the textarea element
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const ta = taRef.current;
|
|
|
|
|
if (!ta) return;
|
|
|
|
|
ta.style.height = "auto";
|
|
|
|
|
ta.style.height = `${Math.min(ta.scrollHeight, 140)}px`;
|
|
|
|
|
}, [input]);
|
|
|
|
|
|
|
|
|
|
const copy = async (id: string, text: string) => {
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(text);
|
|
|
|
|
setCopiedId(id);
|
|
|
|
|
window.setTimeout(() => setCopiedId((c) => (c === id ? null : c)), 1200);
|
|
|
|
|
} catch {
|
|
|
|
|
/* clipboard unavailable */
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const clearAll = async () => {
|
|
|
|
|
if (!userId) return;
|
|
|
|
|
setMsgs([]);
|
|
|
|
|
try {
|
|
|
|
|
await chatbotApi.clearHistory(userId);
|
|
|
|
|
} catch {
|
|
|
|
|
/* best-effort */
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const send = async (text: string) => {
|
|
|
|
|
if (!text.trim() || loading || !userId) return;
|
|
|
|
|
setFailed(null);
|
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) {
|
2026-08-17 21:29:37 +07:00
|
|
|
setFailed(text);
|
2026-08-15 17:53:48 +07:00
|
|
|
toast({ title: "Chat error", description: String(e), tone: "vermilion" });
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label="Open assistant"
|
|
|
|
|
onClick={() => setOpen((o) => !o)}
|
2026-08-17 21:29:37 +07:00
|
|
|
className="fixed right-4 bottom-24 z-50 flex items-center justify-center rounded-full bg-signal text-signal-ink shadow-[0_10px_30px_-8px_var(--color-signal-glow)] transition-transform hover:scale-105 md:right-5 md:bottom-5"
|
2026-08-15 17:53:48 +07:00
|
|
|
style={{ width: 52, height: 52 }}
|
|
|
|
|
>
|
|
|
|
|
{open ? <X className="size-5" /> : <MessageCircle className="size-5" />}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
{open && (
|
|
|
|
|
<GlassPanel
|
2026-08-17 21:29:37 +07:00
|
|
|
className="fixed right-4 bottom-24 z-50 flex w-[min(94vw,360px)] flex-col p-0 md:right-5 md:bottom-20"
|
|
|
|
|
style={{
|
|
|
|
|
animation: "fade-up 0.16s ease",
|
|
|
|
|
height: "min(68dvh, 520px)",
|
|
|
|
|
}}
|
2026-08-15 17:53:48 +07:00
|
|
|
>
|
|
|
|
|
<div className="flex items-center gap-2 border-b border-hairline px-4 py-3">
|
|
|
|
|
<span className="flex size-8 items-center justify-center rounded-full bg-signal/15 text-signal">
|
|
|
|
|
<Bot className="size-4" />
|
|
|
|
|
</span>
|
|
|
|
|
<div>
|
2026-08-15 20:03:55 +07:00
|
|
|
<div className="text-sm font-semibold text-ink">
|
|
|
|
|
GMW Assistant
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mono text-[0.6rem] text-ink-faint">
|
|
|
|
|
context-aware
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
2026-08-17 21:29:37 +07:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label="Clear conversation"
|
|
|
|
|
onClick={clearAll}
|
|
|
|
|
className="ml-auto rounded-[9px] p-1.5 text-ink-faint transition-colors hover:bg-white/5 hover:text-ink-soft"
|
|
|
|
|
>
|
|
|
|
|
<Trash2 className="size-4" />
|
|
|
|
|
</button>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
|
2026-08-15 20:03:55 +07:00
|
|
|
<div
|
|
|
|
|
ref={listRef}
|
|
|
|
|
className="flex-1 space-y-3 overflow-y-auto px-4 py-3"
|
|
|
|
|
>
|
2026-08-17 21:29:37 +07:00
|
|
|
{msgs.length === 0 && !loading && (
|
2026-08-15 17:53:48 +07:00
|
|
|
<div className="py-8 text-center text-xs text-ink-faint">
|
|
|
|
|
Ask about moderation, voice, or media.
|
|
|
|
|
</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-17 21:29:37 +07:00
|
|
|
"group flex gap-2",
|
2026-08-15 20:03:55 +07:00
|
|
|
m.role === "user" ? "justify-end" : "justify-start",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{m.role === "bot" && (
|
|
|
|
|
<Avatar
|
|
|
|
|
name="GMW"
|
|
|
|
|
size={26}
|
|
|
|
|
className="mt-0.5 bg-signal/15 text-signal"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-08-17 21:29:37 +07:00
|
|
|
<div className="flex max-w-[82%] flex-col">
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"rounded-2xl px-3 py-2 text-sm",
|
|
|
|
|
m.role === "user"
|
|
|
|
|
? "rounded-br-sm bg-signal/20 text-ink"
|
|
|
|
|
: "rounded-bl-sm bg-white/5 text-ink-soft",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{m.role === "bot" ? (
|
|
|
|
|
<MarkdownLite content={m.content} />
|
|
|
|
|
) : (
|
|
|
|
|
<span className="whitespace-pre-wrap break-words">
|
|
|
|
|
{m.content}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"mt-0.5 flex items-center gap-1.5 text-[0.6rem] text-ink-faint",
|
|
|
|
|
m.role === "user" && "flex-row-reverse",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<span className="mono">{formatTime(m.ts)}</span>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label="Copy message"
|
|
|
|
|
onClick={() => copy(m.id, m.content)}
|
|
|
|
|
className="rounded p-0.5 opacity-0 transition-opacity hover:text-ink-soft group-hover:opacity-100"
|
|
|
|
|
>
|
|
|
|
|
{copiedId === m.id ? (
|
|
|
|
|
<Check className="size-3 text-signal" />
|
|
|
|
|
) : (
|
|
|
|
|
<Copy className="size-3" />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
{loading && (
|
|
|
|
|
<div className="flex gap-2">
|
2026-08-15 20:03:55 +07:00
|
|
|
<Avatar
|
|
|
|
|
name="GMW"
|
|
|
|
|
size={26}
|
|
|
|
|
className="bg-signal/15 text-signal"
|
|
|
|
|
/>
|
2026-08-17 21:29:37 +07:00
|
|
|
<div className="rounded-2xl rounded-bl-sm bg-white/5 px-3 py-2">
|
|
|
|
|
<TypingDots />
|
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
|
|
|
<div ref={bottomRef} />
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
|
2026-08-17 21:29:37 +07:00
|
|
|
{failed && (
|
|
|
|
|
<div className="mx-3 mb-1 flex items-center gap-2 rounded-[10px] border border-vermilion/30 bg-vermilion/10 px-3 py-1.5 text-xs text-vermilion">
|
|
|
|
|
<span className="flex-1 truncate">Send failed</span>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => send(failed)}
|
|
|
|
|
className="inline-flex items-center gap-1 font-medium hover:underline"
|
|
|
|
|
>
|
|
|
|
|
<RotateCw className="size-3" /> Retry
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="flex items-end gap-2 border-t border-hairline p-3">
|
|
|
|
|
<textarea
|
|
|
|
|
ref={taRef}
|
|
|
|
|
rows={1}
|
2026-08-15 17:53:48 +07:00
|
|
|
placeholder="Message…"
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
2026-08-17 21:29:37 +07:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
send(input);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
className="max-h-[140px] min-h-[40px] flex-1 resize-none rounded-[11px] bg-white/5 border border-hairline px-3 py-2 text-sm text-ink placeholder:text-ink-faint transition-colors focus:outline-none focus:border-signal/50 focus:bg-white/8"
|
2026-08-15 17:53:48 +07:00
|
|
|
/>
|
2026-08-15 20:03:55 +07:00
|
|
|
<Button
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="icon"
|
2026-08-17 21:29:37 +07:00
|
|
|
onClick={() => send(input)}
|
2026-08-15 20:03:55 +07:00
|
|
|
disabled={loading}
|
|
|
|
|
>
|
2026-08-17 21:29:37 +07:00
|
|
|
<MessageCircle className="size-4" />
|
2026-08-15 17:53:48 +07:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</GlassPanel>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|