Skip to content

Conversation

@wzc520pyfm
Copy link
Owner

@wzc520pyfm wzc520pyfm commented Nov 30, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability by adding safeguards against potential runtime errors in edge cases.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2025

Walkthrough

Added a guard condition in the watch handler for the open property to validate that items is an array with at least one element before initializing activePaths. This prevents runtime errors when the items array is null, undefined, or empty.

Changes

Cohort / File(s) Change Summary
Watch handler guard
src/suggestion/useActive.ts
Added defensive checks (Array.isArray() and .length > 0) to the watch handler condition before accessing the first item in activePaths initialization

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5–10 minutes

  • Verify that the guard condition correctly prevents the identified edge case
  • Confirm that the placement of checks doesn't unintentionally suppress valid initialization scenarios
  • Ensure the guard aligns with expected behavior when open is true but items is invalid

Poem

🐰 A guard at the gate, so wise and so keen,
Checks twice before stepping where arrays convene,
No crashes or stumbles, just safety in code,
A rabbit's small fix on a well-paved road! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding empty array compatibility for items in the suggestion module by adding guards against empty arrays.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/suggestion-compatibility-empty-arr-items

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@netlify
Copy link

netlify bot commented Nov 30, 2025

Deploy Preview for antd-design-x-vue ready!

Name Link
🔨 Latest commit fdc221e
🔍 Latest deploy log https://app.netlify.com/projects/antd-design-x-vue/deploys/692c57eb44f6ab00080c49a5
😎 Deploy Preview https://deploy-preview-455--antd-design-x-vue.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/suggestion/useActive.ts (1)

46-57: Empty items can still cause a runtime error via keyboard navigation

Even with the new guard on the watcher, if open is true and items is an empty array, pressing ArrowUp/ArrowDown will hit offsetRow, where itemCount can be 0:

const itemCount = currentItems.length;
// ...
const nextItem = currentItems[(currentRowIndex + offset + itemCount) % itemCount];
setActivePaths([...activePaths.value.slice(0, currentColIndex - 1), nextItem.value]);

When itemCount === 0, this computes (... % 0), yielding NaN, and nextItem becomes undefined, so nextItem.value throws. To fully support empty items, you can early-return when there are no items:

const offsetRow = (offset: number) => {
  const currentColIndex = activePaths.value.length || 1;

  const currentItems = getItems(currentColIndex);
  const itemCount = currentItems.length;
  if (!itemCount) {
    return;
  }

  const currentRowIndex = currentItems.findIndex(
    (item) => item.value === activePaths.value[currentColIndex - 1],
  );
  const nextItem = currentItems[(currentRowIndex + offset + itemCount) % itemCount];
  setActivePaths([...activePaths.value.slice(0, currentColIndex - 1), nextItem.value]);
};

This is a pre-existing issue, but it’s closely related to the empty-array compatibility you’re addressing here.

🧹 Nitpick comments (1)
src/suggestion/useActive.ts (1)

123-128: Watcher guard correctly prevents crashes; consider small cleanup

The new condition achieves the goal of avoiding a crash when items is null/undefined or an empty array. To simplify and avoid multiple evaluations, you could cache open and items once inside the watcher:

-  watch(() => toValue(open), () => {
-    // 确保 items 是一个数组且至少有一个元素
-    if (toValue(open) && Array.isArray(toValue(items)) && toValue(items).length > 0) {
-      setActivePaths([toValue(items)[0].value]);
-    }
-  }, { immediate: true });
+  watch(() => toValue(open), () => {
+    const openVal = toValue(open);
+    const itemsVal = toValue(items);
+
+    // 确保 items 是一个数组且至少有一个元素
+    if (openVal && Array.isArray(itemsVal) && itemsVal.length > 0) {
+      setActivePaths([itemsVal[0].value]);
+    }
+  }, { immediate: true });

This keeps the behavior identical while making the guard easier to read.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 706e1ec and fdc221e.

📒 Files selected for processing (1)
  • src/suggestion/useActive.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules - antd-design-x-vue
  • GitHub Check: Header rules - antd-design-x-vue
  • GitHub Check: Pages changed - antd-design-x-vue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants