"use client"; import { MoreHorizontalIcon, Pencil, PlusIcon, RefreshCwIcon, SearchIcon, Trash2Icon, UsersRoundIcon, } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { toast } from "sonner"; import { EditDialog } from "./team-edit"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { ScrollArea } from "@/components/ui/scroll-area"; import { createAgentTeam, deleteAgentTeam, fetchAgentTeams, updateAgentTeam, type AdminAgentTeam, type CreateAdminAgentTeamPayload, } from "@/lib/api/admin"; import { Status, StatusLabels } from "@/lib/generated/enums"; import { getEnumLabel } from "@/lib/enums"; import { cn } from "@/lib/utils"; type AgentTeamSidebarProps = { selectedTeamId: number | null; onSelectTeam: (team: AdminAgentTeam | null) => void; onTeamsChange?: (teams: AdminAgentTeam[]) => void; }; const statusTabs = [ { value: "all", label: "全部" }, { value: String(Status.Ok), label: StatusLabels[Status.Ok] }, { value: String(Status.Disabled), label: StatusLabels[Status.Disabled] }, ] as const; export function AgentTeamSidebar({ selectedTeamId, onSelectTeam, onTeamsChange, }: AgentTeamSidebarProps) { const [keyword, setKeyword] = useState(""); const [statusFilter, setStatusFilter] = useState<(typeof statusTabs)[number]["value"]>("all"); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [actionLoadingId, setActionLoadingId] = useState(null); const [dialogOpen, setDialogOpen] = useState(false); const [editingItem, setEditingItem] = useState(null); const [teams, setTeams] = useState([]); const loadData = useCallback(async () => { setLoading(true); try { const data = await fetchAgentTeams({ page: 1, limit: 200 }); setTeams(data); onTeamsChange?.(data); } catch (error) { toast.error(error instanceof Error ? error.message : "加载客服组失败"); } finally { setLoading(false); } }, [onTeamsChange]); useEffect(() => { void loadData(); }, [loadData]); useEffect(() => { if (selectedTeamId == null) { return; } const matchedTeam = teams.find((item) => item.id === selectedTeamId) ?? null; if (matchedTeam) { onSelectTeam(matchedTeam); return; } if (!loading && teams.length > 0) { onSelectTeam(teams[0]); } }, [loading, onSelectTeam, selectedTeamId, teams]); const filteredTeams = useMemo(() => { const output = keyword.trim().toLowerCase(); return teams.filter((item) => { const matchedKeyword = output.length === 0 || item.name.toLowerCase().includes(output) || item.description.toLowerCase().includes(output); const matchedStatus = statusFilter === "all" || String(item.status) === statusFilter; return matchedKeyword && matchedStatus; }); }, [keyword, statusFilter, teams]); function openCreateDialog() { setEditingItem(null); setDialogOpen(true); } function openEditDialog(item: AdminAgentTeam) { setEditingItem(item); setDialogOpen(true); } function handleDialogOpenChange(open: boolean) { if (saving) { return; } if (!open) { setEditingItem(null); } setDialogOpen(open); } async function handleSubmit(payload: CreateAdminAgentTeamPayload) { if (saving) { return; } setSaving(true); try { if (editingItem) { await updateAgentTeam({ id: editingItem.id, ...payload }); toast.success(`已更新客服组:${editingItem.name}`); } else { await createAgentTeam(payload); toast.success(`已创建客服组:${payload.name}`); } setDialogOpen(false); setEditingItem(null); await loadData(); } catch (error) { toast.error(error instanceof Error ? error.message : "保存客服组失败"); } finally { setSaving(false); } } async function handleDelete(item: AdminAgentTeam) { setActionLoadingId(item.id); try { await deleteAgentTeam(item.id); toast.success(`已删除客服组:${item.name}`); await loadData(); } catch (error) { toast.error(error instanceof Error ? error.message : "删除客服组失败"); } finally { setActionLoadingId(null); } } return ( <>
客服组
setKeyword(event.target.value)} placeholder="搜索客服组" className="pl-9" />
{statusTabs.map((item) => ( ))}
{filteredTeams.map((item) => (
} aria-label={`更多操作 ${item.name}`} > openEditDialog(item)}> 编辑 void handleDelete(item)} className="text-destructive focus:text-destructive" > {actionLoadingId === item.id ? "删除中..." : "删除"}
))} {!loading && filteredTeams.length === 0 ? (
没有匹配的客服组
) : null}
); }