feat: refactor quick replies page to use DashboardCrudPage component

- Removed redundant state management and effects from DashboardQuickRepliesPage.
- Integrated DashboardCrudPage for handling CRUD operations and filtering.
- Created a new DashboardCrudPage component to encapsulate common CRUD logic.
- Added utility functions for building and normalizing dashboard CRUD queries.
- Implemented tests for the new utility functions to ensure correctness.
This commit is contained in:
mlogclub
2026-05-28 08:39:14 +08:00
parent dd1b0d1b81
commit fe480ff131
7 changed files with 1054 additions and 987 deletions
+158 -321
View File
@@ -1,18 +1,15 @@
"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 { DashboardCrudPage } from "@/components/dashboard/crud"
import { Badge } from "@/components/ui/badge"
import { Switch } from "@/components/ui/switch"
import {
createChannel,
deleteChannel,
@@ -21,39 +18,11 @@ import {
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 { Status, StatusLabels } from "@/lib/generated/enums"
import { useI18n } from "@/i18n/provider"
import { EditDialog } from "./_components/edit"
function getChannelTypeLabel(channelType: string, t: (key: string) => string) {
if (channelType === "wechat_mp") {
@@ -87,45 +56,6 @@ function ChannelIcon({ channelType }: { channelType: string }) {
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<number | null>(null)
const [dialogOpen, setDialogOpen] = useState(false)
const [editingItem, setEditingItem] = useState<AdminChannel | null>(null)
const [result, setResult] = useState<PageResult<AdminChannel>>({
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) => ({
@@ -133,7 +63,6 @@ export default function DashboardChannelsPage() {
label: getStatusLabel(option.value as Status, t),
})),
]
const channelTypeOptions = [
{ value: "all", label: t("channel.allTypes") },
{ value: "web", label: t("channel.typeWeb") },
@@ -141,251 +70,159 @@ export default function DashboardChannelsPage() {
{ 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<HTMLInputElement>) {
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 (
<>
<DashboardPage>
<DashboardToolbar
actions={
<>
<Button variant="outline" onClick={() => void loadData()} disabled={loading}>
<RefreshCwIcon className={loading ? "animate-spin" : ""} />
{t("channel.refresh")}
</Button>
<Button onClick={openCreateDialog}>
<PlusIcon />
{t("channel.new")}
</Button>
</>
}
>
<Input
value={nameInput}
onChange={(event) => setNameInput(event.target.value)}
onKeyDown={handleFilterKeyDown}
placeholder={t("channel.filterName")}
className="w-full sm:w-56"
/>
<div className="relative w-full sm:w-72">
<SearchIcon className="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" />
<Input
value={channelIdInput}
onChange={(event) => setChannelIdInput(event.target.value)}
onKeyDown={handleFilterKeyDown}
placeholder={t("channel.filterChannelId")}
className="pl-9"
/>
</div>
<div className="w-full sm:w-40">
<OptionCombobox
value={channelTypeInput}
options={[...channelTypeOptions]}
placeholder={t("channel.allTypes")}
searchPlaceholder={t("channel.searchType")}
emptyText={t("channel.emptyType")}
onChange={setChannelTypeInput}
/>
</div>
<div className="w-full sm:w-36">
<OptionCombobox
value={statusInput}
options={[...statusOptions]}
placeholder={t("status.all")}
searchPlaceholder={t("channel.searchStatus")}
emptyText={t("channel.emptyStatus")}
onChange={setStatusInput}
/>
</div>
<Button variant="outline" onClick={applyFilters} disabled={loading}>
<SearchIcon />
{t("channel.query")}
</Button>
</DashboardToolbar>
<DashboardTableShell
pagination={
<ListPagination
page={result.page.page}
limit={result.page.limit}
total={result.page.total}
loading={loading}
onPageChange={(nextPage) => setPage(nextPage)}
onLimitChange={(nextLimit) => {
setLimit(nextLimit)
setPage(1)
}}
/>
}
>
<Table>
<TableHeader>
<TableRow>
<TableHead>{t("channel.columnChannel")}</TableHead>
<TableHead>{t("channel.columnType")}</TableHead>
<TableHead>ChannelID</TableHead>
<TableHead>{t("channel.columnAgent")}</TableHead>
<TableHead>{t("channel.columnStatus")}</TableHead>
<TableHead className="w-[88px] text-right">{t("channel.columnActions")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{loading || result.results.length === 0 ? (
<DashboardTableStateRow
colSpan={6}
loading={loading}
loadingText={t("channel.loading")}
emptyText={t("channel.empty")}
/>
) : null}
{result.results.map((item) => (
<TableRow key={item.id}>
<TableCell>
<div className="flex items-center gap-3">
<div className="flex size-10 items-center justify-center rounded-2xl bg-muted">
<ChannelIcon channelType={item.channelType} />
</div>
<div>
<div className="font-medium">{item.name}</div>
<div className="text-xs text-muted-foreground">{getChannelTypeLabel(item.channelType, t)}</div>
</div>
</div>
</TableCell>
<TableCell>
<Badge variant="outline">{getChannelTypeLabel(item.channelType, t)}</Badge>
</TableCell>
<TableCell className="font-mono text-xs">{item.channelId || "-"}</TableCell>
<TableCell>{item.aiAgentName || "-"}</TableCell>
<TableCell>
<div className="flex items-center gap-3">
<Switch
checked={item.status === Status.Ok}
disabled={actionLoadingId === item.id}
onCheckedChange={() => void handleToggleStatus(item)}
aria-label={t("channel.toggleStatus", { name: item.name })}
/>
<Badge variant={item.status === Status.Ok ? "default" : "outline"}>
{getStatusLabel(item.status as Status, t)}
</Badge>
</div>
</TableCell>
<TableCell className="text-right">
<ButtonGroup className="ml-auto">
<Button variant="outline" size="sm" onClick={() => openEditDialog(item)}>
{t("channel.edit")}
</Button>
<DropdownMenu>
<DropdownMenuTrigger
render={<Button variant="outline" size="icon-sm" className="ml-auto" />}
aria-label={t("channel.moreActions", { name: item.name })}
>
<MoreHorizontalIcon />
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem
className="text-destructive"
disabled={actionLoadingId === item.id}
onClick={() => void handleDelete(item)}
>
<Trash2Icon className="size-4" />
{t("channel.delete")}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</ButtonGroup>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</DashboardTableShell>
</DashboardPage>
<EditDialog
open={dialogOpen}
saving={saving}
itemId={editingItem?.id ?? null}
onOpenChange={setDialogOpen}
onSubmit={handleSubmit}
/>
</>
<DashboardCrudPage<AdminChannel, CreateAdminChannelPayload>
filters={[
{
name: "name",
label: t("channel.filterName"),
placeholder: t("channel.filterName"),
defaultValue: "",
trim: true,
className: "w-full sm:w-56",
},
{
name: "channelId",
label: t("channel.filterChannelId"),
placeholder: t("channel.filterChannelId"),
defaultValue: "",
trim: true,
className: "w-full sm:w-72",
},
{
name: "channelType",
label: t("channel.allTypes"),
type: "select",
defaultValue: "all",
allValue: "all",
options: channelTypeOptions,
className: "w-full sm:w-40",
},
{
name: "status",
label: t("status.all"),
type: "select",
defaultValue: "all",
allValue: "all",
options: statusOptions,
className: "w-full sm:w-36",
},
]}
columns={[
{
key: "channel",
label: t("channel.columnChannel"),
render: (item) => (
<div className="flex items-center gap-3">
<div className="flex size-10 items-center justify-center rounded-2xl bg-muted">
<ChannelIcon channelType={item.channelType} />
</div>
<div>
<div className="font-medium">{item.name}</div>
<div className="text-xs text-muted-foreground">
{getChannelTypeLabel(item.channelType, t)}
</div>
</div>
</div>
),
},
{
key: "type",
label: t("channel.columnType"),
render: (item) => (
<Badge variant="outline">
{getChannelTypeLabel(item.channelType, t)}
</Badge>
),
},
{
key: "channelId",
label: "ChannelID",
render: (item) => (
<span className="font-mono text-xs">{item.channelId || "-"}</span>
),
},
{
key: "agent",
label: t("channel.columnAgent"),
render: (item) => item.aiAgentName || "-",
},
{
key: "status",
label: t("channel.columnStatus"),
render: (item, { actionLoading, reload, setActionLoadingId }) => (
<div className="flex items-center gap-3">
<Switch
checked={item.status === Status.Ok}
disabled={actionLoading}
onCheckedChange={() => {
void (async () => {
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 reload()
} catch (error) {
toast.error(
error instanceof Error
? error.message
: t("channel.statusUpdateFailed")
)
} finally {
setActionLoadingId(null)
}
})()
}}
aria-label={t("channel.toggleStatus", { name: item.name })}
/>
<Badge variant={item.status === Status.Ok ? "default" : "outline"}>
{getStatusLabel(item.status as Status, t)}
</Badge>
</div>
),
},
]}
fetchList={fetchChannels}
getItemId={(item) => item.id}
createItem={createChannel}
updateItem={(item, payload) => updateChannel({ id: item.id, ...payload })}
deleteItem={(item) => deleteChannel(item.id)}
renderEditDialog={({ open, saving, itemId, onOpenChange, onSubmit }) => (
<EditDialog
open={open}
saving={saving}
itemId={itemId}
onOpenChange={onOpenChange}
onSubmit={onSubmit}
/>
)}
labels={{
refresh: t("channel.refresh"),
create: t("channel.new"),
query: t("channel.query"),
loading: t("channel.loading"),
empty: t("channel.empty"),
actions: t("channel.columnActions"),
edit: t("channel.edit"),
delete: t("channel.delete"),
processing: t("channel.processing"),
moreActions: (item) => t("channel.moreActions", { name: item.name }),
loadFailed: t("channel.loadFailed"),
saveFailed: t("channel.saveFailed"),
deleteFailed: t("channel.deleteFailed"),
created: (payload) => t("channel.created", { name: payload.name }),
updated: (_item, payload) => t("channel.updated", { name: payload.name }),
deleted: (item) => t("channel.deleted", { name: item.name }),
}}
/>
)
}