WebhooksTable.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react';
  3. import { Link } from 'react-router-dom';
  4. import { API, copy, showError, showSuccess, showWarning } from '../helpers';
  5. import { ITEMS_PER_PAGE } from '../constants';
  6. import { renderTimestamp } from '../helpers/render';
  7. const WebhooksTable = () => {
  8. const [webhooks, setWebhooks] = useState([]);
  9. const [loading, setLoading] = useState(true);
  10. const [activePage, setActivePage] = useState(1);
  11. const [searchKeyword, setSearchKeyword] = useState('');
  12. const [searching, setSearching] = useState(false);
  13. const [user, setUser] = useState({ username: '', token: '' });
  14. const loadWebhooks = async (startIdx) => {
  15. const res = await API.get(`/api/webhook/?p=${startIdx}`);
  16. const { success, message, data } = res.data;
  17. if (success) {
  18. if (startIdx === 0) {
  19. setWebhooks(data);
  20. } else {
  21. let newWebhooks = webhooks;
  22. newWebhooks.push(...data);
  23. setWebhooks(newWebhooks);
  24. }
  25. } else {
  26. showError(message);
  27. }
  28. setLoading(false);
  29. };
  30. const onPaginationChange = (e, { activePage }) => {
  31. (async () => {
  32. if (activePage === Math.ceil(webhooks.length / ITEMS_PER_PAGE) + 1) {
  33. // In this case we have to load more data and then append them.
  34. await loadWebhooks(activePage - 1);
  35. }
  36. setActivePage(activePage);
  37. })();
  38. };
  39. useEffect(() => {
  40. loadWebhooks(0)
  41. .then()
  42. .catch((reason) => {
  43. showError(reason);
  44. });
  45. loadUser()
  46. .then()
  47. .catch((reason) => {
  48. showError(reason);
  49. });
  50. }, []);
  51. const manageWebhook = async (id, action, idx) => {
  52. let data = { id };
  53. let res;
  54. switch (action) {
  55. case 'delete':
  56. res = await API.delete(`/api/webhook/${id}/`);
  57. break;
  58. case 'enable':
  59. data.status = 1;
  60. res = await API.put('/api/webhook/?status_only=true', data);
  61. break;
  62. case 'disable':
  63. data.status = 2;
  64. res = await API.put('/api/webhook/?status_only=true', data);
  65. break;
  66. }
  67. const { success, message } = res.data;
  68. if (success) {
  69. showSuccess('操作成功完成!');
  70. let webhook = res.data.data;
  71. let newWebhooks = [...webhooks];
  72. let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx;
  73. if (action === 'delete') {
  74. newWebhooks[realIdx].deleted = true;
  75. } else {
  76. newWebhooks[realIdx].status = webhook.status;
  77. }
  78. setWebhooks(newWebhooks);
  79. } else {
  80. showError(message);
  81. }
  82. };
  83. const renderStatus = (status) => {
  84. switch (status) {
  85. case 1:
  86. return <Label basic>已启用</Label>;
  87. case 2:
  88. return (
  89. <Label basic color='red'>
  90. 已禁用
  91. </Label>
  92. );
  93. default:
  94. return (
  95. <Label basic color='grey'>
  96. 未知状态
  97. </Label>
  98. );
  99. }
  100. };
  101. const searchWebhooks = async () => {
  102. if (searchKeyword === '') {
  103. // if keyword is blank, load files instead.
  104. await loadWebhooks(0);
  105. setActivePage(1);
  106. return;
  107. }
  108. setSearching(true);
  109. const res = await API.get(`/api/webhook/search?keyword=${searchKeyword}`);
  110. const { success, message, data } = res.data;
  111. if (success) {
  112. setWebhooks(data);
  113. setActivePage(1);
  114. } else {
  115. showError(message);
  116. }
  117. setSearching(false);
  118. };
  119. const handleKeywordChange = async (e, { value }) => {
  120. setSearchKeyword(value.trim());
  121. };
  122. const sortWebhook = (key) => {
  123. if (webhooks.length === 0) return;
  124. setLoading(true);
  125. let sortedWebhooks = [...webhooks];
  126. sortedWebhooks.sort((a, b) => {
  127. return ('' + a[key]).localeCompare(b[key]);
  128. });
  129. if (sortedWebhooks[0].id === webhooks[0].id) {
  130. sortedWebhooks.reverse();
  131. }
  132. setWebhooks(sortedWebhooks);
  133. setLoading(false);
  134. };
  135. const loadUser = async () => {
  136. let res = await API.get(`/api/user/self`);
  137. const { success, message, data } = res.data;
  138. if (success) {
  139. setUser(data);
  140. } else {
  141. showError(message);
  142. }
  143. setLoading(false);
  144. };
  145. return (
  146. <>
  147. <Form onSubmit={searchWebhooks}>
  148. <Form.Input
  149. icon='search'
  150. fluid
  151. iconPosition='left'
  152. placeholder='搜索接口的 ID,链接或名称 ...'
  153. value={searchKeyword}
  154. loading={searching}
  155. onChange={handleKeywordChange}
  156. />
  157. </Form>
  158. <Table basic>
  159. <Table.Header>
  160. <Table.Row>
  161. <Table.HeaderCell
  162. style={{ cursor: 'pointer' }}
  163. onClick={() => {
  164. sortWebhook('id');
  165. }}
  166. >
  167. ID
  168. </Table.HeaderCell>
  169. <Table.HeaderCell
  170. style={{ cursor: 'pointer' }}
  171. onClick={() => {
  172. sortWebhook('name');
  173. }}
  174. >
  175. 名称
  176. </Table.HeaderCell>
  177. <Table.HeaderCell
  178. style={{ cursor: 'pointer' }}
  179. onClick={() => {
  180. sortWebhook('status');
  181. }}
  182. >
  183. 状态
  184. </Table.HeaderCell>
  185. <Table.HeaderCell
  186. style={{ cursor: 'pointer' }}
  187. onClick={() => {
  188. sortWebhook('created_time');
  189. }}
  190. >
  191. 创建时间
  192. </Table.HeaderCell>
  193. <Table.HeaderCell>操作</Table.HeaderCell>
  194. </Table.Row>
  195. </Table.Header>
  196. <Table.Body>
  197. {webhooks
  198. .slice(
  199. (activePage - 1) * ITEMS_PER_PAGE,
  200. activePage * ITEMS_PER_PAGE
  201. )
  202. .map((webhook, idx) => {
  203. if (webhook.deleted) return <></>;
  204. return (
  205. <Table.Row key={webhook.id}>
  206. <Table.Cell>{webhook.id}</Table.Cell>
  207. <Table.Cell>{webhook.name}</Table.Cell>
  208. <Table.Cell>{renderStatus(webhook.status)}</Table.Cell>
  209. <Table.Cell>
  210. {renderTimestamp(webhook.created_time)}
  211. </Table.Cell>
  212. <Table.Cell>
  213. <div>
  214. <Button
  215. size={'small'}
  216. positive
  217. onClick={async () => {
  218. if (
  219. await copy(
  220. `${window.location.origin}/webhook/${webhook.link}`
  221. )
  222. ) {
  223. showSuccess('已复制到剪贴板!');
  224. } else {
  225. showWarning('无法复制到剪贴板!');
  226. }
  227. }}
  228. >
  229. 复制 Webhook 链接
  230. </Button>
  231. <Button
  232. size={'small'}
  233. negative
  234. onClick={() => {
  235. manageWebhook(webhook.id, 'delete', idx).then();
  236. }}
  237. >
  238. 删除
  239. </Button>
  240. <Button
  241. size={'small'}
  242. onClick={() => {
  243. manageWebhook(
  244. webhook.id,
  245. webhook.status === 1 ? 'disable' : 'enable',
  246. idx
  247. ).then();
  248. }}
  249. >
  250. {webhook.status === 1 ? '禁用' : '启用'}
  251. </Button>
  252. <Button
  253. size={'small'}
  254. as={Link}
  255. to={'/webhook/edit/' + webhook.id}
  256. >
  257. 编辑
  258. </Button>
  259. </div>
  260. </Table.Cell>
  261. </Table.Row>
  262. );
  263. })}
  264. </Table.Body>
  265. <Table.Footer>
  266. <Table.Row>
  267. <Table.HeaderCell colSpan='7'>
  268. <Button
  269. size='small'
  270. as={Link}
  271. to='/webhook/add'
  272. loading={loading}
  273. >
  274. 添加新的接口
  275. </Button>
  276. <Pagination
  277. floated='right'
  278. activePage={activePage}
  279. onPageChange={onPaginationChange}
  280. size='small'
  281. siblingRange={1}
  282. totalPages={
  283. Math.ceil(webhooks.length / ITEMS_PER_PAGE) +
  284. (webhooks.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
  285. }
  286. />
  287. </Table.HeaderCell>
  288. </Table.Row>
  289. </Table.Footer>
  290. </Table>
  291. </>
  292. );
  293. };
  294. export default WebhooksTable;