Skip to content

Commit 04ef695

Browse files
authored
fix(editor): Ensure page reloads after toggling prettify state (fixes #379). (#380)
1 parent f11e40b commit 04ef695

File tree

4 files changed

+20
-35
lines changed

4 files changed

+20
-35
lines changed

docs/src/dev-guide/contributing-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ still be tested manually:
3939
* Exporting all logs to a file
4040
* Toggling tabbed panels in the sidebar
4141
* Using keyboard shortcuts
42-
* Toggling "Prettify" in both the status bar and the address bar
42+
* Toggling "Prettify" via the status bar button, the address bar (URL hash), and the Monaco action
4343

4444
[gh-workflow-test]: https://github.com/y-scope/yscope-log-viewer/blob/main/.github/workflows/test.yaml

src/components/Editor/index.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
CONFIG_KEY,
1818
THEME_NAME,
1919
} from "../../typings/config";
20-
import {HASH_PARAM_NAMES} from "../../typings/url";
2120
import {BeginLineNumToLogEventNumMap} from "../../typings/worker";
2221
import {
2322
ACTION_NAME,
@@ -33,7 +32,10 @@ import {
3332
getMapValueWithNearestLessThanOrEqualKey,
3433
} from "../../utils/data";
3534
import {updateWindowUrlHashParams} from "../../utils/url";
36-
import {updateViewHashParams} from "../../utils/url/urlHash";
35+
import {
36+
togglePrettify,
37+
updateViewHashParams,
38+
} from "../../utils/url/urlHash";
3739
import MonacoInstance from "./MonacoInstance";
3840
import {goToPositionAndCenter} from "./MonacoInstance/utils";
3941

@@ -163,13 +165,7 @@ const handleEditorCustomAction = (
163165
break;
164166
}
165167
case ACTION_NAME.TOGGLE_PRETTIFY: {
166-
const {isPrettified, setIsPrettified} = useViewStore.getState();
167-
const newIsPrettified = !isPrettified;
168-
updateWindowUrlHashParams({
169-
[HASH_PARAM_NAMES.IS_PRETTIFIED]: newIsPrettified,
170-
});
171-
setIsPrettified(newIsPrettified);
172-
updateViewHashParams();
168+
togglePrettify();
173169
break;
174170
}
175171
case ACTION_NAME.TOGGLE_WORD_WRAP:

src/components/StatusBar/index.tsx

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import React, {useCallback} from "react";
2-
31
import {
42
Button,
53
Divider,
@@ -15,14 +13,10 @@ import useLogFileStore from "../../stores/logFileStore";
1513
import useUiStore from "../../stores/uiStore";
1614
import useViewStore from "../../stores/viewStore";
1715
import {UI_ELEMENT} from "../../typings/states";
18-
import {HASH_PARAM_NAMES} from "../../typings/url";
1916
import {ACTION_NAME} from "../../utils/actions";
2017
import {isDisabled} from "../../utils/states";
21-
import {
22-
copyPermalinkToClipboard,
23-
updateWindowUrlHashParams,
24-
} from "../../utils/url";
25-
import {updateViewHashParams} from "../../utils/url/urlHash";
18+
import {copyPermalinkToClipboard} from "../../utils/url";
19+
import {togglePrettify} from "../../utils/url/urlHash";
2620
import LogLevelSelect from "./LogLevelSelect";
2721
import StatusBarToggleButton from "./StatusBarToggleButton";
2822

@@ -47,22 +41,6 @@ const StatusBar = () => {
4741
const numEvents = useLogFileStore((state) => state.numEvents);
4842
const uiState = useUiStore((state) => state.uiState);
4943

50-
const handleStatusButtonClick = useCallback((ev: React.MouseEvent<HTMLButtonElement>) => {
51-
const {actionName} = ev.currentTarget.dataset;
52-
53-
switch (actionName) {
54-
case ACTION_NAME.TOGGLE_PRETTIFY:
55-
updateWindowUrlHashParams({
56-
[HASH_PARAM_NAMES.IS_PRETTIFIED]: false === isPrettified,
57-
});
58-
updateViewHashParams();
59-
break;
60-
default:
61-
console.error(`Unexpected action: ${actionName}`);
62-
break;
63-
}
64-
}, [isPrettified]);
65-
6644
const isPrettifyButtonDisabled = isDisabled(uiState, UI_ELEMENT.PRETTIFY_BUTTON);
6745

6846
return (
@@ -104,7 +82,7 @@ const StatusBar = () => {
10482
tooltipTitle={false === isPrettified ?
10583
"Turn on Prettify" :
10684
"Turn off Prettify"}
107-
onClick={handleStatusButtonClick}/>
85+
onClick={togglePrettify}/>
10886
</Sheet>
10987
);
11088
};

src/utils/url/urlHash.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {handleErrorWithNotification} from "../../stores/notificationStore";
44
import useQueryStore from "../../stores/queryStore";
55
import useViewStore from "../../stores/viewStore";
66
import {Nullable} from "../../typings/common";
7+
import {HASH_PARAM_NAMES} from "../../typings/url";
78
import {
89
CURSOR_CODE,
910
CursorType,
@@ -169,7 +170,17 @@ const updateQueryHashParams = () => {
169170
return isQueryModified;
170171
};
171172

173+
/**
174+
* Toggles the prettify state for formatted log viewing.
175+
*/
176+
const togglePrettify = () => {
177+
const {isPrettified} = useViewStore.getState();
178+
updateWindowUrlHashParams({[HASH_PARAM_NAMES.IS_PRETTIFIED]: !isPrettified});
179+
updateViewHashParams();
180+
};
181+
172182
export {
183+
togglePrettify,
173184
updateQueryHashParams,
174185
updateViewHashParams,
175186
};

0 commit comments

Comments
 (0)