Skip to content

Commit e919d7e

Browse files
committed
refactor: Move infoKeysDashboard to dashboard component and clean up useDashboardLogic
1 parent a7e734a commit e919d7e

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

frontend/src/pages/dashboard/dashboard.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ import type { GetDashboardStatsResult } from "@/shared/api/dashboard/dashboard";
1717
import { getProtocolDisplayName } from "@/shared/lib/getProtocolDisplayName";
1818
import { ServiceTable } from "../service/serviceTable";
1919

20+
const infoKeysDashboard = [
21+
{ key: "total_services", label: "Total services" },
22+
{ key: "services_up", label: "Services up" },
23+
{ key: "services_down", label: "Services down" },
24+
{ key: "active_incidents", label: "Active incidents" },
25+
{ key: "avg_response_time", label: "Average response time (ms)" },
26+
{ key: "total_checks", label: "Total checks" },
27+
{ key: "uptime_percentage", label: "Uptime" },
28+
{ key: "checks_per_minute", label: "Checks per minute" },
29+
];
30+
2031
const Dashboard = () => {
21-
const { infoKeysDashboard, dashboardInfo, onRefreshDashboard } =
22-
useDashboardLogic();
32+
const { dashboardInfo, onRefreshDashboard } = useDashboardLogic();
2333

2434
if (!dashboardInfo) return <Loader loaderPage />;
2535

@@ -50,7 +60,7 @@ const Dashboard = () => {
5060
const value =
5161
item.key === "uptime_percentage"
5262
? Number(
53-
dashboardInfo[item.key as keyof GetDashboardStatsResult],
63+
dashboardInfo[item.key as keyof GetDashboardStatsResult]
5464
).toFixed(1) + "%"
5565
: item.key === "avg_response_time"
5666
? dashboardInfo[
@@ -77,7 +87,7 @@ const Dashboard = () => {
7787
Object.entries(dashboardInfo.protocols).map(
7888
([protocol, count]) => {
7989
const totalCount = Object.values(
80-
dashboardInfo.protocols!,
90+
dashboardInfo.protocols!
8191
).reduce((a, b) => a + b, 0);
8292
const percentage =
8393
totalCount > 0
@@ -107,7 +117,7 @@ const Dashboard = () => {
107117
</div>
108118
</Card>
109119
);
110-
},
120+
}
111121
)
112122
) : (
113123
<p className="text-muted-foreground text-center">

frontend/src/pages/dashboard/hooks/useDashboardLogic.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { socketUrl } from "@/shared/api/baseApi";
2-
import { useEffect, useMemo } from "react";
2+
import { useEffect } from "react";
33
import useWebSocket from "react-use-websocket";
44
import { useShallow } from "zustand/react/shallow";
55
import { useServiceTableStore } from "@/pages/service/store/useServiceTableStore";
@@ -18,7 +18,7 @@ export const useDashboardLogic = () => {
1818
setUpdateService: s.setUpdateService,
1919
setUpdateAllServices: s.setUpdateAllServices,
2020
addServiceInData: s.addServiceInData,
21-
})),
21+
}))
2222
);
2323

2424
const { getDashboardStats } = getDashboard();
@@ -27,13 +27,23 @@ export const useDashboardLogic = () => {
2727
useShallow((s) => ({
2828
dashboardInfo: s.dashboardInfo,
2929
setDashboardInfo: s.setDashboardInfo,
30-
})),
30+
}))
3131
);
3232

3333
const onRefreshDashboard = async () => {
3434
await getDashboardStats();
3535
};
3636

37+
useEffect(() => {
38+
getDashboardStats().then((res) => {
39+
setDashboardInfo(res);
40+
});
41+
42+
return () => {
43+
setDashboardInfo(null);
44+
};
45+
}, []);
46+
3747
const { lastMessage } = useWebSocket(socketUrl, {
3848
shouldReconnect: () => true,
3949
});
@@ -60,23 +70,8 @@ export const useDashboardLogic = () => {
6070
}
6171
}, [lastMessage]);
6272

63-
const infoKeysDashboard = useMemo(
64-
() => [
65-
{ key: "total_services", label: "Total services" },
66-
{ key: "services_up", label: "Services up" },
67-
{ key: "services_down", label: "Services down" },
68-
{ key: "active_incidents", label: "Active incidents" },
69-
{ key: "avg_response_time", label: "Average response time (ms)" },
70-
{ key: "total_checks", label: "Total checks" },
71-
{ key: "uptime_percentage", label: "Uptime" },
72-
{ key: "checks_per_minute", label: "Checks per minute" },
73-
],
74-
[],
75-
);
76-
7773
return {
7874
dashboardInfo,
79-
infoKeysDashboard,
8075
onRefreshDashboard,
8176
};
8277
};

0 commit comments

Comments
 (0)