import React, { useEffect, useState } from 'react'; import { Button, Form, Label, Pagination, Popup, Table } from 'semantic-ui-react'; import { Link } from 'react-router-dom'; import { API, showError, showInfo, showSuccess, timestamp2string } from '../helpers'; import { CHANNEL_OPTIONS, ITEMS_PER_PAGE } from '../constants'; function renderTimestamp(timestamp) { return ( <> {timestamp2string(timestamp)} > ); } let type2label = undefined; function renderType(type) { if (!type2label) { type2label = new Map; for (let i = 0; i < CHANNEL_OPTIONS.length; i++) { type2label[CHANNEL_OPTIONS[i].value] = CHANNEL_OPTIONS[i]; } type2label[0] = { value: 0, text: '未知类型', color: 'grey' }; } return ; } const ChannelsTable = () => { const [channels, setChannels] = useState([]); const [loading, setLoading] = useState(true); const [activePage, setActivePage] = useState(1); const [searchKeyword, setSearchKeyword] = useState(''); const [searching, setSearching] = useState(false); const loadChannels = async (startIdx) => { const res = await API.get(`/api/channel/?p=${startIdx}`); const { success, message, data } = res.data; if (success) { if (startIdx === 0) { setChannels(data); } else { let newChannels = channels; newChannels.push(...data); setChannels(newChannels); } } else { showError(message); } setLoading(false); }; const onPaginationChange = (e, { activePage }) => { (async () => { if (activePage === Math.ceil(channels.length / ITEMS_PER_PAGE) + 1) { // In this case we have to load more data and then append them. await loadChannels(activePage - 1); } setActivePage(activePage); })(); }; useEffect(() => { loadChannels(0) .then() .catch((reason) => { showError(reason); }); }, []); const manageChannel = async (id, action, idx) => { let data = { id }; let res; switch (action) { case 'delete': res = await API.delete(`/api/channel/${id}/`); break; case 'enable': data.status = 1; res = await API.put('/api/channel/', data); break; case 'disable': data.status = 2; res = await API.put('/api/channel/', data); break; } const { success, message } = res.data; if (success) { showSuccess('操作成功完成!'); let channel = res.data.data; let newChannels = [...channels]; let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx; if (action === 'delete') { newChannels[realIdx].deleted = true; } else { newChannels[realIdx].status = channel.status; } setChannels(newChannels); } else { showError(message); } }; const renderStatus = (status) => { switch (status) { case 1: return ; case 2: return ( ); default: return ( ); } }; const renderResponseTime = (responseTime) => { let time = responseTime / 1000; time = time.toFixed(2) + " 秒"; if (responseTime === 0) { return ; } else if (responseTime <= 1000) { return ; } else if (responseTime <= 3000) { return ; } else if (responseTime <= 5000) { return ; } else { return ; } }; const searchChannels = async () => { if (searchKeyword === '') { // if keyword is blank, load files instead. await loadChannels(0); setActivePage(1); return; } setSearching(true); const res = await API.get(`/api/channel/search?keyword=${searchKeyword}`); const { success, message, data } = res.data; if (success) { setChannels(data); setActivePage(1); } else { showError(message); } setSearching(false); }; const testChannel = async (id, name, idx) => { const res = await API.get(`/api/channel/test/${id}/`); const { success, message, time } = res.data; if (success) { let newChannels = [...channels]; let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx; newChannels[realIdx].response_time = time * 1000; newChannels[realIdx].test_time = Date.now() / 1000; setChannels(newChannels); showInfo(`通道 ${name} 测试成功,耗时 ${time} 秒。`); } else { showError(message); } }; const testAllChannels = async () => { const res = await API.get(`/api/channel/test`); const { success, message } = res.data; if (success) { showSuccess("已成功开始测试所有已启用通道,请刷新页面查看结果。"); } else { showError(message); } } const handleKeywordChange = async (e, { value }) => { setSearchKeyword(value.trim()); }; const sortChannel = (key) => { if (channels.length === 0) return; setLoading(true); let sortedChannels = [...channels]; sortedChannels.sort((a, b) => { return ('' + a[key]).localeCompare(b[key]); }); if (sortedChannels[0].id === channels[0].id) { sortedChannels.reverse(); } setChannels(sortedChannels); setLoading(false); }; return ( <>