Skip to content

Commit 96f8599

Browse files
committed
Use the NavigateFunction
1 parent b64a1f3 commit 96f8599

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

website/src/app.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export default function App() {
3636
return (
3737
<div style={{ height: "100%" }}>
3838
<AuthProvider>
39-
<ContextProvider>
40-
<Router>
39+
<Router>
40+
<ContextProvider>
4141
<GlobalHeader />
4242
<div style={{ height: "56px", backgroundColor: "#000716" }}>&nbsp;</div>
4343
<div>
@@ -208,8 +208,8 @@ export default function App() {
208208
)}
209209
</Routes>
210210
</div>
211-
</Router>
212-
</ContextProvider>
211+
</ContextProvider>
212+
</Router>
213213
</AuthProvider>
214214
</div>
215215
);

website/src/common/helpers/api-helper.ts

+36-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import axios, { AxiosResponse } from "axios";
2+
import { NavigateFunction } from "react-router-dom";
23

34
export abstract class ApiHelper {
4-
static async get<T>(path: string): Promise<T | null> {
5+
// Add an optional navigate parameter
6+
static async get<T>(path: string, navigate?: NavigateFunction): Promise<T | null> {
57
try {
68
const response: AxiosResponse<T> = await axios.get("/api/" + path, {
7-
timeout: 10000 // 10 seconds timeout
9+
timeout: 10000, // 10 seconds timeout
810
});
911
return response.data;
1012
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -13,14 +15,18 @@ export abstract class ApiHelper {
1315
console.log("Unauthorized");
1416
// Only redirect if we're not already on the login page
1517
if (!window.location.pathname.includes("/login")) {
16-
window.location.href = "/#/login";
18+
if (navigate) {
19+
navigate("/login");
20+
} else {
21+
window.location.href = "/#/login";
22+
}
1723
}
1824
return null;
1925
}
2026
if (
2127
(error.response?.status >= 500 && error.response?.status < 600) ||
2228
error.code === "ERR_CONNECTION_REFUSED" ||
23-
error.code === "ERR_CONNECTION_TIMED_OUT" ||
29+
error.code === "ERR_CONNECTION_TIMED_OUT" ||
2430
error.code === "ERR_CONNECTION_RESET" ||
2531
error.code === "ECONNABORTED" ||
2632
error.code === "ERR_NETWORK" ||
@@ -31,21 +37,29 @@ export abstract class ApiHelper {
3137
console.log("Unable to connect to server", {
3238
code: error.code,
3339
message: error.message,
34-
status: error.request?.status
40+
status: error.request?.status,
3541
});
36-
// Force redirect using document.location for more reliable navigation
37-
document.location.href = "/#/system-unavailable";
42+
// Use navigate when available, fallback to window.location
43+
if (navigate) {
44+
navigate("/system-unavailable");
45+
} else {
46+
window.location.href = "/#/system-unavailable";
47+
}
3848
return null;
3949
}
4050
console.error("Error getting api " + path + ":", error);
4151
return null;
4252
}
4353
}
4454

45-
static async post<T>(path: string, data: unknown): Promise<T | null> {
55+
static async post<T>(
56+
path: string,
57+
data: unknown,
58+
navigate?: NavigateFunction
59+
): Promise<T | null> {
4660
try {
4761
const response: AxiosResponse<T> = await axios.post("/api/" + path, data, {
48-
timeout: 10000 // 10 seconds timeout
62+
timeout: 10000, // 10 seconds timeout
4963
});
5064
return response.data;
5165
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -54,14 +68,18 @@ export abstract class ApiHelper {
5468
console.log("Unauthorized");
5569
// Only redirect if we're not already on the login page
5670
if (!window.location.pathname.includes("/login")) {
57-
window.location.href = "/#/login";
71+
if (navigate) {
72+
navigate("/login");
73+
} else {
74+
window.location.href = "/#/login";
75+
}
5876
}
5977
return null;
6078
}
6179
if (
6280
(error.response?.status >= 500 && error.response?.status < 600) ||
6381
error.code === "ERR_CONNECTION_REFUSED" ||
64-
error.code === "ERR_CONNECTION_TIMED_OUT" ||
82+
error.code === "ERR_CONNECTION_TIMED_OUT" ||
6583
error.code === "ERR_CONNECTION_RESET" ||
6684
error.code === "ECONNABORTED" ||
6785
error.code === "ERR_NETWORK" ||
@@ -72,10 +90,14 @@ export abstract class ApiHelper {
7290
console.log("Unable to connect to server", {
7391
code: error.code,
7492
message: error.message,
75-
status: error.request?.status
93+
status: error.request?.status,
7694
});
77-
// Force redirect using document.location for more reliable navigation
78-
document.location.href = "/#/system-unavailable";
95+
// Use navigate when available, fallback to window.location
96+
if (navigate) {
97+
navigate("/system-unavailable");
98+
} else {
99+
window.location.href = "/#/system-unavailable";
100+
}
79101
return null;
80102
}
81103
console.error("Error posting to api " + path + ":", error);

website/src/common/hooks/use-battery.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createContext, useContext, useEffect, useState } from "react";
2+
import { useNavigate } from 'react-router-dom';
23
import { useAuth } from "./use-authentication";
34
import { FlashbarProps } from "@cloudscape-design/components";
45
import { ApiHelper } from "../helpers/api-helper";
@@ -43,6 +44,7 @@ export const useBatteryProvider = () => {
4344
const [hasInitialReading, setHasInitialReading] = useState(false);
4445
const [pageLoadTime] = useState<number>(Date.now());
4546
const { isAuthenticated } = useAuth();
47+
const navigate = useNavigate();
4648

4749
// Battery notifications state
4850
const [batteryFlashbarItems, setBatteryFlashbarItems] = useState<
@@ -146,7 +148,7 @@ export const useBatteryProvider = () => {
146148

147149
const getBatteryStatus = async () => {
148150
try {
149-
const response = await ApiHelper.get<BatteryResponse>("get_battery_level");
151+
const response = await ApiHelper.get<BatteryResponse>("get_battery_level", navigate);
150152
return response;
151153
} catch (error) {
152154
console.error("Error fetching battery status:", error);
@@ -165,7 +167,7 @@ export const useBatteryProvider = () => {
165167
isSubscribed = false;
166168
clearInterval(batteryInterval);
167169
};
168-
}, [isAuthenticated]); // Add isAuthenticated as a dependency
170+
}, [isAuthenticated, navigate]); // Add isAuthenticated as a dependency
169171

170172
const batteryContextValue: BatteryState = {
171173
batteryLevel,

0 commit comments

Comments
 (0)