"use client" import { useCallback, useEffect, useState } from "react" import { Building2Icon, MessagesSquareIcon, MessageSquareMoreIcon, MoreHorizontalIcon, PlusIcon, RefreshCwIcon, SearchIcon, Trash2Icon, } from "lucide-react" import { toast } from "sonner" import { createChannel, deleteChannel, fetchChannels, updateChannel, updateChannelStatus, type AdminChannel, type CreateAdminChannelPayload, type PageResult, } from "@/lib/api/admin" import { DashboardPage, DashboardTableShell, DashboardTableStateRow, DashboardToolbar, } from "@/components/dashboard-page" import { OptionCombobox } from "@/components/option-combobox" import { EditDialog } from "./_components/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 { ListPagination } from "@/components/list-pagination" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Status, StatusLabels } from "@/lib/generated/enums" import { getEnumOptions } from "@/lib/enums" import { ButtonGroup } from "@/components/ui/button-group" import { Switch } from "@/components/ui/switch" import { useI18n } from "@/i18n/provider" function getChannelTypeLabel(channelType: string, t: (key: string) => string) { if (channelType === "wechat_mp") { return t("channel.typeWechatMp") } if (channelType === "wxwork_kf") { return t("channel.typeWxworkKf") } return t("channel.typeWeb") } function getStatusLabel(status: Status, t: (key: string) => string) { if (status === Status.Disabled) { return t("status.disabled") } if (status === Status.Deleted) { return t("status.deleted") } return t("status.ok") } function ChannelIcon({ channelType }: { channelType: string }) { if (channelType === "wechat_mp") { return } if (channelType === "wxwork_kf") { return } return } export default function DashboardChannelsPage() { const t = useI18n() const [nameInput, setNameInput] = useState("") const [channelIdInput, setChannelIdInput] = useState("") const [channelTypeInput, setChannelTypeInput] = useState("all") const [statusInput, setStatusInput] = useState("all") const [name, setName] = useState("") const [channelId, setChannelId] = useState("") const [channelType, setChannelType] = useState("all") const [status, setStatus] = useState("all") const [page, setPage] = useState(1) const [limit, setLimit] = useState(20) 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 [result, setResult] = useState>({ results: [], page: { page: 1, limit: 20, total: 0 }, }) const loadData = useCallback(async () => { setLoading(true) try { const data = await fetchChannels({ name: name.trim() || undefined, channelId: channelId.trim() || undefined, channelType: channelType === "all" ? undefined : channelType, status: status === "all" ? undefined : status, page, limit, }) setResult(data) } catch (error) { toast.error(error instanceof Error ? error.message : t("channel.loadFailed")) } finally { setLoading(false) } }, [channelId, channelType, limit, name, page, status, t]) const statusOptions = [ { value: "all", label: t("status.all") }, ...getEnumOptions(StatusLabels).map((option) => ({ value: String(option.value), label: getStatusLabel(option.value as Status, t), })), ] const channelTypeOptions = [ { value: "all", label: t("channel.allTypes") }, { value: "web", label: t("channel.typeWeb") }, { value: "wechat_mp", label: t("channel.typeWechatMp") }, { value: "wxwork_kf", label: t("channel.typeWxworkKf") }, ] useEffect(() => { void loadData() }, [loadData]) function applyFilters() { setName(nameInput) setChannelId(channelIdInput) setChannelType(channelTypeInput) setStatus(statusInput) setPage(1) } function handleFilterKeyDown(event: React.KeyboardEvent) { if (event.key !== "Enter") { return } event.preventDefault() applyFilters() } function openCreateDialog() { setEditingItem(null) setDialogOpen(true) } function openEditDialog(item: AdminChannel) { setEditingItem(item) setDialogOpen(true) } async function handleSubmit(payload: CreateAdminChannelPayload) { if (saving) { return } setSaving(true) try { if (editingItem) { await updateChannel({ id: editingItem.id, ...payload }) toast.success(t("channel.updated", { name: payload.name })) } else { const created = await createChannel(payload) toast.success(t("channel.created", { name: created.name })) } setDialogOpen(false) setEditingItem(null) await loadData() } catch (error) { toast.error(error instanceof Error ? error.message : t("channel.saveFailed")) } finally { setSaving(false) } } async function handleToggleStatus(item: AdminChannel) { setActionLoadingId(item.id) try { const nextStatus = item.status === Status.Ok ? Status.Disabled : Status.Ok await updateChannelStatus(item.id, nextStatus) toast.success(t(nextStatus === Status.Ok ? "channel.statusEnabled" : "channel.statusDisabled", { name: item.name })) await loadData() } catch (error) { toast.error(error instanceof Error ? error.message : t("channel.statusUpdateFailed")) } finally { setActionLoadingId(null) } } async function handleDelete(item: AdminChannel) { setActionLoadingId(item.id) try { await deleteChannel(item.id) toast.success(t("channel.deleted", { name: item.name })) await loadData() } catch (error) { toast.error(error instanceof Error ? error.message : t("channel.deleteFailed")) } finally { setActionLoadingId(null) } } return ( <> } > setNameInput(event.target.value)} onKeyDown={handleFilterKeyDown} placeholder={t("channel.filterName")} className="w-full sm:w-56" />
setChannelIdInput(event.target.value)} onKeyDown={handleFilterKeyDown} placeholder={t("channel.filterChannelId")} className="pl-9" />
setPage(nextPage)} onLimitChange={(nextLimit) => { setLimit(nextLimit) setPage(1) }} /> } > {t("channel.columnChannel")} {t("channel.columnType")} ChannelID {t("channel.columnAgent")} {t("channel.columnStatus")} {t("channel.columnActions")} {loading || result.results.length === 0 ? ( ) : null} {result.results.map((item) => (
{item.name}
{getChannelTypeLabel(item.channelType, t)}
{getChannelTypeLabel(item.channelType, t)} {item.channelId || "-"} {item.aiAgentName || "-"}
void handleToggleStatus(item)} aria-label={t("channel.toggleStatus", { name: item.name })} /> {getStatusLabel(item.status as Status, t)}
} aria-label={t("channel.moreActions", { name: item.name })} > void handleDelete(item)} > {t("channel.delete")}
))}
) }