frontPanel/src/pages/Analytics/Devices.tsx

160 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-03-21 14:24:56 +00:00
import { useEffect, useState } from "react";
import { Box, Paper, Typography, useMediaQuery, useTheme } from "@mui/material";
2024-03-20 14:44:51 +00:00
import { PieChart } from "@mui/x-charts";
2024-03-21 14:24:56 +00:00
import { enqueueSnackbar } from "notistack";
2024-03-20 14:44:51 +00:00
2024-03-21 14:24:56 +00:00
import { getDevices } from "@api/statistic";
2024-03-20 14:44:51 +00:00
2024-03-21 14:24:56 +00:00
import type { DevicesResponse } from "@api/statistic";
2024-03-20 14:44:51 +00:00
type DeviceProps = {
title: string;
devices: Record<string, number>;
};
2024-03-21 14:24:56 +00:00
const COLORS: Record<number, string> = {
0: "#7E2AEA",
1: "#FA7738",
2: "#62BB1C",
3: "#0886FB",
};
const DEVICES_MOCK: DevicesResponse = {
2024-03-22 14:29:51 +00:00
device: { PC: 75, Mobile: 25 },
os: { Windows: 44, AndroidOS: 25, "OS X": 19, Linux: 13 },
browser: { Chrome: 75, Firefox: 25 },
2024-03-21 14:24:56 +00:00
};
2024-03-20 14:44:51 +00:00
const Device = ({ title, devices }: DeviceProps) => {
2024-03-21 14:24:56 +00:00
const theme = useTheme();
console.log("devices ", devices);
if (devices === undefined || Object.keys(devices).length === 0)
return <Typography>{title} - нет данных</Typography>;
2024-03-21 14:24:56 +00:00
const data = Object.entries(devices).map(([id, value], index) => ({
id,
value,
color: COLORS[index],
}));
2024-03-20 14:44:51 +00:00
return (
2024-03-21 14:24:56 +00:00
<Paper
sx={{
overflow: "hidden",
minHeight: "500px",
display: "flex",
flexDirection: "column",
gap: "30px",
borderRadius: "12px",
boxShadow: "0 0 20px rgba(0, 0, 0, 0.15)",
}}
>
<Typography sx={{ margin: "20px" }}>{title}</Typography>
<Box sx={{ flexGrow: 1 }}>
<Box sx={{ display: "flex", alignItems: "center" }}>
<PieChart
height={245}
width={245}
margin={{ right: 0 }}
series={[{ data, innerRadius: 50 }]}
/>
</Box>
</Box>
<Box
sx={{ background: theme.palette.background.default, padding: "20px" }}
>
{data.map(({ id, value, color }) => (
<Box
2024-03-22 14:29:51 +00:00
key={id}
2024-03-21 14:24:56 +00:00
sx={{
display: "flex",
marginBottom: "10px",
"&:last-child": { margin: 0 },
}}
>
<Typography
sx={{
flexGrow: 1,
position: "relative",
paddingLeft: "30px",
"&::before": {
content: "''",
display: "block",
position: "absolute",
left: "0",
background: color,
height: "20px",
width: "20px",
borderRadius: "6px",
},
}}
>
{id}
</Typography>
<Typography>{value} %</Typography>
</Box>
))}
2024-03-20 14:44:51 +00:00
</Box>
</Paper>
);
};
export const Devices = ({ data = {} }) => {
2024-03-29 06:10:33 +00:00
const [devices, setDevices] = useState<DevicesResponse>(data);
2024-03-20 14:44:51 +00:00
const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
2024-03-21 14:24:56 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(700));
2024-03-29 06:10:33 +00:00
// useEffect(() => {
// const requestDevices = async () => {
// const [devicesResponse, devicesError] = await getDevices("14761");
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// if (devicesError) {
// enqueueSnackbar(devicesError);
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// return;
// }
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// if (!devicesResponse) {
// enqueueSnackbar("Список девайсов пуст.");
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// return;
// }
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// setDevices(devicesResponse);
// };
2024-03-21 14:24:56 +00:00
2024-03-29 06:10:33 +00:00
// // requestDevices();
// }, []);
2024-03-20 14:44:51 +00:00
return (
2024-03-21 14:24:56 +00:00
<Box sx={{ marginTop: "120px" }}>
<Typography
component="h3"
sx={{
fontSize: "24px",
fontWeight: "bold",
color: theme.palette.text.primary,
}}
>
Статистика пользователей
</Typography>
<Box
sx={{
display: "grid",
gridTemplateColumns: isTablet
? isMobile
? "1fr"
: "1fr 1fr"
: "1fr 1fr 1fr",
gap: "20px",
marginTop: "30px",
}}
>
<Device title="Устройства" devices={devices.device} />
<Device title="Операционные системы" devices={devices.os} />
<Device title="Браузеры" devices={devices.browser} />
</Box>
2024-03-20 14:44:51 +00:00
</Box>
);
};