Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,24 @@ const Editor = () => {
case ACTION_NAME.LAST_PAGE:
loadPageByAction({code: actionName, args: null});
break;
case ACTION_NAME.PAGE_TOP:
case ACTION_NAME.PAGE_TOP: {
goToPositionAndCenter(editor, {lineNumber: 1, column: 1});
const newlogEventNum = beginLineNumToLogEventNumRef.current.get(1);
if (newlogEventNum) {
updateWindowUrlHashParams({logEventNum: newlogEventNum});
}
break;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and variable naming consistency.

The implementation has several areas for improvement:

  1. The variable name newlogEventNum should follow camelCase convention.
  2. Missing error handling when no log event number is found.
  3. Should verify editor model exists before proceeding.

Apply this diff to address these issues:

 case ACTION_NAME.PAGE_TOP: {
+    const model = editor.getModel();
+    if (!model) {
+        console.error("Editor model is undefined");
+        break;
+    }
     goToPositionAndCenter(editor, {lineNumber: 1, column: 1});
-    const newlogEventNum = beginLineNumToLogEventNumRef.current.get(1);
-    if (newlogEventNum) {
-        updateWindowUrlHashParams({logEventNum: newlogEventNum});
+    const newLogEventNum = beginLineNumToLogEventNumRef.current.get(1);
+    if (!newLogEventNum) {
+        console.error("Unable to find log event number for first line");
+        break;
     }
+    updateWindowUrlHashParams({logEventNum: newLogEventNum});
     break;
 }

Committable suggestion skipped: line range outside the PR's diff.

case ACTION_NAME.PAGE_BOTTOM: {
const lineCount = editor.getModel()?.getLineCount();
if ("undefined" === typeof lineCount) {
break;
}
goToPositionAndCenter(editor, {lineNumber: lineCount, column: 1});
const newlogEvent = beginLineNumToLogEventNumRef.current.get(1);
if (newlogEvent) {
updateWindowUrlHashParams({logEventNum: newlogEvent + pageSizeRef.current - 1});
}
break;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and calculation accuracy.

The implementation has several areas for improvement:

  1. The variable name newlogEvent should follow camelCase convention.
  2. Missing error handling when no log event number is found.
  3. The calculation of the last log event number might be inaccurate if log event numbers are not continuous.

Apply this diff to address these issues:

 case ACTION_NAME.PAGE_BOTTOM: {
     const lineCount = editor.getModel()?.getLineCount();
     if ("undefined" === typeof lineCount) {
         break;
     }
     goToPositionAndCenter(editor, {lineNumber: lineCount, column: 1});
-    const newlogEvent = beginLineNumToLogEventNumRef.current.get(1);
-    if (newlogEvent) {
-        updateWindowUrlHashParams({logEventNum: newlogEvent + pageSizeRef.current - 1});
+    const lastLineLogEventNum = getMapValueWithNearestLessThanOrEqualKey(
+        beginLineNumToLogEventNumRef.current,
+        lineCount
+    );
+    if (!lastLineLogEventNum) {
+        console.error("Unable to find log event number for last line");
+        break;
     }
+    updateWindowUrlHashParams({logEventNum: lastLineLogEventNum});
     break;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
case ACTION_NAME.PAGE_BOTTOM: {
const lineCount = editor.getModel()?.getLineCount();
if ("undefined" === typeof lineCount) {
break;
}
goToPositionAndCenter(editor, {lineNumber: lineCount, column: 1});
const newlogEvent = beginLineNumToLogEventNumRef.current.get(1);
if (newlogEvent) {
updateWindowUrlHashParams({logEventNum: newlogEvent + pageSizeRef.current - 1});
}
break;
}
case ACTION_NAME.PAGE_BOTTOM: {
const lineCount = editor.getModel()?.getLineCount();
if ("undefined" === typeof lineCount) {
break;
}
goToPositionAndCenter(editor, {lineNumber: lineCount, column: 1});
const lastLineLogEventNum = getMapValueWithNearestLessThanOrEqualKey(
beginLineNumToLogEventNumRef.current,
lineCount
);
if (!lastLineLogEventNum) {
console.error("Unable to find log event number for last line");
break;
}
updateWindowUrlHashParams({logEventNum: lastLineLogEventNum});
break;
}

default:
Expand Down