import { Container, Box, Alert, Snackbar, Button, Stack, Dialog, DialogTitle, DialogContent, DialogActions, Typography, TextField, TableContainer, Table, TableHead, TableRow, TableCell, TableBody, } from "@mui/material"; import { useEffect, useState } from "react"; import { useRouter } from "next/router"; import config from "../next.config"; const prefix = config.basePath ? config.basePath : ""; export default function Time(props) { const [ranges, setRanges] = useState([]); const [range, setRange] = useState(""); const [newName, setNewName] = useState(""); const [snackbarError, setSnackbarError] = useState(false); const [snackbarErrorMessage, setSnackbarErrorMessage] = useState(""); const [snackbarSuccess, setSnackbarSuccess] = useState(false); const [limit, setLimit] = useState(1); const [inputedLimit, setInputedLimit] = useState(1); const [token, setToken] = useState(""); const [stats, setStats] = useState({}); const [modifyTime, setModifyTime] = useState({}); const router = useRouter(); const modifyRange = () => { fetch(`${prefix}/api/time/ranges/${modifyTime.id}`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token, ...modifyTime }), }) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setModifyTime({}); setSnackbarSuccess(true); refreshRanges(); } }); }; const getStats = () => { fetch(`${prefix}/api/stats`, { method: "GET", }) .then((res) => res.json()) .then((res) => { setStats(res); }); }; const setStarted = (started) => { fetch(`${prefix}/api/time/started`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token, started }), }) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setSnackbarSuccess(true); } }); }; const isAdmin = () => { if (props.username === "admin") { return true; } else { return false; } }; const getLimit = () => { fetch(`${prefix}/api/time/limit`, { method: "GET", headers: { "Content-Type": "application/json", }, }) .then((res) => res.json()) .then((res) => { setLimit(res.limit); }); }; const addRange = () => { fetch(`${prefix}/api/time/ranges`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: newName, range, token, }), }).then((res) => res.json().then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setSnackbarSuccess(true); refreshRanges(); } }) ); }; const refreshRanges = () => { getLimit(); getStats(); fetch(`${prefix}/api/time/ranges`) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setRanges(res); } }); }; const deleteRange = (id) => { fetch(`${prefix}/api/time/ranges/${id}`, { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token }), }) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setSnackbarSuccess(true); refreshRanges(); } }); }; const updateUsername = (id, username) => { fetch(`${prefix}/api/time/ranges/${id}`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username }), }) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); refreshRanges(); } else { setSnackbarSuccess(true); refreshRanges(); } }); }; const updateLimit = (limit) => { fetch(`${prefix}/api/time/limit`, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ limit, token }), }) .then((res) => res.json()) .then((res) => { if (res.error) { setSnackbarError(true); setSnackbarErrorMessage(res.error); } else { setSnackbarSuccess(true); refreshRanges(); } }); }; useEffect(() => { if (!props.username) { router.push("/"); } }); useEffect(() => { refreshRanges(); const interval = setInterval(() => { refreshRanges(); }, 1000); return () => clearInterval(interval); }, []); /* useEffect(() => { refreshRanges(); }, []); */ return ( {isAdmin() && ( setToken(e.target.value)} /> setNewName(e.target.value)} /> setRange(e.target.value)} placeholder="2022-01-01 00:00:00" /> setInputedLimit(e.target.value)} /> )} 当前每人数量上限: {limit}
服务器负载 (QPS): {stats.apiqps}
名称 时间段 操作 {ranges.map((range) => ( {range.name} {range.range} {isAdmin() && ( <> )} ))}
setSnackbarError(false)} > setSnackbarError(false)} severity="error" > {snackbarErrorMessage} setSnackbarSuccess(false)} > setSnackbarSuccess(false)} severity="success" > 操作成功! setModifyTime({})}> 修改时间段 {modifyTime.id} setModifyTime({ ...modifyTime, name: e.target.value }) } /> setModifyTime({ ...modifyTime, range: e.target.value }) } placeholder="2022-01-01 00:00:00" /> setModifyTime({ ...modifyTime, username: e.target.value }) } />
); }