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

367 lines
11 KiB
JavaScript
Raw Normal View History

2023-04-23 03:31:00 +00:00
import React, { useEffect, useState } from 'react';
import { Button, Form, Label, Modal, Pagination, Table } from 'semantic-ui-react';
2023-04-23 03:31:00 +00:00
import { Link } from 'react-router-dom';
import { API, copy, showError, showSuccess, showWarning, 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
}
function renderStatus(status) {
switch (status) {
case 1:
return <Label basic color='green'>已启用</Label>;
case 2:
return <Label basic color='red'> 已禁用 </Label>;
case 3:
return <Label basic color='yellow'> 已过期 </Label>;
case 4:
return <Label basic color='grey'> 已耗尽 </Label>;
default:
return <Label basic color='black'> 未知状态 </Label>;
}
}
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);
const [showTopUpModal, setShowTopUpModal] = useState(false);
const [targetTokenIdx, setTargetTokenIdx] = useState(0);
const [redemptionCode, setRedemptionCode] = useState('');
2023-04-23 03:31:00 +00:00
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/?status_only=true', data);
2023-04-23 04:43:10 +00:00
break;
case 'disable':
data.status = 2;
res = await API.put('/api/token/?status_only=true', data);
2023-04-23 04:43:10 +00:00
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
};
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);
};
const topUp = async () => {
if (redemptionCode === '') {
return;
}
const res = await API.post('/api/token/topup/', {
id: tokens[targetTokenIdx].id,
key: redemptionCode
});
const { success, message, data } = res.data;
if (success) {
showSuccess('充值成功!');
let newTokens = [...tokens];
let realIdx = (activePage - 1) * ITEMS_PER_PAGE + targetTokenIdx;
newTokens[realIdx].remain_times += data;
setTokens(newTokens);
setRedemptionCode('');
setShowTopUpModal(false);
} else {
showError(message);
}
}
2023-04-23 03:31:00 +00:00
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={() => {
sortToken('remain_times');
}}
>
剩余次数
</Table.HeaderCell>
2023-04-23 03:31:00 +00:00
<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={() => {
sortToken('expired_time');
}}
>
过期时间
</Table.HeaderCell>
2023-04-23 03:31:00 +00:00
<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>{token.unlimited_times ? '无限制' : token.remain_times}</Table.Cell>
2023-04-23 04:43:10 +00:00
<Table.Cell>{renderTimestamp(token.created_time)}</Table.Cell>
<Table.Cell>{token.expired_time === -1 ? '永不过期' : renderTimestamp(token.expired_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 {
showWarning('无法复制到剪贴板,请手动复制,已将令牌填入搜索框。');
setSearchKeyword(token.key);
2023-04-23 04:43:10 +00:00
}
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'}
color={'yellow'}
onClick={() => {
setTargetTokenIdx(idx);
setShowTopUpModal(true);
}}>
充值
</Button>
2023-04-23 03:31:00 +00:00
<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='8'>
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>
<Modal
onClose={() => setShowTopUpModal(false)}
onOpen={() => setShowTopUpModal(true)}
open={showTopUpModal}
size={'mini'}
>
<Modal.Header>通过兑换码为令牌{tokens[targetTokenIdx]?.name}充值</Modal.Header>
<Modal.Content>
<Modal.Description>
{/*<Image src={status.wechat_qrcode} fluid />*/}
<Form size='large'>
<Form.Input
fluid
placeholder='兑换码'
name='redemptionCode'
value={redemptionCode}
onChange={(e) => {
setRedemptionCode(e.target.value);
}}
/>
<Button color='' fluid size='large' onClick={topUp}>
充值
</Button>
</Form>
</Modal.Description>
</Modal.Content>
</Modal>
2023-04-23 03:31:00 +00:00
</>
);
};
export default TokensTable;