Skip to content

Commit cc93358

Browse files
refactor: Replace NotificationContext with useNotificationStore for improved state management (fixes #290, resolves #292). (#297)
Co-authored-by: Henry8192 <[email protected]>
1 parent afc318b commit cc93358

File tree

12 files changed

+105
-222
lines changed

12 files changed

+105
-222
lines changed

src/App.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import AppController from "./components/AppController";
22
import Layout from "./components/Layout";
3-
import NotificationContextProvider from "./contexts/NotificationContextProvider";
43
import UrlContextProvider from "./contexts/UrlContextProvider";
54

65

@@ -11,13 +10,11 @@ import UrlContextProvider from "./contexts/UrlContextProvider";
1110
*/
1211
const App = () => {
1312
return (
14-
<NotificationContextProvider>
15-
<UrlContextProvider>
16-
<AppController>
17-
<Layout/>
18-
</AppController>
19-
</UrlContextProvider>
20-
</NotificationContextProvider>
13+
<UrlContextProvider>
14+
<AppController>
15+
<Layout/>
16+
</AppController>
17+
</UrlContextProvider>
2118
);
2219
};
2320

src/components/AppController.tsx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, {
44
useRef,
55
} from "react";
66

7-
import {NotificationContext} from "../contexts/NotificationContextProvider";
87
import {
98
updateWindowUrlHashParams,
109
URL_HASH_PARAMS_DEFAULT,
@@ -14,10 +13,9 @@ import {
1413
import useContextStore from "../stores/contextStore";
1514
import useLogFileManagerStore from "../stores/logFileManagerProxyStore";
1615
import useLogFileStore from "../stores/logFileStore";
16+
import {handleErrorWithNotification} from "../stores/notificationStore";
1717
import useUiStore from "../stores/uiStore";
1818
import useViewStore from "../stores/viewStore";
19-
import {LOG_LEVEL} from "../typings/logs";
20-
import {DO_NOT_TIMEOUT_VALUE} from "../typings/notifications";
2119
import {UI_STATE} from "../typings/states";
2220
import {
2321
CURSOR_CODE,
@@ -80,12 +78,10 @@ interface AppControllerProps {
8078
* @return
8179
*/
8280
const AppController = ({children}: AppControllerProps) => {
83-
const {postPopUp} = useContext(NotificationContext);
8481
const {filePath, isPrettified, logEventNum} = useContext(UrlContext);
8582

8683
// States
8784
const setLogEventNum = useContextStore((state) => state.setLogEventNum);
88-
const setPostPopUp = useContextStore((state) => state.setPostPopUp);
8985
const logFileManagerProxy = useLogFileManagerStore((state) => state.logFileManagerProxy);
9086
const loadFile = useLogFileStore((state) => state.loadFile);
9187
const numEvents = useLogFileStore((state) => state.numEvents);
@@ -142,21 +138,12 @@ const AppController = ({children}: AppControllerProps) => {
142138
};
143139
const pageData = await logFileManagerProxy.loadPage(cursor, isPrettifiedRef.current);
144140
updatePageData(pageData);
145-
})().catch((e: unknown) => {
146-
console.error(e);
147-
postPopUp({
148-
level: LOG_LEVEL.ERROR,
149-
message: String(e),
150-
timeoutMillis: DO_NOT_TIMEOUT_VALUE,
151-
title: "Action failed",
152-
});
153-
});
141+
})().catch(handleErrorWithNotification);
154142
}, [
155143
beginLineNumToLogEventNum,
156144
logEventNum,
157145
logFileManagerProxy,
158146
numEvents,
159-
postPopUp,
160147
setUiState,
161148
updatePageData,
162149
]);
@@ -180,13 +167,6 @@ const AppController = ({children}: AppControllerProps) => {
180167
loadFile,
181168
]);
182169

183-
useEffect(() => {
184-
setPostPopUp(postPopUp);
185-
}, [
186-
postPopUp,
187-
setPostPopUp,
188-
]);
189-
190170
return children;
191171
};
192172

src/components/CentralContainer/Sidebar/SidebarTabs/SettingsTabPanel/index.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import React, {
2-
useCallback,
3-
useContext,
4-
} from "react";
1+
import React, {useCallback} from "react";
52

