Conversation
- chapters.ts:抽出攤平題目清單當抽象屏障,取代重複三層 for 迴圈 - App.tsx:navGroupOfChapter 改用 find;畫面渲染改成無可變賦值的 switch 表達式 - Mascot.tsx:getStage 改用 reduce - useProgress.ts:migrateWrongIds 改用 Object.fromEntries+map - CodeBlock.tsx:highlight 改用 matchAll+reduce 取代 while 迴圈手動維護多個可變變數 - answer/route.ts:把「決定要寫入什麼」拆成宣告式運算式,跟「執行交易」的動作分開 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe changes refactor chapter/question lookups, progress migration and answer writes, screen selection, syntax highlighting, and mascot stage selection toward functional collection operations while preserving existing public APIs and behavior. ChangesCore application refactors
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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.
🧹 Nitpick comments (3)
src/data/chapters.ts (1)
70-71: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueConsider caching the flattened levels array.
getLevelcallschapters.flatMap((ch) => ch.levels)on every invocation, allocating a new array each time. Since it's called on every render inApp.tsxwhen in quiz view, you could cache the flattened levels at module scope (likeflatQuestions) for consistency and to avoid repeated allocation.♻️ Optional: cache flattened levels
+const flatLevels: Level[] = chapters.flatMap((ch) => ch.levels)+ export const getLevel = (levelId: string): Level | null => - chapters.flatMap((ch) => ch.levels).find((l) => l.id === levelId) ?? null+ flatLevels.find((l) => l.id === levelId) ?? null🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/data/chapters.ts` around lines 70 - 71, Cache the flattened levels array at module scope, alongside the existing flatQuestions cache, and update getLevel to search that cached collection instead of calling chapters.flatMap on every invocation.src/components/CodeBlock.tsx (1)
9-23: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueOptional: avoid O(n²) spread in reduce.
...acc.partson each iteration copies the entire accumulator, making this quadratic in the number of matches. For typical code snippets this is negligible, but if you want to keep it functional without the cost, consider collecting into a flat array viaconcat:♻️ Optional refactor using concat
const { parts, last } = matches.reduce<{ parts: ReactNode[]; last: number }>( (acc, m, i) => { const [text, comment, string, keyword, number] = m const cls = comment ? 'tok-comment' : string ? 'tok-string' : keyword ? 'tok-keyword' : number ? 'tok-number' : '' const gap = m.index > acc.last ? [code.slice(acc.last, m.index)] : [] return { - parts: [- ...acc.parts,- ...gap,- <span key={i} className={cls}>- {text}- </span>,- ],+ parts: acc.parts.concat(gap, [+ <span key={i} className={cls}>+ {text}+ </span>,+ ]), last: m.index + text.length, } }, { parts: [], last: 0 }, )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/components/CodeBlock.tsx` around lines 9 - 23, Optionally refactor the matches.reduce accumulator in CodeBlock so each iteration avoids spreading the full acc.parts array; append the gap and highlighted span through concat or equivalent in-place collection while preserving output order and the existing last-index tracking.src/App.tsx (1)
72-191: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSwitch IIFE and quizLevel guard look correct.
The early return on line 75 safely guarantees
quizLevelis non-null inside thecase 'quiz'branch, making thequizLevel!assertions on lines 83 and 89 safe. All 11Viewvariants are covered by the switch, withdefaultcorrectly mapping toHome.One optional improvement: an explicit
case 'home':instead of relying ondefaultwould give you compile-time exhaustiveness checking if a new view variant is added later.♻️ Optional: explicit home case for exhaustiveness
case 'profile': return <Profile progress={progress} /> - default:+ case 'home': return ( <Home progress={progress} onOpenChapter={(chapterId) => setView({ name: 'levellist', chapterId })} onMixedPractice={startMixedPractice} /> ) + default:+ const _exhaustive: never = view+ throw new Error(`Unhandled view: ${JSON.stringify(_exhaustive)}`) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/App.tsx` around lines 72 - 191, Optionally replace the fallback default branch in the content switch with an explicit case 'home' branch returning Home, while preserving the existing Home props and behavior; use an exhaustiveness check for any unhandled View variants so future additions are caught at compile time.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/App.tsx`:
- Around line 72-191: Optionally replace the fallback default branch in the
content switch with an explicit case 'home' branch returning Home, while
preserving the existing Home props and behavior; use an exhaustiveness check for
any unhandled View variants so future additions are caught at compile time.
In `@src/components/CodeBlock.tsx`:
- Around line 9-23: Optionally refactor the matches.reduce accumulator in
CodeBlock so each iteration avoids spreading the full acc.parts array; append
the gap and highlighted span through concat or equivalent in-place collection
while preserving output order and the existing last-index tracking.
In `@src/data/chapters.ts`:
- Around line 70-71: Cache the flattened levels array at module scope, alongside
the existing flatQuestions cache, and update getLevel to search that cached
collection instead of calling chapters.flatMap on every invocation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: bc335efe-a684-4394-b54f-4e2368fec16f
📒 Files selected for processing (6)
src/App.tsxsrc/app/api/progress/answer/route.tssrc/components/CodeBlock.tsxsrc/components/Mascot.tsxsrc/data/chapters.tssrc/hooks/useProgress.ts
Summary by CodeRabbit