2025-11-05 17:20:58 +08:00
|
|
|
|
import React, { useCallback, useMemo, useState, useEffect } from 'react';
|
2025-11-04 18:32:51 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Box,
|
|
|
|
|
|
Typography,
|
|
|
|
|
|
TextField,
|
|
|
|
|
|
InputAdornment,
|
|
|
|
|
|
Paper,
|
|
|
|
|
|
Button,
|
|
|
|
|
|
Pagination,
|
|
|
|
|
|
Stack,
|
|
|
|
|
|
} from '@mui/material';
|
2025-11-05 17:20:58 +08:00
|
|
|
|
import { Search as SearchIcon, Refresh as RefreshIcon, Add as AddIcon } from '@mui/icons-material';
|
|
|
|
|
|
import { useAgentList, useAgentOperations } from '@/hooks/agent-hooks';
|
2025-11-04 18:32:51 +08:00
|
|
|
|
import AgentGridView from '@/components/agent/AgentGridView';
|
2025-11-05 17:20:58 +08:00
|
|
|
|
import CreateAgentDialog from '@/pages/agent/components/CreateAgentDialog';
|
|
|
|
|
|
import EditAgentDialog from '@/pages/agent/components/EditAgentDialog';
|
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
import { useDialog } from '@/hooks/useDialog';
|
2025-11-06 16:53:47 +08:00
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
2025-11-04 18:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
function AgentListPage() {
|
|
|
|
|
|
const [searchValue, setSearchValue] = useState('');
|
2025-11-05 17:20:58 +08:00
|
|
|
|
const [createOpen, setCreateOpen] = useState(false);
|
|
|
|
|
|
const [editOpen, setEditOpen] = useState(false);
|
|
|
|
|
|
const [editTarget, setEditTarget] = useState<any>(null);
|
2025-11-04 18:32:51 +08:00
|
|
|
|
const {
|
|
|
|
|
|
agents,
|
|
|
|
|
|
total,
|
|
|
|
|
|
loading,
|
|
|
|
|
|
currentPage,
|
|
|
|
|
|
pageSize,
|
|
|
|
|
|
setCurrentPage,
|
|
|
|
|
|
setKeywords,
|
|
|
|
|
|
refresh,
|
|
|
|
|
|
} = useAgentList({ page: 1, page_size: 10 });
|
|
|
|
|
|
|
2025-11-05 17:20:58 +08:00
|
|
|
|
const { t } = useTranslation();
|
2025-11-06 16:53:47 +08:00
|
|
|
|
const navigate = useNavigate();
|
2025-11-05 17:20:58 +08:00
|
|
|
|
const dialog = useDialog();
|
|
|
|
|
|
const ops = useAgentOperations();
|
|
|
|
|
|
|
2025-11-04 18:32:51 +08:00
|
|
|
|
const totalPages = useMemo(() => {
|
|
|
|
|
|
return Math.ceil((agents?.length || 0) / pageSize) || 1;
|
|
|
|
|
|
}, [agents, pageSize]);
|
|
|
|
|
|
|
|
|
|
|
|
const currentPageData = useMemo(() => {
|
|
|
|
|
|
const startIndex = (currentPage - 1) * pageSize;
|
|
|
|
|
|
const endIndex = startIndex + pageSize;
|
|
|
|
|
|
return (agents || []).slice(startIndex, endIndex);
|
|
|
|
|
|
}, [agents, currentPage, pageSize]);
|
|
|
|
|
|
|
2025-11-05 17:20:58 +08:00
|
|
|
|
const handleSearch = useCallback((value: string) => {
|
|
|
|
|
|
// 仅更新输入值,实际搜索在 500ms 防抖后触发
|
|
|
|
|
|
setSearchValue(value);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
// 500ms 防抖:在用户停止输入 500ms 后触发搜索
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const handler = setTimeout(() => {
|
|
|
|
|
|
setKeywords(searchValue);
|
|
|
|
|
|
setCurrentPage(1);
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
return () => clearTimeout(handler);
|
2025-11-04 18:32:51 +08:00
|
|
|
|
}, [searchValue, setKeywords, setCurrentPage]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Box sx={{ p: 3 }}>
|
2025-11-05 17:20:58 +08:00
|
|
|
|
{/* 页面标题 */}
|
|
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 3 }}>
|
|
|
|
|
|
<Typography variant="h4" fontWeight={600}>
|
|
|
|
|
|
{t('agent.agentList')}
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="contained"
|
|
|
|
|
|
startIcon={<AddIcon />}
|
|
|
|
|
|
onClick={() => setCreateOpen(true)}
|
|
|
|
|
|
sx={{ borderRadius: 2 }}
|
|
|
|
|
|
>
|
|
|
|
|
|
{t('agent.createAgent')}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Box>
|
2025-11-04 18:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
<Paper sx={{ p: 2, mb: 2 }}>
|
|
|
|
|
|
<Box display="flex" gap={2} alignItems="center">
|
|
|
|
|
|
<TextField
|
|
|
|
|
|
value={searchValue}
|
2025-11-05 17:20:58 +08:00
|
|
|
|
onChange={(e) => handleSearch(e.target.value)}
|
2025-11-04 18:32:51 +08:00
|
|
|
|
placeholder="搜索名称或描述"
|
|
|
|
|
|
size="small"
|
|
|
|
|
|
InputProps={{
|
|
|
|
|
|
startAdornment: (
|
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
|
<SearchIcon />
|
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
|
)
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Button variant="outlined" startIcon={<RefreshIcon />} onClick={refresh}>刷新</Button>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
</Paper>
|
|
|
|
|
|
|
|
|
|
|
|
<AgentGridView
|
|
|
|
|
|
agents={currentPageData}
|
|
|
|
|
|
loading={loading}
|
|
|
|
|
|
searchTerm={searchValue}
|
2025-11-05 17:20:58 +08:00
|
|
|
|
onCreateAgent={() => setCreateOpen(true)}
|
|
|
|
|
|
onEdit={(agent) => { setEditTarget(agent); setEditOpen(true); }}
|
2025-11-06 16:53:47 +08:00
|
|
|
|
onView={(agent) => {
|
|
|
|
|
|
navigate(`/agent/${agent.id}`);
|
|
|
|
|
|
}}
|
2025-11-05 17:20:58 +08:00
|
|
|
|
onDelete={async (agent) => {
|
|
|
|
|
|
const confirmed = await dialog.confirm({
|
|
|
|
|
|
title: t('dialog.confirmDelete'),
|
|
|
|
|
|
content: t('dialog.confirmDeleteMessage'),
|
|
|
|
|
|
confirmText: t('dialog.delete'),
|
|
|
|
|
|
cancelText: t('dialog.cancel'),
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!confirmed) return;
|
|
|
|
|
|
const res = await ops.deleteAgents([agent.id]);
|
|
|
|
|
|
if (res.success) refresh();
|
|
|
|
|
|
}}
|
2025-11-04 18:32:51 +08:00
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
{totalPages >= 1 && (
|
|
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'center', mt: 4 }}>
|
|
|
|
|
|
<Stack spacing={2}>
|
|
|
|
|
|
<Pagination
|
|
|
|
|
|
count={totalPages}
|
|
|
|
|
|
page={currentPage}
|
|
|
|
|
|
onChange={(_, page) => setCurrentPage(page)}
|
|
|
|
|
|
color="primary"
|
|
|
|
|
|
size="large"
|
|
|
|
|
|
showFirstButton
|
|
|
|
|
|
showLastButton
|
|
|
|
|
|
/>
|
|
|
|
|
|
<Typography variant="body2" color="text.secondary" textAlign="center">
|
|
|
|
|
|
共 {total} 个,当前第 {currentPage} / {totalPages} 页
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
</Stack>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
)}
|
2025-11-05 17:20:58 +08:00
|
|
|
|
<CreateAgentDialog
|
|
|
|
|
|
open={createOpen}
|
|
|
|
|
|
onClose={() => setCreateOpen(false)}
|
|
|
|
|
|
onCreated={() => {
|
|
|
|
|
|
setCreateOpen(false);
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<EditAgentDialog
|
|
|
|
|
|
open={editOpen}
|
|
|
|
|
|
agent={editTarget}
|
|
|
|
|
|
onClose={() => { setEditOpen(false); setEditTarget(null); }}
|
|
|
|
|
|
onSaved={() => { setEditOpen(false); setEditTarget(null); refresh(); }}
|
|
|
|
|
|
/>
|
2025-11-04 18:32:51 +08:00
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default AgentListPage;
|