63
import {
74
Box,
@@ -15,7 +12,7 @@ import {
1512
Textarea,
1613
} from "@mui/joy";
1714

18-
import {NotificationContext} from "../../../../../contexts/NotificationContextProvider";
15+
import useNotificationStore from "../../../../../stores/notificationStore";
1916
import useViewStore from "../../../../../stores/viewStore";
2017
import {Nullable} from "../../../../../typings/common";
2118
import {
@@ -138,7 +135,6 @@ const handleConfigFormReset = (ev: React.FormEvent) => {
138135
* @return
139136
*/
140137
const SettingsTabPanel = () => {
141-
const {postPopUp} = useContext(NotificationContext);
142138
const loadPageByAction = useViewStore((state) => state.loadPageByAction);
143139

144140
const handleConfigFormSubmit = useCallback((ev: React.FormEvent) => {
@@ -162,6 +158,7 @@ const SettingsTabPanel = () => {
162158
});
163159

164160
if (null !== error) {
161+
const {postPopUp} = useNotificationStore.getState();
165162
postPopUp({
166163
level: LOG_LEVEL.ERROR,
167164
message: error,
@@ -171,10 +168,7 @@ const SettingsTabPanel = () => {
171168
} else {
172169
loadPageByAction({code: ACTION_NAME.RELOAD, args: null});
173170
}
174-
}, [
175-
loadPageByAction,
176-
postPopUp,
177-
]);
171+
}, [loadPageByAction]);
178172

179173
return (
180174
<CustomTabPanel

src/components/PopUps/PopUpMessageBox.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, {
2-
useContext,
32
useEffect,
43
useRef,
54
useState,
@@ -16,10 +15,7 @@ import {
1615

1716
import CloseIcon from "@mui/icons-material/Close";
1817

19-
import {
20-
NotificationContext,
21-
PopUpMessage,
22-
} from "../../contexts/NotificationContextProvider";
18+
import useNotificationStore, {PopUpMessage} from "../../stores/notificationStore";
2319
import {WithId} from "../../typings/common";
2420
import {LOG_LEVEL} from "../../typings/logs";
2521
import {DO_NOT_TIMEOUT_VALUE} from "../../typings/notifications";
@@ -43,7 +39,8 @@ interface PopUpMessageProps {
4339
const PopUpMessageBox = ({message}: PopUpMessageProps) => {
4440
const {id, level, primaryAction, message: messageStr, title, timeoutMillis} = message;
4541

46-
const {handlePopUpMessageClose} = useContext(NotificationContext);
42+
const handlePopUpMessageClose = useNotificationStore((state) => state.handlePopUpMessageClose);
43+
4744
const [percentRemaining, setPercentRemaining] = useState<number>(100);
4845
const intervalCountRef = useRef<number>(0);
4946

src/components/PopUps/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import {useContext} from "react";
2-
31
import {
42
Snackbar,
53
Stack,
64
} from "@mui/joy";
75

8-
import {NotificationContext} from "../../contexts/NotificationContextProvider";
6+
import useNotificationStore from "../../stores/notificationStore";
97
import PopUpMessageBox from "./PopUpMessageBox";
108

119
import "./index.css";
@@ -17,7 +15,7 @@ import "./index.css";
1715
* @return
1816
*/
1917
const PopUps = () => {
20-
const {popUpMessages} = useContext(NotificationContext);
18+
const popUpMessages = useNotificationStore((state) => state.popUpMessages);
2119

2220
return (
2321
<Snackbar

src/contexts/NotificationContextProvider.tsx

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/stores/contextStore.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
import {create} from "zustand";
22

3-
import {PopUpMessage} from "../typings/notifications";
4-
53

64
interface ContextValues {
75
logEventNum: number;
8-
postPopUp: (message: PopUpMessage) => void;
96
}
107

118
interface ContextActions {
129
setLogEventNum: (newLogEventNum: number) => void;
13-
setPostPopUp: (postPopUp: (message: PopUpMessage) => void) => void;
1410
}
1511

1612
type ContextState = ContextValues & ContextActions;
1713

1814
const CONTEXT_STORE_DEFAULT: ContextValues = {
1915
logEventNum: 0,
20-
postPopUp: () => {
21-
},
2216
};
2317

2418
const useContextStore = create<ContextState>((set) => ({
2519
...CONTEXT_STORE_DEFAULT,
2620
setLogEventNum: (newLogEventNum) => {
2721
set({logEventNum: newLogEventNum});
2822
},
29-
setPostPopUp: (postPopUp) => {
30-
set({postPopUp});
31-
},
3223
}));
3324

3425
export default useContextStore;

src/stores/logExportStore.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import {create} from "zustand";
22

33
import LogExportManager, {EXPORT_LOGS_PROGRESS_VALUE_MIN} from "../services/LogExportManager";
44
import {Nullable} from "../typings/common";
5-
import {LOG_LEVEL} from "../typings/logs";
6-
import {DO_NOT_TIMEOUT_VALUE} from "../typings/notifications";
75
import {EXPORT_LOGS_CHUNK_SIZE} from "../utils/config";
8-
import useContextStore from "./contextStore";
96
import useLogFileManagerProxyStore from "./logFileManagerProxyStore";
107
import useLogFileStore from "./logFileStore";
8+
import {handleErrorWithNotification} from "./notificationStore";
119

1210

1311
interface LogExportValues {
@@ -47,17 +45,7 @@ const useLogExportStore = create<LogExportState>((set) => ({
4745
(async () => {
4846
const {logFileManagerProxy} = useLogFileManagerProxyStore.getState();
4947
await logFileManagerProxy.exportLogs();
50-
})().catch((e: unknown) => {
51-
console.error(e);
52-
53-
const {postPopUp} = useContextStore.getState();
54-
postPopUp({
55-
level: LOG_LEVEL.ERROR,
56-
message: String(e),
57-
timeoutMillis: DO_NOT_TIMEOUT_VALUE,
58-
title: "Action failed",
59-
});
60-
});
48+
})().catch(handleErrorWithNotification);
6149
},
6250
}));
6351

0 commit comments

Comments
 (0)