Files
Checkmate/Client/src/Components/TabPanels/Status/ContentPanel.jsx
T

193 lines
5.2 KiB
React
Raw Normal View History

import { useState, useContext, useEffect } from "react";
2024-12-05 10:34:41 -05:00
import { Button, Box, Stack, Typography } from "@mui/material";
2024-12-24 10:18:45 -05:00
import ConfigBox from "../../../Components/ConfigBox";
2024-12-05 10:34:41 -05:00
import { useTheme } from "@emotion/react";
import TabPanel from "@mui/lab/TabPanel";
2024-12-17 11:24:01 -05:00
import { StatusFormContext } from "../../../Pages/Status/CreateStatusContext";
2024-12-27 20:59:03 -05:00
import { useSelector } from "react-redux";
import { logger } from "../../../Utils/Logger"
import { createToast } from "../../../Utils/toastUtils";
import { networkService } from "../../../main";
import ServersList from "./ServersList";
2024-12-09 11:33:30 -05:00
2024-12-30 13:52:22 -05:00
/**
* Content Panel is used to compose the second part of the status page
* for the servers/monitors to watch for in its public page presence and some
* other server related configurations etc
*
*/
2024-12-16 20:18:05 -05:00
const ContentPanel = () => {
2024-12-05 10:34:41 -05:00
const theme = useTheme();
2024-12-24 14:46:54 -05:00
const { form, setForm, errors, setErrors } = useContext(StatusFormContext);
const [cards, setCards] = useState([]);
2024-12-27 20:59:03 -05:00
const {user, authToken } = useSelector((state) => state.auth);
const [monitors, setMonitors] = useState([]);
2024-12-27 20:59:03 -05:00
useEffect(() => {
const fetchMonitors = async () => {
try {
const response = await networkService.getMonitorsByTeamId({
authToken: authToken,
teamId: user.teamId,
2025-01-10 11:06:36 -05:00
limit: null, // donot return any checks for the monitors
types: ["http"], // status page is available only for the uptime type
2024-12-27 20:59:03 -05:00
});
if(response.data.data.monitors.length==0){
setErrors({monitors: "Please config monitors to setup status page"})
}
const fullMonitors = response.data.data.monitors ;
setMonitors(fullMonitors);
2025-01-02 15:30:24 -05:00
if (form.monitors.length > 0) {
const initiCards = form.monitors.map((mid, idx) => ({
id: "" + idx,
val: fullMonitors.filter((fM) =>
mid._id ? fM._id == mid._id : fM._id == mid
)[0],
}));
setCards(initiCards);
}
2024-12-27 20:59:03 -05:00
} catch (error) {
createToast({ body: "Failed to fetch monitors data" });
logger.error("Failed to fetch monitors", error);
}
};
fetchMonitors();
}, [user, authToken]);
2024-12-05 15:57:45 -05:00
const handleAddNew = () => {
2024-12-27 20:59:03 -05:00
if (cards.length === monitors.length) return;
2025-01-02 15:30:24 -05:00
const newCards = [...cards, { id: "" + Math.random(),val:{} }];
2024-12-24 14:46:54 -05:00
setCards(newCards);
2024-12-09 11:33:30 -05:00
};
const removeCard = (id) => {
2024-12-24 14:46:54 -05:00
const newCards = cards.filter((c) => c?.id != id);
setCards(newCards);
2024-12-27 20:59:03 -05:00
setForm({
...form,
monitors: newCards.filter((c) => c.val !== undefined).map((c) => c.val),
});
2024-12-05 10:34:41 -05:00
};
return (
<TabPanel
value="Contents"
sx={{
"& h1, & p, & input": {
color: theme.palette.text.tertiary,
},
}}
>
<Stack
component="form"
className="status-contents-form"
noValidate
spellCheck="false"
gap={theme.spacing(12)}
mt={theme.spacing(6)}
>
<ConfigBox>
<Box>
<Stack gap={theme.spacing(6)}>
<Typography component="h2">Status page servers</Typography>
<Typography component="p">
You can add any number of servers that you monitor to your status page.
You can also reorder them for the best viewing experience.
</Typography>
</Stack>
</Box>
2024-12-05 15:57:45 -05:00
<Box
className="status-contents-server-list"
sx={{
2024-12-17 11:24:01 -05:00
margin: theme.spacing(20),
2024-12-05 10:34:41 -05:00
border: "solid",
borderRadius: theme.shape.borderRadius,
borderColor: theme.palette.border.light,
2024-12-17 11:24:01 -05:00
borderWidth: "1px",
2024-12-05 10:34:41 -05:00
transition: "0.2s",
"&:hover": {
borderColor: theme.palette.primary.main,
backgroundColor: "hsl(215, 87%, 51%, 0.05)",
2024-12-05 15:57:45 -05:00
},
}}
>
2024-12-24 14:46:54 -05:00
<Stack
direction="row"
justifyContent="space-between"
2024-12-24 14:46:54 -05:00
>
<Typography
component="p"
alignSelf={"center"}
>
{" "}
Servers list{" "}
</Typography>
2024-12-27 20:59:03 -05:00
<Button
onClick={handleAddNew}
disabled={monitors.length === 0 || monitors.length <= cards.length}
>
Add new
2024-12-27 20:59:03 -05:00
</Button>
2024-12-24 14:46:54 -05:00
</Stack>
{cards.length > 0 && (
2024-12-05 15:57:45 -05:00
<Stack
2024-12-24 14:46:54 -05:00
id="monitors"
direction="column"
gap={theme.spacing(2)}
2024-12-05 10:34:41 -05:00
>
<ServersList
monitors={monitors}
cards={cards}
setCards={setCards}
form={form}
setForm={setForm}
removeItem={removeCard}
/>
2024-12-05 15:57:45 -05:00
</Stack>
2024-12-24 14:46:54 -05:00
)}
{errors["monitors"] && (
<Typography
component="span"
className="input-error"
color={theme.palette.error.main}
sx={{
opacity: 0.8,
}}
>
{errors["monitors"]}
</Typography>
)}
2024-12-05 10:34:41 -05:00
</Box>
</ConfigBox>
2024-12-24 14:46:54 -05:00
<ConfigBox>
<Box>
<Stack gap={theme.spacing(6)}>
<Typography component="h2">Features</Typography>
<Typography component="p">Show more details on the status page</Typography>
</Stack>
</Box>
2024-12-24 14:46:54 -05:00
{/* <Stack sx={{margin: theme.spacing(20)}}
>
<Checkbox
id="show-barcode"
label={`Show Barcode`}
isChecked={form.showBarcode}
onChange={handleChange}
onBlur={handleBlur}
/>
<Checkbox
id="show-uptime-percentage"
label={`Show Uptime Percentage`}
isChecked={form.showUptimePercentage}
onChange={handleChange}
onBlur={handleBlur}
/>
2024-12-24 14:46:54 -05:00
</Stack> */}
</ConfigBox>
2024-12-05 10:34:41 -05:00
</Stack>
</TabPanel>
);
};
export default ContentPanel;