Skip to content

Commit b6b63ea

Browse files
authored
new-log-viewer: Add event cursor, refactor state context, and redesign load page methods to support discontinuous log event numbers in preparation for log level filtering. (#76)
1 parent d185624 commit b6b63ea

File tree

12 files changed

+410
-259
lines changed

12 files changed

+410
-259
lines changed

new-log-viewer/src/components/Editor/MonacoInstance/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
22

33
import {Nullable} from "../../../typings/common";
4-
import {ActionType} from "../../../utils/actions";
4+
import {EditorAction} from "../../../utils/actions";
55
import {clamp} from "../../../utils/math";
66
import type {
77
CursorExplicitPosChangeCallback,
@@ -152,7 +152,7 @@ const setupMobileZoom = (
152152
*/
153153
const setupCustomActions = (
154154
editor: monaco.editor.IStandaloneCodeEditor,
155-
actions: ActionType[],
155+
actions: EditorAction[],
156156
onCustomAction: CustomActionCallback
157157
) => {
158158
actions.forEach(({actionName, label, keyBindings}) => {

new-log-viewer/src/components/Editor/MonacoInstance/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55

66
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
77

8-
import {ActionType} from "../../../utils/actions";
8+
import {EditorAction} from "../../../utils/actions";
99
import {
1010
BeforeMountCallback,
1111
BeforeTextUpdateCallback,
@@ -23,7 +23,7 @@ import "./index.css";
2323

2424

2525
interface MonacoEditorProps {
26-
actions: ActionType[],
26+
actions: EditorAction[],
2727
lineNum: number,
2828
text: string,
2929
themeName: "dark" | "light",

new-log-viewer/src/components/Editor/MonacoInstance/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
22

3-
import {ActionType} from "../../../utils/actions";
3+
import {EditorAction} from "../../../utils/actions";
44
import {
55
setupCursorExplicitPosChangeCallback,
66
setupCustomActions,
@@ -40,7 +40,7 @@ const goToPositionAndCenter = (
4040
*/
4141
const createMonacoEditor = (
4242
editorContainer: HTMLDivElement,
43-
actions: ActionType[],
43+
actions: EditorAction[],
4444
handlers: CustomMonacoEditorHandlers
4545
): monaco.editor.IStandaloneCodeEditor => {
4646
setupCustomLogLanguage();

new-log-viewer/src/components/Editor/index.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {BeginLineNumToLogEventNumMap} from "../../typings/worker";
2424
import {
2525
ACTION_NAME,
2626
EDITOR_ACTIONS,
27-
handleAction,
2827
} from "../../utils/actions";
2928
import {
3029
CONFIG_DEFAULT,
@@ -63,7 +62,7 @@ const resetCachedPageSize = () => {
6362
const Editor = () => {
6463
const {mode, systemMode} = useColorScheme();
6564

66-
const {beginLineNumToLogEventNum, logData, numEvents} = useContext(StateContext);
65+
const {beginLineNumToLogEventNum, logData, loadPageByAction} = useContext(StateContext);
6766
const {logEventNum} = useContext(UrlContext);
6867

6968
const [lineNum, setLineNum] = useState<number>(1);
@@ -72,23 +71,18 @@ const Editor = () => {
7271
);
7372
const editorRef = useRef<Nullable<monaco.editor.IStandaloneCodeEditor>>(null);
7473
const isMouseDownRef = useRef<boolean>(false);
75-
const logEventNumRef = useRef<Nullable<number>>(logEventNum);
76-
const numEventsRef = useRef<Nullable<number>>(numEvents);
7774
const pageSizeRef = useRef(getConfig(CONFIG_KEY.PAGE_SIZE));
7875

7976
const handleEditorCustomAction = useCallback((
8077
editor: monaco.editor.IStandaloneCodeEditor,
8178
actionName: ACTION_NAME
8279
) => {
83-
if (null === logEventNumRef.current || null === numEventsRef.current) {
84-
return;
85-
}
8680
switch (actionName) {
8781
case ACTION_NAME.FIRST_PAGE:
8882
case ACTION_NAME.PREV_PAGE:
8983
case ACTION_NAME.NEXT_PAGE:
9084
case ACTION_NAME.LAST_PAGE:
91-
handleAction(actionName, logEventNumRef.current, numEventsRef.current);
85+
loadPageByAction({code: actionName, args: null});
9286
break;
9387
case ACTION_NAME.PAGE_TOP:
9488
goToPositionAndCenter(editor, {lineNumber: 1, column: 1});
@@ -104,7 +98,7 @@ const Editor = () => {
10498
default:
10599
break;
106100
}
107-
}, []);
101+
}, [loadPageByAction]);
108102

109103
/**
110104
* Sets `editorRef` and configures callbacks for mouse down detection.
@@ -162,16 +156,6 @@ const Editor = () => {
162156
beginLineNumToLogEventNumRef.current = beginLineNumToLogEventNum;
163157
}, [beginLineNumToLogEventNum]);
164158

165-
// Synchronize `logEventNumRef` with `logEventNum`.
166-
useEffect(() => {
167-
logEventNumRef.current = logEventNum;
168-
}, [logEventNum]);
169-
170-
// Synchronize `numEventsRef` with `numEvents`.
171-
useEffect(() => {
172-
numEventsRef.current = numEvents;
173-
}, [numEvents]);
174-
175159
// On `logEventNum` update, update line number in the editor.
176160
useEffect(() => {
177161
if (null === editorRef.current || isMouseDownRef.current) {

new-log-viewer/src/components/MenuBar/NavigationBar.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import SkipNext from "@mui/icons-material/SkipNext";
66
import SkipPrevious from "@mui/icons-material/SkipPrevious";
77

88
import {StateContext} from "../../contexts/StateContextProvider";
9-
import {UrlContext} from "../../contexts/UrlContextProvider";
10-
import {
11-
ACTION_NAME,
12-
handleAction,
13-
} from "../../utils/actions";
9+
import {ACTION_NAME} from "../../utils/actions";
1410
import PageNumInput from "./PageNumInput";
1511
import SmallIconButton from "./SmallIconButton";
1612

@@ -21,16 +17,19 @@ import SmallIconButton from "./SmallIconButton";
2117
* @return
2218
*/
2319
const NavigationBar = () => {
24-
const {numEvents} = useContext(StateContext);
25-
const {logEventNum} = useContext(UrlContext);
20+
const {loadPageByAction} = useContext(StateContext);
2621

2722
const handleNavButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => {
28-
if (null === logEventNum) {
29-
return;
30-
}
31-
const {actionName} = event.currentTarget.dataset as { actionName: ACTION_NAME };
32-
if (Object.values(ACTION_NAME).includes(actionName)) {
33-
handleAction(actionName, logEventNum, numEvents);
23+
const {actionName} = event.currentTarget.dataset;
24+
25+
// Ensure `actionName` is a valid navigation action code with no args.
26+
if (
27+
actionName === ACTION_NAME.FIRST_PAGE ||
28+
actionName === ACTION_NAME.PREV_PAGE ||
29+
actionName === ACTION_NAME.NEXT_PAGE ||
30+
actionName === ACTION_NAME.LAST_PAGE
31+
) {
32+
loadPageByAction({code: actionName, args: null});
3433
}
3534
};
3635

new-log-viewer/src/components/MenuBar/PageNumInput.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Typography} from "@mui/joy";
99
import Input from "@mui/joy/Input";
1010

1111
import {StateContext} from "../../contexts/StateContextProvider";
12+
import {ACTION_NAME} from "../../utils/actions";
1213

1314
import "./PageNumInput.css";
1415

@@ -22,10 +23,7 @@ const PAGE_NUM_INPUT_FIT_EXTRA_WIDTH = 2;
2223
* @return
2324
*/
2425
const PageNumInput = () => {
25-
const {loadPage, numPages, pageNum} = useContext(StateContext);
26-
const adjustedPageNum = (null === pageNum) ?
27-
0 :
28-
pageNum;
26+
const {loadPageByAction, numPages, pageNum} = useContext(StateContext);
2927

3028
const [isEditing, setIsEditing] = useState<boolean>(false);
3129
const inputRef = useRef<HTMLInputElement>(null);
@@ -38,7 +36,10 @@ const PageNumInput = () => {
3836
return;
3937
}
4038

41-
loadPage(Number(inputRef.current.value));
39+
loadPageByAction({
40+
code: ACTION_NAME.SPECIFIC_PAGE,
41+
args: {pageNum: Number(inputRef.current.value)},
42+
});
4243
setIsEditing(false);
4344
};
4445

@@ -68,9 +69,9 @@ const PageNumInput = () => {
6869
if (null === inputRef.current) {
6970
return;
7071
}
71-
inputRef.current.value = adjustedPageNum.toString();
72+
inputRef.current.value = pageNum.toString();
7273
adjustInputWidth();
73-
}, [adjustedPageNum]);
74+
}, [pageNum]);
7475

7576
return (
7677
<form

0 commit comments

Comments
 (0)