refactor(thumbnail): sort blocks by their names#110
Conversation
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings. WalkthroughThe changes introduce locale-aware, numeric-based file-name sorting to the Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Pre-merge checks❌ Failed checks (1 inconclusive)
✅ Passed checks (1 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/utils/thumbnail.ts (1)
241-254: LGTM! Robust comparison logic with clear prioritization.The three-tier fallback (explicit order → numeric collator → locale compare) provides flexible, deterministic sorting. The logic correctly prioritizes items in the explicit order and sorts remaining items naturally.
For larger
orderarrays, consider a Map-based optimization to avoid O(n) indexOf lookups:🔎 Optional performance optimization
function compareFileName(name1: string, name2: string, order?: string[]): number { if (order) { - if (order.includes(name1) && order.includes(name2)) { - return order.indexOf(name1) - order.indexOf(name2); - } - if (order.includes(name1)) { - return -1; - } - if (order.includes(name2)) { - return 1; - } + const orderMap = new Map(order.map((name, idx) => [name, idx])); + const idx1 = orderMap.get(name1); + const idx2 = orderMap.get(name2); + if (idx1 !== undefined && idx2 !== undefined) { + return idx1 - idx2; + } + if (idx1 !== undefined) { + return -1; + } + if (idx2 !== undefined) { + return 1; + } } return numericCollator?.compare(name1, name2) || name1.localeCompare(name2); }Note: This creates a Map on every call. For repeated sorts, consider memoizing the Map or building it once outside the comparator.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/utils/thumbnail.ts(5 hunks)
🔇 Additional comments (4)
src/utils/thumbnail.ts (4)
232-239: LGTM! Safe collator initialization with fallback.The defensive try-catch ensures compatibility across environments where
Intl.Collatormight not be available. The numeric option enables natural sorting (e.g., "file2" before "file10").
569-569: LGTM! Clean property addition for custom block ordering.The
blocksOrderproperty is correctly typed as optional and sourced from the manifest's UI configuration. Making it public readonly allows consuming code to access the ordering while preventing mutation.Also applies to: 587-587
321-321: LGTM! Blocks sorted with custom order support.The sort is correctly applied after collecting all blocks (tasks and subflows) and respects the custom
blocksOrderwhen provided, falling back to natural numeric sorting otherwise.
356-356: LGTM! Flows sorted with natural numeric ordering.The sort correctly applies natural, locale-aware numeric sorting to flows. Not passing
blocksOrderis appropriate since flows don't require custom ordering like blocks do.
No description provided.