feat(dashboard): add code refactors
This commit is contained in:
6 files changed
+61
-16
No files matched your search
@@ -1,4 +1,8 @@
|
|||||||
import { dashboardCardKeys, type DashboardCardKey } from "@/Types/Dashboard";
|
import {
|
||||||
|
dashboardCardKeys,
|
||||||
|
type DashboardCardKey,
|
||||||
|
type DashboardSortOrder,
|
||||||
|
} from "@/Types/Dashboard";
|
||||||
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
|
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
type ThemeMode = "light" | "dark";
|
type ThemeMode = "light" | "dark";
|
||||||
@@ -38,6 +42,7 @@ interface UIState {
|
|||||||
starPromptOpen: boolean;
|
starPromptOpen: boolean;
|
||||||
chartType: ChartType;
|
chartType: ChartType;
|
||||||
dashboardVisibleCards: DashboardCardKey[];
|
dashboardVisibleCards: DashboardCardKey[];
|
||||||
|
dashboardUptimeSortOrder: DashboardSortOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialMode: ThemeMode = window?.matchMedia?.("(prefers-color-scheme: dark)")
|
const initialMode: ThemeMode = window?.matchMedia?.("(prefers-color-scheme: dark)")
|
||||||
@@ -78,6 +83,7 @@ const initialState: UIState = {
|
|||||||
starPromptOpen: true,
|
starPromptOpen: true,
|
||||||
chartType: "histogram",
|
chartType: "histogram",
|
||||||
dashboardVisibleCards: [...dashboardCardKeys],
|
dashboardVisibleCards: [...dashboardCardKeys],
|
||||||
|
dashboardUptimeSortOrder: "ascending",
|
||||||
};
|
};
|
||||||
|
|
||||||
const uiSlice = createSlice({
|
const uiSlice = createSlice({
|
||||||
@@ -124,6 +130,9 @@ const uiSlice = createSlice({
|
|||||||
setDashboardVisibleCards: (state, action: PayloadAction<DashboardCardKey[]>) => {
|
setDashboardVisibleCards: (state, action: PayloadAction<DashboardCardKey[]>) => {
|
||||||
state.dashboardVisibleCards = action.payload;
|
state.dashboardVisibleCards = action.payload;
|
||||||
},
|
},
|
||||||
|
setDashboardUptimeSortOrder: (state, action: PayloadAction<DashboardSortOrder>) => {
|
||||||
|
state.dashboardUptimeSortOrder = action.payload;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -141,4 +150,5 @@ export const {
|
|||||||
setStarPromptOpen,
|
setStarPromptOpen,
|
||||||
setChartType,
|
setChartType,
|
||||||
setDashboardVisibleCards,
|
setDashboardVisibleCards,
|
||||||
|
setDashboardUptimeSortOrder,
|
||||||
} = uiSlice.actions;
|
} = uiSlice.actions;
|
||||||
@@ -4,9 +4,8 @@ import { useTheme } from "@mui/material/styles";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { DashboardCard } from "@/Pages/Dashboard/components/DashboardCard";
|
import { DashboardCard } from "@/Pages/Dashboard/components/DashboardCard";
|
||||||
import { Dot } from "@/Components/design-elements";
|
import { Dot } from "@/Components/design-elements";
|
||||||
import { getStatusColor } from "@/Utils/MonitorUtils";
|
|
||||||
import { typographyLevels } from "@/Utils/Theme/Palette";
|
import { typographyLevels } from "@/Utils/Theme/Palette";
|
||||||
import { LAYOUT, SPACING } from "@/Utils/Theme/constants";
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
||||||
import type { Monitor } from "@/Types/Monitor";
|
import type { Monitor } from "@/Types/Monitor";
|
||||||
|
|
||||||
interface CurrentlyDownCardProps {
|
interface CurrentlyDownCardProps {
|
||||||
@@ -32,7 +31,7 @@ export const CurrentlyDownCard = ({ monitors }: CurrentlyDownCardProps) => {
|
|||||||
{t("pages.dashboard.currentlyDown.empty")}
|
{t("pages.dashboard.currentlyDown.empty")}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
<Stack gap={theme.spacing(SPACING.SM)}>
|
<Stack gap={theme.spacing(LAYOUT.SM)}>
|
||||||
{downMonitors.map((monitor) => (
|
{downMonitors.map((monitor) => (
|
||||||
<Stack
|
<Stack
|
||||||
key={monitor.id}
|
key={monitor.id}
|
||||||
@@ -40,7 +39,7 @@ export const CurrentlyDownCard = ({ monitors }: CurrentlyDownCardProps) => {
|
|||||||
alignItems="center"
|
alignItems="center"
|
||||||
gap={theme.spacing(LAYOUT.XS)}
|
gap={theme.spacing(LAYOUT.XS)}
|
||||||
>
|
>
|
||||||
<Dot color={getStatusColor(monitor.status, theme)} />
|
<Dot color={theme.palette.error.main} />
|
||||||
<Typography
|
<Typography
|
||||||
fontSize={typographyLevels.m}
|
fontSize={typographyLevels.m}
|
||||||
color={theme.palette.text.primary}
|
color={theme.palette.text.primary}
|
||||||
|
|||||||
+37
-7
@@ -1,36 +1,66 @@
|
|||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
import { ArrowDown, ArrowUp } from "lucide-react";
|
||||||
|
import { Icon } from "@/Components/design-elements";
|
||||||
import { DashboardCard } from "@/Pages/Dashboard/components/DashboardCard";
|
import { DashboardCard } from "@/Pages/Dashboard/components/DashboardCard";
|
||||||
import { BarRow } from "@/Pages/Dashboard/components/cards/BarRow";
|
import { BarRow } from "@/Pages/Dashboard/components/cards/BarRow";
|
||||||
import { getUptimePercentageColor } from "@/Utils/MonitorUtils";
|
import { getUptimePercentageColor } from "@/Utils/MonitorUtils";
|
||||||
import { formatPercentage } from "@/Utils/FormatUtils";
|
import { formatPercentage } from "@/Utils/FormatUtils";
|
||||||
import { typographyLevels } from "@/Utils/Theme/Palette";
|
import { typographyLevels } from "@/Utils/Theme/Palette";
|
||||||
import { SPACING } from "@/Utils/Theme/constants";
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
||||||
|
import { setDashboardUptimeSortOrder } from "@/Features/UI/uiSlice";
|
||||||
import type { Monitor } from "@/Types/Monitor";
|
import type { Monitor } from "@/Types/Monitor";
|
||||||
|
import type { AppDispatch, RootState } from "@/store";
|
||||||
|
|
||||||
const TOP_N = 5;
|
const TOP_N = 5;
|
||||||
|
|
||||||
interface LowestUptimeCardProps {
|
interface UptimeCardProps {
|
||||||
monitors: Monitor[];
|
monitors: Monitor[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const LowestUptimeCard = ({ monitors }: LowestUptimeCardProps) => {
|
export const UptimeCard = ({ monitors }: UptimeCardProps) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const dispatch = useDispatch<AppDispatch>();
|
||||||
|
const sortOrder = useSelector((state: RootState) => state.ui.dashboardUptimeSortOrder);
|
||||||
|
|
||||||
|
const direction = sortOrder === "ascending" ? 1 : -1;
|
||||||
const ranked = monitors
|
const ranked = monitors
|
||||||
.filter((monitor) => monitor.status !== "paused")
|
.filter((monitor) => monitor.status !== "paused")
|
||||||
.filter(
|
.filter(
|
||||||
(monitor): monitor is Monitor & { uptimePercentage: number } =>
|
(monitor): monitor is Monitor & { uptimePercentage: number } =>
|
||||||
typeof monitor.uptimePercentage === "number"
|
typeof monitor.uptimePercentage === "number"
|
||||||
)
|
)
|
||||||
.sort((a, b) => a.uptimePercentage - b.uptimePercentage)
|
.sort((a, b) => direction * (a.uptimePercentage - b.uptimePercentage))
|
||||||
.slice(0, TOP_N);
|
.slice(0, TOP_N);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DashboardCard title={t("pages.dashboard.cards.lowestUptime")}>
|
<DashboardCard
|
||||||
|
title={t("pages.dashboard.cards.uptime")}
|
||||||
|
topRight={
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
aria-label={t("pages.dashboard.uptimeCard.toggleSortOrder")}
|
||||||
|
onClick={() =>
|
||||||
|
dispatch(
|
||||||
|
setDashboardUptimeSortOrder(
|
||||||
|
sortOrder === "ascending" ? "descending" : "ascending"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Icon
|
||||||
|
icon={sortOrder === "ascending" ? ArrowUp : ArrowDown}
|
||||||
|
size={16}
|
||||||
|
color={theme.palette.text.secondary}
|
||||||
|
/>
|
||||||
|
</IconButton>
|
||||||
|
}
|
||||||
|
>
|
||||||
{ranked.length === 0 ? (
|
{ranked.length === 0 ? (
|
||||||
<Typography
|
<Typography
|
||||||
fontSize={typographyLevels.m}
|
fontSize={typographyLevels.m}
|
||||||
@@ -39,7 +69,7 @@ export const LowestUptimeCard = ({ monitors }: LowestUptimeCardProps) => {
|
|||||||
{t("pages.dashboard.noData")}
|
{t("pages.dashboard.noData")}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
<Stack gap={theme.spacing(SPACING.SM)}>
|
<Stack gap={theme.spacing(LAYOUT.SM)}>
|
||||||
{ranked.map((monitor) => {
|
{ranked.map((monitor) => {
|
||||||
const paletteKey = getUptimePercentageColor(monitor.uptimePercentage);
|
const paletteKey = getUptimePercentageColor(monitor.uptimePercentage);
|
||||||
const color = theme.palette[paletteKey].main;
|
const color = theme.palette[paletteKey].main;
|
||||||
@@ -48,7 +78,7 @@ export const LowestUptimeCard = ({ monitors }: LowestUptimeCardProps) => {
|
|||||||
key={monitor.id}
|
key={monitor.id}
|
||||||
label={monitor.name}
|
label={monitor.name}
|
||||||
value={formatPercentage(monitor.uptimePercentage)}
|
value={formatPercentage(monitor.uptimePercentage)}
|
||||||
fillRatio={1 - monitor.uptimePercentage}
|
fillRatio={monitor.uptimePercentage}
|
||||||
valueColor={color}
|
valueColor={color}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -15,7 +15,7 @@ import type { RootState, AppDispatch } from "@/store";
|
|||||||
|
|
||||||
import { MonitorStatusCard } from "@/Pages/Dashboard/components/cards/MonitorStatusCard";
|
import { MonitorStatusCard } from "@/Pages/Dashboard/components/cards/MonitorStatusCard";
|
||||||
import { CurrentlyDownCard } from "@/Pages/Dashboard/components/cards/CurrentlyDownCard";
|
import { CurrentlyDownCard } from "@/Pages/Dashboard/components/cards/CurrentlyDownCard";
|
||||||
import { LowestUptimeCard } from "@/Pages/Dashboard/components/cards/LowestUptimeCard";
|
import { UptimeCard } from "@/Pages/Dashboard/components/cards/UptimeCard";
|
||||||
import { MonitorsByTypeCard } from "@/Pages/Dashboard/components/cards/MonitorsByTypeCard";
|
import { MonitorsByTypeCard } from "@/Pages/Dashboard/components/cards/MonitorsByTypeCard";
|
||||||
import { EditCardsModal } from "@/Pages/Dashboard/components/EditCardsModal";
|
import { EditCardsModal } from "@/Pages/Dashboard/components/EditCardsModal";
|
||||||
import { type DashboardCardKey, dashboardCardKeys } from "@/Types/Dashboard";
|
import { type DashboardCardKey, dashboardCardKeys } from "@/Types/Dashboard";
|
||||||
@@ -51,7 +51,7 @@ const Dashboard = () => {
|
|||||||
const cardComponents: Record<DashboardCardKey, ReactNode> = {
|
const cardComponents: Record<DashboardCardKey, ReactNode> = {
|
||||||
monitorStatus: <MonitorStatusCard summary={summary} />,
|
monitorStatus: <MonitorStatusCard summary={summary} />,
|
||||||
currentlyDown: <CurrentlyDownCard monitors={monitors ?? []} />,
|
currentlyDown: <CurrentlyDownCard monitors={monitors ?? []} />,
|
||||||
lowestUptime: <LowestUptimeCard monitors={monitors ?? []} />,
|
uptime: <UptimeCard monitors={monitors ?? []} />,
|
||||||
monitorsByType: <MonitorsByTypeCard monitorsByType={monitorsByType} />,
|
monitorsByType: <MonitorsByTypeCard monitorsByType={monitorsByType} />,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
export const dashboardCardKeys = [
|
export const dashboardCardKeys = [
|
||||||
"monitorStatus",
|
"monitorStatus",
|
||||||
"currentlyDown",
|
"currentlyDown",
|
||||||
"lowestUptime",
|
"uptime",
|
||||||
"monitorsByType",
|
"monitorsByType",
|
||||||
] as const;
|
] as const;
|
||||||
export type DashboardCardKey = (typeof dashboardCardKeys)[number];
|
export type DashboardCardKey = (typeof dashboardCardKeys)[number];
|
||||||
|
|
||||||
|
export const dashboardSortOrders = ["ascending", "descending"] as const;
|
||||||
|
export type DashboardSortOrder = (typeof dashboardSortOrders)[number];
|
||||||
@@ -1925,9 +1925,12 @@
|
|||||||
"cards": {
|
"cards": {
|
||||||
"monitorStatus": "Monitor status",
|
"monitorStatus": "Monitor status",
|
||||||
"currentlyDown": "Currently down",
|
"currentlyDown": "Currently down",
|
||||||
"lowestUptime": "Lowest uptime",
|
"uptime": "Monitor uptime",
|
||||||
"monitorsByType": "Monitors by type"
|
"monitorsByType": "Monitors by type"
|
||||||
},
|
},
|
||||||
|
"uptimeCard": {
|
||||||
|
"toggleSortOrder": "Toggle sort order"
|
||||||
|
},
|
||||||
"currentlyDown": {
|
"currentlyDown": {
|
||||||
"empty": "No monitors are down at this moment."
|
"empty": "No monitors are down at this moment."
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user