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