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

177 lines
4.5 KiB
React
Raw Normal View History

2024-12-16 20:18:05 -05:00
import { useState, useCallback, useContext } 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-09 11:33:30 -05:00
import Card from "./Card";
import update from "immutability-helper";
2024-12-17 11:24:01 -05:00
import { StatusFormContext } from "../../../Pages/Status/CreateStatusContext";
2024-12-09 11:33:30 -05:00
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(form.monitors);
2024-12-09 11:33:30 -05:00
const moveCard = useCallback(
(dragIndex, hoverIndex) => {
const dragCard = cards[dragIndex];
setCards(
update(cards, {
$splice: [
[dragIndex, 1],
[hoverIndex, 0, dragCard],
],
})
);
},
[cards]
);
const handleCardChange = (event) => {
event.preventDefault();
const { value, id } = event.target;
let idx = cards.findIndex((a) => a.id == id);
2024-12-24 14:46:54 -05:00
let newCards;
if (idx >= 0) {
let x = JSON.parse(JSON.stringify(cards[idx]));
x.url = value;
2024-12-24 14:46:54 -05:00
newCards = update(cards, { $splice: [[idx, 1, x]] });
setCards(newCards);
} else {
newCards = update(cards, { $push: [{ id: id, url: value }] });
setCards(newCards);
}
setForm({ ...form, monitors: newCards });
};
2024-12-05 15:57:45 -05:00
const handleAddNew = () => {
2024-12-24 14:46:54 -05:00
const newCards = [...cards, { id: "" + Math.random() }];
setCards(newCards);
setForm({ ...form, monitors: 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);
setForm({ ...form, monitors: newCards });
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-around"
>
<Typography
component="p"
alignSelf={"center"}
>
{" "}
Servers list{" "}
</Typography>
<Button onClick={handleAddNew}>Add New</Button>
</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
>
2024-12-24 14:46:54 -05:00
{cards.map((card, idx) => (
<Card
key={idx}
index={idx}
id={card?.id ?? "" + Math.random()}
text={"" + idx}
moveCard={moveCard}
removeCard={removeCard}
value={card?.url ?? ""}
onChange={handleCardChange}
/>
))}
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;