Skip to content

Commit ec35a06

Browse files
authored
feat(settings): Reload file instead of the whole app when applying settings. (#185)
1 parent 773629c commit ec35a06

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

src/components/Editor/index.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ import {goToPositionAndCenter} from "./MonacoInstance/utils";
3939
import "./index.css";
4040

4141

42-
/**
43-
* Resets the cached page size in case it causes a client OOM. If it doesn't, the saved value
44-
* will be restored when {@link restoreCachedPageSize} is called.
45-
*/
46-
const resetCachedPageSize = () => {
47-
const error = setConfig(
48-
{key: CONFIG_KEY.PAGE_SIZE, value: CONFIG_DEFAULT[CONFIG_KEY.PAGE_SIZE]}
49-
);
50-
51-
if (null !== error) {
52-
console.error(`Unexpected error returned by setConfig(): ${error}`);
53-
}
54-
};
55-
5642
/**
5743
* Renders a read-only editor for viewing logs.
5844
*
@@ -114,6 +100,23 @@ const Editor = () => {
114100
});
115101
}, []);
116102

103+
/**
104+
* Backs up the current page size and resets the cached page size in case it causes a client
105+
* OOM. If it doesn't, the saved value will be restored when {@link restoreCachedPageSize} is
106+
* called.
107+
*/
108+
const resetCachedPageSize = useCallback(() => {
109+
pageSizeRef.current = getConfig(CONFIG_KEY.PAGE_SIZE);
110+
111+
const error = setConfig(
112+
{key: CONFIG_KEY.PAGE_SIZE, value: CONFIG_DEFAULT[CONFIG_KEY.PAGE_SIZE]}
113+
);
114+
115+
if (null !== error) {
116+
console.error(`Unexpected error returned by setConfig(): ${error}`);
117+
}
118+
}, []);
119+
117120
/**
118121
* Restores the cached page size that was unset in {@link resetCachedPageSize};
119122
*/

src/components/modals/SettingsModal/SettingsDialog.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@ import {
1818
} from "@mui/joy";
1919

2020
import {NotificationContext} from "../../../contexts/NotificationContextProvider";
21+
import {StateContext} from "../../../contexts/StateContextProvider";
2122
import {Nullable} from "../../../typings/common";
2223
import {
2324
CONFIG_KEY,
2425
LOCAL_STORAGE_KEY,
2526
} from "../../../typings/config";
2627
import {LOG_LEVEL} from "../../../typings/logs";
2728
import {DO_NOT_TIMEOUT_VALUE} from "../../../typings/notifications";
29+
import {ACTION_NAME} from "../../../utils/actions";
2830
import {
2931
getConfig,
3032
setConfig,
3133
} from "../../../utils/config";
3234
import ThemeSwitchFormField from "./ThemeSwitchFormField";
3335

3436

35-
const CONFIG_FORM_FIELDS = [
37+
/**
38+
* Gets form fields information for user input of configuration values.
39+
*
40+
* @return A list of form fields information.
41+
*/
42+
const getConfigFormFields = () => [
3643
{
3744
helperText: (
3845
<span>
@@ -96,6 +103,7 @@ const handleConfigFormReset = (ev: React.FormEvent) => {
96103
*/
97104
const SettingsDialog = forwardRef<HTMLFormElement>((_, ref) => {
98105
const {postPopUp} = useContext(NotificationContext);
106+
const {loadPageByAction, setIsSettingsModalOpen} = useContext(StateContext);
99107

100108
const handleConfigFormSubmit = useCallback((ev: React.FormEvent) => {
101109
ev.preventDefault();
@@ -125,9 +133,14 @@ const SettingsDialog = forwardRef<HTMLFormElement>((_, ref) => {
125133
title: "Unable to apply config.",
126134
});
127135
} else {
128-
window.location.reload();
136+
loadPageByAction({code: ACTION_NAME.RELOAD, args: null});
137+
setIsSettingsModalOpen(false);
129138
}
130-
}, [postPopUp]);
139+
}, [
140+
loadPageByAction,
141+
postPopUp,
142+
setIsSettingsModalOpen,
143+
]);
131144

132145
return (
133146
<form
@@ -145,7 +158,7 @@ const SettingsDialog = forwardRef<HTMLFormElement>((_, ref) => {
145158
</DialogTitle>
146159
<DialogContent>
147160
<ThemeSwitchFormField/>
148-
{CONFIG_FORM_FIELDS.map((field, index) => (
161+
{getConfigFormFields().map((field, index) => (
149162
<FormControl
150163
className={"config-form-control"}
151164
key={index}
@@ -168,7 +181,7 @@ const SettingsDialog = forwardRef<HTMLFormElement>((_, ref) => {
168181
color={"primary"}
169182
type={"submit"}
170183
>
171-
Apply & Reload
184+
Apply
172185
</Button>
173186
<Button
174187
color={"neutral"}

src/contexts/StateContextProvider.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
268268
// Refs
269269
const beginLineNumToLogEventNumRef =
270270
useRef<BeginLineNumToLogEventNumMap>(STATE_DEFAULT.beginLineNumToLogEventNum);
271+
const fileSrcRef = useRef<Nullable<FileSrcType>>(null);
271272
const logEventNumRef = useRef(logEventNum);
272273
const logExportManagerRef = useRef<null | LogExportManager>(null);
273274
const mainWorkerRef = useRef<null | Worker>(null);
@@ -406,6 +407,9 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
406407
setOnDiskFileSizeInBytes(STATE_DEFAULT.onDiskFileSizeInBytes);
407408
setExportProgress(STATE_DEFAULT.exportProgress);
408409

410+
// Cache `fileSrc` for reloads.
411+
fileSrcRef.current = fileSrc;
412+
409413
if ("string" !== typeof fileSrc) {
410414
updateWindowUrlSearchParams({[SEARCH_PARAM_NAMES.FILE_PATH]: null});
411415
}
@@ -433,6 +437,19 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
433437
return;
434438
}
435439

440+
if (navAction.code === ACTION_NAME.RELOAD) {
441+
if (null === fileSrcRef.current || null === logEventNumRef.current) {
442+
throw new Error(`Unexpected fileSrc=${JSON.stringify(fileSrcRef.current)
443+
}, logEventNum=${logEventNumRef.current} when reloading.`);
444+
}
445+
loadFile(fileSrcRef.current, {
446+
code: CURSOR_CODE.EVENT_NUM,
447+
args: {eventNum: logEventNumRef.current},
448+
});
449+
450+
return;
451+
}
452+
436453
const cursor = getPageNumCursor(navAction, pageNumRef.current, numPagesRef.current);
437454
if (null === cursor) {
438455
console.error(`Error with nav action ${navAction.code}.`);
@@ -442,7 +459,7 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
442459

443460
setUiState(UI_STATE.FAST_LOADING);
444461
loadPageByCursor(mainWorkerRef.current, cursor);
445-
}, []);
462+
}, [loadFile]);
446463

447464
const filterLogs = useCallback((filter: LogLevelFilter) => {
448465
if (null === mainWorkerRef.current) {

src/utils/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum ACTION_NAME {
1111
LAST_PAGE = "lastPage",
1212
PAGE_TOP = "pageTop",
1313
PAGE_BOTTOM = "pageBottom",
14+
RELOAD = "reload",
1415
}
1516

1617
interface EditorAction {
@@ -69,6 +70,7 @@ type NavigationActionsMap = {
6970
[ACTION_NAME.PREV_PAGE]: null;
7071
[ACTION_NAME.NEXT_PAGE]: null;
7172
[ACTION_NAME.LAST_PAGE]: null;
73+
[ACTION_NAME.RELOAD]: null;
7274
};
7375

7476
type NavigationAction = {

0 commit comments

Comments
 (0)