'use client'; import { useCallback, useEffect, useState } from 'react'; import Link from 'next/link'; import Sidebar from '@/components/Sidebar'; import { ArrowLeft, Sparkles, Check } from 'lucide-react'; type Group = { id: number; name: string; color: string; emoji: string | null }; type Suggestion = { chatroom_id: string; name: string; summary: string; suggested_group_id: number | null; suggested_group_name: string | null; reason: string; }; export default function ClassifyPage() { const [groups, setGroups] = useState([]); const [suggestions, setSuggestions] = useState([]); const [picks, setPicks] = useState>({}); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const load = useCallback(async () => { const r = await fetch('/api/ai-classify'); const j = await r.json(); if (j.ok) { setGroups(j.groups); setSuggestions(j.suggestions); const initial: Record = {}; for (const s of j.suggestions as Suggestion[]) { initial[s.chatroom_id] = s.suggested_group_id; } setPicks(initial); } }, []); useEffect(() => { queueMicrotask(() => void load()); }, [load]); const apply = async () => { setBusy(true); setMsg(null); const list = Object.entries(picks) .filter(([, v]) => v !== null) .map(([chatroom_id, group_id]) => ({ chatroom_id, group_id: group_id as number })); if (list.length === 0) { setMsg('没有可应用的分类'); setBusy(false); return; } const r = await fetch('/api/ai-classify', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ picks: list }), }); const j = await r.json(); setBusy(false); if (j.ok) { setMsg(`已应用 ${j.applied} 条`); load(); } else { setMsg('应用失败:' + (j.error ?? '未知')); } }; const matched = suggestions.filter((s) => picks[s.chatroom_id] !== null).length; return (
AI Classification
AI 智能分类
{suggestions.length} 个未分组群 · 已建议 {matched} 条
{msg && {msg}}
{suggestions.length === 0 ? (
所有群都已分类
) : (
{suggestions.map((s) => ( ))}
群名 最近消息 建议分组 理由
{s.name}
{s.summary}
{s.reason}
)}
); }