Skip to content

Commit 14082f5

Browse files
Henry8192hoophalabjunhaoliao
authored
refactor: Replace StateContextProvider with Zustand for state management (resolves #168, resolves #211). (#224)
Co-authored-by: hoophalab <[email protected]> Co-authored-by: Junhao Liao <[email protected]>
1 parent 3a3c9f4 commit 14082f5

36 files changed

+1269
-960
lines changed

package-lock.json

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
"@mui/joy": "^5.0.0-beta.51",
3232
"axios": "^1.8.2",
3333
"clp-ffi-js": "^0.5.0",
34+
"comlink": "^4.4.2",
3435
"dayjs": "^1.11.13",
3536
"js-beautify": "^1.15.4",
3637
"monaco-editor": "0.50.0",
3738
"react": "^19.0.0",
38-
"react-dom": "^19.0.0"
39+
"react-dom": "^19.0.0",
40+
"zustand": "^5.0.4"
3941
},
4042
"devDependencies": {
4143
"@babel/core": "^7.26.0",

src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Layout from "./components/Layout";
2+
import AppController from "./contexts/AppController";
23
import NotificationContextProvider from "./contexts/NotificationContextProvider";
3-
import StateContextProvider from "./contexts/StateContextProvider";
44
import UrlContextProvider from "./contexts/UrlContextProvider";
55

66

@@ -13,9 +13,9 @@ const App = () => {
1313
return (
1414
<NotificationContextProvider>
1515
<UrlContextProvider>
16-
<StateContextProvider>
16+
<AppController>
1717
<Layout/>
18-
</StateContextProvider>
18+
</AppController>
1919
</UrlContextProvider>
2020
</NotificationContextProvider>
2121
);

src/components/CentralContainer/Sidebar/SidebarTabs/FileInfoTabPanel.tsx

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

63
import {
74
Divider,
@@ -11,7 +8,7 @@ import {
118
import AbcIcon from "@mui/icons-material/Abc";
129
import StorageIcon from "@mui/icons-material/Storage";
1310

14-
import {StateContext} from "../../../../contexts/StateContextProvider";
11+
import useLogFileStore from "../../../../stores/logFileStore";
1512
import {
1613
TAB_DISPLAY_NAMES,
1714
TAB_NAME,
@@ -27,7 +24,8 @@ import CustomTabPanel from "./CustomTabPanel";
2724
* @return
2825
*/
2926
const FileInfoTabPanel = () => {
30-
const {fileName, onDiskFileSizeInBytes} = useContext(StateContext);
27+
const fileName = useLogFileStore((state) => state.fileName);
28+
const onDiskFileSizeInBytes = useLogFileStore((state) => state.onDiskFileSizeInBytes);
3129

3230
const isFileUnloaded = 0 === fileName.length;
3331
const formattedOnDiskSize = useMemo(

src/components/CentralContainer/Sidebar/SidebarTabs/SearchTabPanel/QueryInputBox.tsx

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

63
import {
74
LinearProgress,
85
Stack,
96
Textarea,
107
} from "@mui/joy";
118

12-
import {StateContext} from "../../../../../contexts/StateContextProvider";
13-
import {
14-
QUERY_PROGRESS_VALUE_MAX,
15-
QueryArgs,
16-
} from "../../../../../typings/query";
9+
import useQueryStore from "../../../../../stores/queryStore";
10+
import useUiStore from "../../../../../stores/uiStore";
11+
import {QUERY_PROGRESS_VALUE_MAX} from "../../../../../typings/query";
1712
import {UI_ELEMENT} from "../../../../../typings/states";
1813
import {isDisabled} from "../../../../../utils/states";
1914
import ToggleIconButton from "./ToggleIconButton";
@@ -27,34 +22,29 @@ import "./QueryInputBox.css";
2722
* @return
2823
*/
2924
const QueryInputBox = () => {
30-
const {queryProgress, startQuery, uiState} = useContext(StateContext);
31-
32-
const [queryString, setQueryString] = useState<string>("");
33-
const [isCaseSensitive, setIsCaseSensitive] = useState<boolean>(false);
34-
const [isRegex, setIsRegex] = useState<boolean>(false);
35-
36-
const handleQuerySubmit = (newArgs: Partial<QueryArgs>) => {
37-
startQuery({
38-
isCaseSensitive: isCaseSensitive,
39-
isRegex: isRegex,
40-
queryString: queryString,
41-
...newArgs,
42-
});
43-
};
25+
const isCaseSensitive = useQueryStore((state) => state.queryIsCaseSensitive);
26+
const isRegex = useQueryStore((state) => state.queryIsRegex);
27+
const querystring = useQueryStore((state) => state.queryString);
28+
const setQueryIsCaseSensitive = useQueryStore((state) => state.setQueryIsCaseSensitive);
29+
const setQueryIsRegex = useQueryStore((state) => state.setQueryIsRegex);
30+
const setQueryString = useQueryStore((state) => state.setQueryString);
31+
const queryProgress = useQueryStore((state) => state.queryProgress);
32+
const startQuery = useQueryStore((state) => state.startQuery);
33+
const uiState = useUiStore((state) => state.uiState);
4434

4535
const handleQueryInputChange = (ev: React.ChangeEvent<HTMLTextAreaElement>) => {
4636
setQueryString(ev.target.value);
47-
handleQuerySubmit({queryString: ev.target.value});
37+
startQuery();
4838
};
4939

5040
const handleCaseSensitivityButtonClick = () => {
51-
handleQuerySubmit({isCaseSensitive: !isCaseSensitive});
52-
setIsCaseSensitive(!isCaseSensitive);
41+
setQueryIsCaseSensitive(!isCaseSensitive);
42+
startQuery();
5343
};
5444

5545
const handleRegexButtonClick = () => {
56-
handleQuerySubmit({isRegex: !isRegex});
57-
setIsRegex(!isRegex);
46+
setQueryIsRegex(!isRegex);
47+
startQuery();
5848
};
5949

6050
const isQueryInputBoxDisabled = isDisabled(uiState, UI_ELEMENT.QUERY_INPUT_BOX);
@@ -66,6 +56,7 @@ const QueryInputBox = () => {
6656
maxRows={7}
6757
placeholder={"Search"}
6858
size={"sm"}
59+
value={querystring}
6960
endDecorator={
7061
<Stack
7162
direction={"row"}

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

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

63
import {
74
AccordionGroup,
@@ -11,7 +8,7 @@ import {
118
import UnfoldLessIcon from "@mui/icons-material/UnfoldLess";
129
import UnfoldMoreIcon from "@mui/icons-material/UnfoldMore";
1310

14-
import {StateContext} from "../../../../../contexts/StateContextProvider";
11+
import useQueryStore from "../../../../../stores/queryStore";
1512
import {
1613
TAB_DISPLAY_NAMES,
1714
TAB_NAME,
@@ -30,7 +27,7 @@ import "./index.css";
3027
* @return
3128
*/
3229
const SearchTabPanel = () => {
33-
const {queryResults} = useContext(StateContext);
30+
const queryResults = useQueryStore((state) => state.queryResults);
3431

3532
const [isAllExpanded, setIsAllExpanded] = useState<boolean>(true);
3633

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "@mui/joy";
1717

1818
import {NotificationContext} from "../../../../../contexts/NotificationContextProvider";
19-
import {StateContext} from "../../../../../contexts/StateContextProvider";
19+
import useViewStore from "../../../../../stores/viewStore";
2020
import {Nullable} from "../../../../../typings/common";
2121
import {
2222
CONFIG_KEY,
@@ -108,7 +108,7 @@ const handleConfigFormReset = (ev: React.FormEvent) => {
108108
*/
109109
const SettingsTabPanel = () => {
110110
const {postPopUp} = useContext(NotificationContext);
111-
const {loadPageByAction} = useContext(StateContext);
111+
const loadPageByAction = useViewStore((state) => state.loadPageByAction);
112112

113113
const handleConfigFormSubmit = useCallback((ev: React.FormEvent) => {
114114
ev.preventDefault();

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

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

63
import {
74
TabList,
@@ -14,7 +11,7 @@ import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
1411
import SearchIcon from "@mui/icons-material/Search";
1512
import SettingsOutlinedIcon from "@mui/icons-material/SettingsOutlined";
1613

17-
import {StateContext} from "../../../../contexts/StateContextProvider";
14+
import useUiStore from "../../../../stores/uiStore";
1815
import {TAB_NAME} from "../../../../typings/tab";
1916
import {openInNewTab} from "../../../../utils/url";
2017
import FileInfoTabPanel from "./FileInfoTabPanel";
@@ -49,11 +46,9 @@ interface SidebarTabsProps {
4946
* @param props.ref Reference object used to access the TabList DOM element.
5047
* @return
5148
*/
52-
const SidebarTabs = ({
53-
ref,
54-
}: SidebarTabsProps) => {
55-
const {activeTabName, setActiveTabName} = useContext(StateContext);
56-
49+
const SidebarTabs = ({ref}: SidebarTabsProps) => {
50+
const activeTabName = useUiStore((state) => state.activeTabName);
51+
const setActiveTabName = useUiStore((state) => state.setActiveTabName);
5752
const handleTabButtonClick = (tabName: TAB_NAME) => {
5853
switch (tabName) {
5954
case TAB_NAME.DOCUMENTATION:

src/components/CentralContainer/Sidebar/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import {
22
useCallback,
3-
useContext,
43
useEffect,
54
useRef,
65
} from "react";
76

8-
import {StateContext} from "../../../contexts/StateContextProvider";
7+
import useUiStore from "../../../stores/uiStore";
98
import {CONFIG_KEY} from "../../../typings/config";
109
import {TAB_NAME} from "../../../typings/tab";
1110
import {setConfig} from "../../../utils/config";
@@ -46,7 +45,8 @@ const setPanelWidth = (newValue: number) => {
4645
* @return
4746
*/
4847
const Sidebar = () => {
49-
const {activeTabName, setActiveTabName} = useContext(StateContext);
48+
const activeTabName = useUiStore((state) => state.activeTabName);
49+
const setActiveTabName = useUiStore((state) => state.setActiveTabName);
5050
const tabListRef = useRef<HTMLDivElement>(null);
5151

5252
const handleResizeHandleRelease = useCallback(() => {

src/components/DropFileContainer/index.tsx

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

6-
import {StateContext} from "../../contexts/StateContextProvider";
3+
import useLogFileStore from "../../stores/logFileStore";
4+
import useUiStore from "../../stores/uiStore";
75
import {UI_ELEMENT} from "../../typings/states";
86
import {CURSOR_CODE} from "../../typings/worker";
97
import {isDisabled} from "../../utils/states";
@@ -23,7 +21,8 @@ interface DropFileContextProviderProps {
2321
* @return
2422
*/
2523
const DropFileContainer = ({children}: DropFileContextProviderProps) => {
26-
const {loadFile, uiState} = useContext(StateContext);
24+
const loadFile = useLogFileStore((state) => state.loadFile);
25+
const uiState = useUiStore((state) => state.uiState);
2726
const [isFileHovering, setIsFileHovering] = useState(false);
2827
const disabled = isDisabled(uiState, UI_ELEMENT.DRAG_AND_DROP);
2928

src/components/Editor/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import {
1111
import {useColorScheme} from "@mui/joy";
1212
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
1313

14-
import {StateContext} from "../../contexts/StateContextProvider";
1514
import {
1615
updateWindowUrlHashParams,
1716
UrlContext,
1817
} from "../../contexts/UrlContextProvider";
18+
import useViewStore from "../../stores/viewStore";
1919
import {Nullable} from "../../typings/common";
2020
import {
2121
CONFIG_KEY,
@@ -137,7 +137,9 @@ const handleWordWrapAction = (editor: monaco.editor.IStandaloneCodeEditor) => {
137137
const Editor = () => {
138138
const {mode, systemMode} = useColorScheme();
139139

140-
const {beginLineNumToLogEventNum, logData, loadPageByAction} = useContext(StateContext);
140+
const beginLineNumToLogEventNum = useViewStore((state) => state.beginLineNumToLogEventNum);
141+
const logData = useViewStore((state) => state.logData);
142+
const loadPageByAction = useViewStore((state) => state.loadPageByAction);
141143
const {isPrettified, logEventNum} = useContext(UrlContext);
142144

143145
const [lineNum, setLineNum] = useState<number>(1);

0 commit comments

Comments
 (0)