fix(pagination): 统一表格分页与密钥查询交互
统一各中心表格的分页样式与切换行为,保留密钥筛选条件和兑换码搜索分页。 验证:871 项单测、ESLint、Prettier 和本地浏览器验收通过。
This commit is contained in:
@@ -33,6 +33,8 @@ import { Modal, Pagination } from '@/components/ui';
|
||||
import { ShieldCheck } from 'lucide-react';
|
||||
import Joyride, { STATUS, ACTIONS, EVENTS } from 'react-joyride';
|
||||
import { useTokensData } from '../../hooks/tokens/useTokensData';
|
||||
import { useFilterSearch } from '../../hooks/common/useFilterSearch';
|
||||
import { collectPagedItems } from '../../helpers/paging';
|
||||
import EditTokenModal from '../../components/table/tokens/modals/EditTokenModal';
|
||||
import {
|
||||
API,
|
||||
@@ -484,6 +486,9 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
openFluentNotificationRef.current?.(key),
|
||||
);
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
const [searchResult, setSearchResult] = useState(null);
|
||||
const [searchLoading, setSearchLoading] = useState(false);
|
||||
const searchRequest = useRef(0);
|
||||
const [showKeys, setShowKeys] = useState({});
|
||||
const [modelsSheetVisible, setModelsSheetVisible] = useState(false);
|
||||
const [selectedToken, setSelectedToken] = useState(null);
|
||||
@@ -498,6 +503,7 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
activePage,
|
||||
tokenCount,
|
||||
pageSize,
|
||||
loadTokens,
|
||||
showEdit,
|
||||
editingToken,
|
||||
setEditingToken,
|
||||
@@ -702,7 +708,7 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
okType: 'danger',
|
||||
onOk: async () => {
|
||||
await manageToken(token.id, 'delete', token);
|
||||
refresh();
|
||||
refreshKeyList();
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -725,15 +731,80 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
if (!handled) showInfo(t('暂无可用客服'));
|
||||
};
|
||||
|
||||
// Filter tokens(本页原本即输即筛;“搜索”/回车只是立即应用当前输入,不发请求)
|
||||
const filteredTokens = tokens.filter(
|
||||
(token) =>
|
||||
!searchValue ||
|
||||
token.name?.toLowerCase().includes(searchValue.toLowerCase()) ||
|
||||
token.key?.toLowerCase().includes(searchValue.toLowerCase()) ||
|
||||
token.group?.toLowerCase().includes(searchValue.toLowerCase()),
|
||||
const displayedPageSize = searchResult?.pageSize ?? pageSize;
|
||||
const listLoading = loading || searchLoading;
|
||||
|
||||
const runSearch = async (value, page = 1) => {
|
||||
const id = ++searchRequest.current;
|
||||
const query = value.trim().toLowerCase();
|
||||
if (!query) {
|
||||
setSearchLoading(false);
|
||||
setSearchResult(null);
|
||||
return loadTokens(1, displayedPageSize);
|
||||
}
|
||||
setSearchLoading(true);
|
||||
try {
|
||||
const { items, total, message, truncated } = await collectPagedItems(
|
||||
(p, size) => API.get('/api/token/', { params: { p, page_size: size } }),
|
||||
{ shouldAbort: () => id !== searchRequest.current },
|
||||
);
|
||||
if (id !== searchRequest.current) return;
|
||||
if (message || truncated || items.length < total) {
|
||||
showError(message || originalT('加载失败'));
|
||||
return;
|
||||
}
|
||||
const matches = items.filter((token) =>
|
||||
[token.name, token.key, token.group].some((value) =>
|
||||
value?.toLowerCase().includes(query),
|
||||
),
|
||||
);
|
||||
setSearchResult({
|
||||
query,
|
||||
items: matches,
|
||||
page: Math.min(
|
||||
page,
|
||||
Math.max(1, Math.ceil(matches.length / displayedPageSize)),
|
||||
),
|
||||
pageSize: displayedPageSize,
|
||||
});
|
||||
} catch (error) {
|
||||
if (id === searchRequest.current && !error?.isAxiosError) {
|
||||
showError(originalT('加载失败'));
|
||||
}
|
||||
} finally {
|
||||
if (id === searchRequest.current) setSearchLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const { scheduleSearch, submitSearch, cancelPendingSearch } = useFilterSearch(
|
||||
(value = searchValue) => runSearch(value),
|
||||
);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
searchRequest.current += 1;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const resetSearch = () => {
|
||||
cancelPendingSearch();
|
||||
setSearchValue('');
|
||||
runSearch('');
|
||||
};
|
||||
|
||||
const refreshKeyList = () =>
|
||||
searchResult ? runSearch(searchResult.query, searchResult.page) : refresh();
|
||||
|
||||
const displayedPage = searchResult?.page ?? activePage;
|
||||
const displayedTotal = searchResult?.items.length ?? tokenCount;
|
||||
const displayedTokens = searchResult
|
||||
? searchResult.items.slice(
|
||||
(displayedPage - 1) * displayedPageSize,
|
||||
displayedPage * displayedPageSize,
|
||||
)
|
||||
: tokens;
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* 新手引导 */}
|
||||
@@ -880,8 +951,9 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
<div className='km-panels'>
|
||||
<ConsoleCollectionSearch className='km-card km-search-card tour-search-input'>
|
||||
<ConsoleFilterGrid
|
||||
onReset={() => setSearchValue('')}
|
||||
onSearch={() => setSearchValue(searchValue.trim())}
|
||||
onReset={resetSearch}
|
||||
onSearch={submitSearch}
|
||||
loading={listLoading}
|
||||
>
|
||||
<ConsoleFilterField label={t('密钥查询')} id='km-search-input'>
|
||||
<Input
|
||||
@@ -889,7 +961,14 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
prefix={<ActionSearch size={14} />}
|
||||
placeholder={t('搜索密钥名称、Key 或分组')}
|
||||
value={searchValue}
|
||||
onChange={(event) => setSearchValue(event.target.value)}
|
||||
onChange={(event) => {
|
||||
const value = event.target.value;
|
||||
searchRequest.current += 1;
|
||||
setSearchLoading(false);
|
||||
setSearchValue(value);
|
||||
scheduleSearch(value);
|
||||
}}
|
||||
autoComplete='off'
|
||||
allowClear
|
||||
/>
|
||||
</ConsoleFilterField>
|
||||
@@ -928,8 +1007,8 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
</div>
|
||||
|
||||
<KeyTable
|
||||
tokens={filteredTokens}
|
||||
loading={loading}
|
||||
tokens={displayedTokens}
|
||||
loading={listLoading}
|
||||
visibleColumns={visibleColumns}
|
||||
emptyText={<TableEmpty />}
|
||||
onCopy={copyText}
|
||||
@@ -942,17 +1021,28 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
t={t}
|
||||
/>
|
||||
|
||||
{tokenCount > 0 ? (
|
||||
{displayedTotal > 0 ? (
|
||||
<Pagination
|
||||
className='km-pagination'
|
||||
size='small'
|
||||
total={tokenCount}
|
||||
currentPage={activePage}
|
||||
pageSize={pageSize}
|
||||
showSizeChanger
|
||||
showTotal={(total) => t('共 {{total}} 条', { total })}
|
||||
onPageChange={handlePageChange}
|
||||
onPageSizeChange={handlePageSizeChange}
|
||||
total={displayedTotal}
|
||||
currentPage={displayedPage}
|
||||
pageSize={displayedPageSize}
|
||||
loading={listLoading}
|
||||
onPageChange={
|
||||
searchResult
|
||||
? (page) => setSearchResult({ ...searchResult, page })
|
||||
: handlePageChange
|
||||
}
|
||||
onPageSizeChange={
|
||||
searchResult
|
||||
? (pageSize) =>
|
||||
setSearchResult({
|
||||
...searchResult,
|
||||
page: 1,
|
||||
pageSize,
|
||||
})
|
||||
: handlePageSizeChange
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
</Card>
|
||||
@@ -961,7 +1051,7 @@ const KeyManagement = ({ embedded = false }) => {
|
||||
|
||||
{/* 创建/编辑抽屉 */}
|
||||
<EditTokenModal
|
||||
refresh={refresh}
|
||||
refresh={refreshKeyList}
|
||||
editingToken={editingToken}
|
||||
visiable={showEdit}
|
||||
handleClose={closeEdit}
|
||||
|
||||
Reference in New Issue
Block a user