ai-gateway/web/src/components/TokensTable.js

299 lines
8.4 KiB
JavaScript
Raw Normal View History

2023-04-23 03:31:00 +00:00
import React, { useEffect, useState } from 'react';
import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react';
import { Link } from 'react-router-dom';
2023-04-23 04:43:10 +00:00
import { API, copy, showError, showSuccess, timestamp2string } from '../helpers';
2023-04-23 03:31:00 +00:00
import { ITEMS_PER_PAGE } from '../constants';
2023-04-23 04:43:10 +00:00
function renderTimestamp(timestamp) {
return (
<>
{timestamp2string(timestamp)}
</>
);
2023-04-23 03:31:00 +00:00
}
const TokensTable = () => {
2023-04-23 04:43:10 +00:00
const [tokens, setTokens] = useState([]);
2023-04-23 03:31:00 +00:00
const [loading, setLoading] = useState(true);
const [activePage, setActivePage] = useState(1);
const [searchKeyword, setSearchKeyword] = useState('');
const [searching, setSearching] = useState(false);
2023-04-23 04:43:10 +00:00
const loadTokens = async (startIdx) => {
const res = await API.get(`/api/token/?p=${startIdx}`);
2023-04-23 03:31:00 +00:00
const { success, message, data } = res.data;
if (success) {
if (startIdx === 0) {
2023-04-23 04:43:10 +00:00
setTokens(data);
2023-04-23 03:31:00 +00:00
} else {
2023-04-23 04:43:10 +00:00
let newTokens = tokens;
newTokens.push(...data);
setTokens(newTokens);
2023-04-23 03:31:00 +00:00
}
} else {
showError(message);
}
setLoading(false);
};
const onPaginationChange = (e, { activePage }) => {
(async () => {
2023-04-23 04:43:10 +00:00
if (activePage === Math.ceil(tokens.length / ITEMS_PER_PAGE) + 1) {
2023-04-23 03:31:00 +00:00
// In this case we have to load more data and then append them.
2023-04-23 04:43:10 +00:00
await loadTokens(activePage - 1);
2023-04-23 03:31:00 +00:00
}
setActivePage(activePage);
})();
};
useEffect(() => {
2023-04-23 04:43:10 +00:00
loadTokens(0)
2023-04-23 03:31:00 +00:00
.then()
.catch((reason) => {
showError(reason);
});
}, []);
2023-04-23 04:43:10 +00:00
const manageToken = async (id, action, idx) => {
let data = { id };
let res;
switch (action) {
case 'delete':
res = await API.delete(`/api/token/${id}/`);
break;
case 'enable':
data.status = 1;
res = await API.put('/api/token/', data);
break;
case 'disable':
data.status = 2;
res = await API.put('/api/token/', data);
break;
}
const { success, message } = res.data;
if (success) {
showSuccess('操作成功完成!');
let token = res.data.data;
let newTokens = [...tokens];
let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx;
if (action === 'delete') {
newTokens[realIdx].deleted = true;
2023-04-23 03:31:00 +00:00
} else {
2023-04-23 04:43:10 +00:00
newTokens[realIdx].status = token.status;
2023-04-23 03:31:00 +00:00
}
2023-04-23 04:43:10 +00:00
setTokens(newTokens);
} else {
showError(message);
}
2023-04-23 03:31:00 +00:00
};
const renderStatus = (status) => {
switch (status) {
case 1:
2023-04-23 04:43:10 +00:00
return <Label basic color='green'>已启用</Label>;
2023-04-23 03:31:00 +00:00
case 2:
return (
<Label basic color='red'>
2023-04-23 04:43:10 +00:00
已禁用
2023-04-23 03:31:00 +00:00
</Label>
);
default:
return (
<Label basic color='grey'>
未知状态
</Label>
);
}
};
2023-04-23 04:43:10 +00:00
const searchTokens = async () => {
2023-04-23 03:31:00 +00:00
if (searchKeyword === '') {
// if keyword is blank, load files instead.
2023-04-23 04:43:10 +00:00
await loadTokens(0);
2023-04-23 03:31:00 +00:00
setActivePage(1);
return;
}
setSearching(true);
2023-04-23 07:42:23 +00:00
const res = await API.get(`/api/token/search?keyword=${searchKeyword}`);
2023-04-23 03:31:00 +00:00
const { success, message, data } = res.data;
if (success) {
2023-04-23 04:43:10 +00:00
setTokens(data);
2023-04-23 03:31:00 +00:00
setActivePage(1);
} else {
showError(message);
}
setSearching(false);
};
const handleKeywordChange = async (e, { value }) => {
setSearchKeyword(value.trim());
};
2023-04-23 04:43:10 +00:00
const sortToken = (key) => {
if (tokens.length === 0) return;
2023-04-23 03:31:00 +00:00
setLoading(true);
2023-04-23 04:43:10 +00:00
let sortedTokens = [...tokens];
sortedTokens.sort((a, b) => {
2023-04-23 03:31:00 +00:00
return ('' + a[key]).localeCompare(b[key]);
});
2023-04-23 04:43:10 +00:00
if (sortedTokens[0].id === tokens[0].id) {
sortedTokens.reverse();
2023-04-23 03:31:00 +00:00
}
2023-04-23 04:43:10 +00:00
setTokens(sortedTokens);
2023-04-23 03:31:00 +00:00
setLoading(false);
};
return (
<>
2023-04-23 04:43:10 +00:00
<Form onSubmit={searchTokens}>
2023-04-23 03:31:00 +00:00
<Form.Input
icon='search'
fluid
iconPosition='left'
2023-04-23 04:43:10 +00:00
placeholder='搜索令牌的 ID 和名称 ...'
2023-04-23 03:31:00 +00:00
value={searchKeyword}
loading={searching}
onChange={handleKeywordChange}
/>
</Form>
<Table basic>
<Table.Header>
<Table.Row>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
2023-04-23 04:43:10 +00:00
sortToken('id');
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
ID
2023-04-23 03:31:00 +00:00
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
2023-04-23 04:43:10 +00:00
sortToken('name');
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
名称
2023-04-23 03:31:00 +00:00
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
2023-04-23 04:43:10 +00:00
sortToken('status');
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
状态
2023-04-23 03:31:00 +00:00
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
2023-04-23 04:43:10 +00:00
sortToken('created_time');
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
创建时间
2023-04-23 03:31:00 +00:00
</Table.HeaderCell>
<Table.HeaderCell
style={{ cursor: 'pointer' }}
onClick={() => {
2023-04-23 04:43:10 +00:00
sortToken('accessed_time');
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
访问时间
2023-04-23 03:31:00 +00:00
</Table.HeaderCell>
<Table.HeaderCell>操作</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
2023-04-23 04:43:10 +00:00
{tokens
2023-04-23 03:31:00 +00:00
.slice(
(activePage - 1) * ITEMS_PER_PAGE,
activePage * ITEMS_PER_PAGE
)
2023-04-23 04:43:10 +00:00
.map((token, idx) => {
if (token.deleted) return <></>;
2023-04-23 03:31:00 +00:00
return (
2023-04-23 04:43:10 +00:00
<Table.Row key={token.id}>
<Table.Cell>{token.id}</Table.Cell>
<Table.Cell>{token.name ? token.name : '无'}</Table.Cell>
<Table.Cell>{renderStatus(token.status)}</Table.Cell>
<Table.Cell>{renderTimestamp(token.created_time)}</Table.Cell>
<Table.Cell>{renderTimestamp(token.accessed_time)}</Table.Cell>
2023-04-23 03:31:00 +00:00
<Table.Cell>
<div>
<Button
size={'small'}
positive
2023-04-23 04:43:10 +00:00
onClick={async () => {
if (await copy(token.key)) {
showSuccess('已复制到剪贴板!');
} else {
showError('复制失败!');
}
2023-04-23 03:31:00 +00:00
}}
>
2023-04-23 04:43:10 +00:00
复制
2023-04-23 03:31:00 +00:00
</Button>
<Button
size={'small'}
negative
onClick={() => {
2023-04-23 04:43:10 +00:00
manageToken(token.id, 'delete', idx);
2023-04-23 03:31:00 +00:00
}}
>
删除
</Button>
<Button
size={'small'}
onClick={() => {
2023-04-23 04:43:10 +00:00
manageToken(
token.id,
token.status === 1 ? 'disable' : 'enable',
2023-04-23 03:31:00 +00:00
idx
);
}}
>
2023-04-23 04:43:10 +00:00
{token.status === 1 ? '禁用' : '启用'}
2023-04-23 03:31:00 +00:00
</Button>
<Button
size={'small'}
as={Link}
2023-04-23 04:43:10 +00:00
to={'/token/edit/' + token.id}
2023-04-23 03:31:00 +00:00
>
编辑
</Button>
</div>
</Table.Cell>
</Table.Row>
);
})}
</Table.Body>
<Table.Footer>
<Table.Row>
<Table.HeaderCell colSpan='6'>
2023-04-23 04:43:10 +00:00
<Button size='small' as={Link} to='/token/add' loading={loading}>
添加新的令牌
2023-04-23 03:31:00 +00:00
</Button>
<Pagination
floated='right'
activePage={activePage}
onPageChange={onPaginationChange}
size='small'
siblingRange={1}
totalPages={
2023-04-23 04:43:10 +00:00
Math.ceil(tokens.length / ITEMS_PER_PAGE) +
(tokens.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
2023-04-23 03:31:00 +00:00
}
/>
</Table.HeaderCell>
</Table.Row>
</Table.Footer>
</Table>
</>
);
};
export default TokensTable;