Repository navigation
JR西日本テーマのTTS停車駅リストが初回以降案内されない不具合を修正 - #5692
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
📝 WalkthroughWalkthroughJR西日本向けの停車駅リスト案内を、現在停車位置と「最初の5停車駅」境界に基づき条件付きで生成するメモ化ロジックを追加し、その挙動を検証するテストスイートを導入しました。 Changes
Sequence Diagram(s)(該当なし) Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
src/hooks/useTTSText.ts (2)
562-568: 重複した計算の抽出を検討してください
allStops.slice(0, 5).filter(Boolean).reverse()[0]が.id、.name、.nameKatakanaの各アクセスで繰り返し計算されています。可読性と軽微なパフォーマンス改善のため、変数への抽出を検討してください。♻️ 提案する修正
+ const lastBatchStop = allStops.slice(0, 5).filter(Boolean).reverse()[0]; ... - allStops.slice(0, 5).filter(Boolean).reverse()[0]?.id === + lastBatchStop?.id === selectedBound?.id ? '' : `${replaceJapaneseText( - allStops.slice(0, 5).filter(Boolean).reverse()[0]?.name, - allStops.slice(0, 5).filter(Boolean).reverse()[0] - ?.nameKatakana + lastBatchStop?.name, + lastBatchStop?.nameKatakana )}から先は、後ほどご案内いたします。`🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useTTSText.ts` around lines 562 - 568, The expression allStops.slice(0, 5).filter(Boolean).reverse()[0] is computed repeatedly when comparing ids and accessing .name/.nameKatakana; extract that value into a local variable (e.g., lastStop) before the ternary so subsequent uses in the conditional and in the call to replaceJapaneseText reuse the same reference, then update the conditional to compare lastStop.id to selectedBound?.id and pass lastStop.name and lastStop.nameKatakana to replaceJapaneseText to improve readability and avoid redundant computation.
997-1004: 同様の重複計算(英語テンプレート)日本語テンプレート(562-568行)と同様に、
allStops.slice(0, 5).filter(Boolean).reverse()[0]が複数回計算されています。同じリファクタリングを適用できます。🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useTTSText.ts` around lines 997 - 1004, The duplicated expression allStops.slice(0, 5).filter(Boolean).reverse()[0] is computed multiple times in the English template (and similarly in the Japanese template); introduce a local constant (e.g., lastVisibleStop or lastStop) just above where the templates are built, assign that expression once, and then replace all repeated occurrences in the conditional and in the ph(...) calls (referencing lastVisibleStop?.id, lastVisibleStop?.nameTtsSegments, lastVisibleStop?.nameRoman) as well as in the Japanese template’s uses to avoid redundant work and improve readability.src/hooks/useTTSText.test.tsx (1)
424-436:act()の使用箇所について
act()は React の状態更新をラップするためのものですが、ここでは単純な変数代入をラップしています。動作には問題ありませんが、より慣用的な書き方としてはact()を省略できます。♻️ 提案する修正
- act(() => { - firstSpeech = false; - }); + firstSpeech = false; rerender({}); expect(result.current.text[0]).not.toContain('の順に停まります'); // Phase 3: move to station 5 (神保町) → past batch boundary → re-announce // Station index 5 = 神保町, nextStation = 小川町 (index 6) setupMockUseNextStation(TOEI_SHINJUKU_LINE_STATIONS[6]); - act(() => { - stationIndex = 5; - }); + stationIndex = 5; rerender({});🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useTTSText.test.tsx` around lines 424 - 436, The test unnecessarily wraps simple variable assignments in act(); remove the act() wrapper around "firstSpeech = false" and "stationIndex = 5" and just perform the assignments directly before calling rerender(), leaving setupMockUseNextStation(...) and rerender({}) unchanged so the hook update flow (setupMockUseNextStation, stationIndex change, then rerender) remains intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/hooks/useTTSText.test.tsx`:
- Around line 424-436: The test unnecessarily wraps simple variable assignments
in act(); remove the act() wrapper around "firstSpeech = false" and
"stationIndex = 5" and just perform the assignments directly before calling
rerender(), leaving setupMockUseNextStation(...) and rerender({}) unchanged so
the hook update flow (setupMockUseNextStation, stationIndex change, then
rerender) remains intact.
In `@src/hooks/useTTSText.ts`:
- Around line 562-568: The expression allStops.slice(0,
5).filter(Boolean).reverse()[0] is computed repeatedly when comparing ids and
accessing .name/.nameKatakana; extract that value into a local variable (e.g.,
lastStop) before the ternary so subsequent uses in the conditional and in the
call to replaceJapaneseText reuse the same reference, then update the
conditional to compare lastStop.id to selectedBound?.id and pass lastStop.name
and lastStop.nameKatakana to replaceJapaneseText to improve readability and
avoid redundant computation.
- Around line 997-1004: The duplicated expression allStops.slice(0,
5).filter(Boolean).reverse()[0] is computed multiple times in the English
template (and similarly in the Japanese template); introduce a local constant
(e.g., lastVisibleStop or lastStop) just above where the templates are built,
assign that expression once, and then replace all repeated occurrences in the
conditional and in the ph(...) calls (referencing lastVisibleStop?.id,
lastVisibleStop?.nameTtsSegments, lastVisibleStop?.nameRoman) as well as in the
Japanese template’s uses to avoid redundant work and improve readability.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 63672792-5be8-4260-83c1-8b8873f27104
📒 Files selected for processing (2)
src/hooks/useTTSText.test.tsxsrc/hooks/useTTSText.ts
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/hooks/useTTSText.ts`:
- Around line 559-573: The end-of-line check uses selectedBound?.id and
lastAnnouncedStop?.id which can mismatch for shared/through stations; change
those comparisons to use groupId instead (e.g., compare s.groupId /
lastAnnouncedStop?.groupId against selectedBound?.groupId) inside the
shouldAnnounceJrWestStopList block (around the allStops.slice(...).map(...) and
the trailing lastAnnouncedStop conditional) and apply the same replacement in
the other occurrence referenced (the similar logic at lines ~993-1007) so
end-of-line detection consistently uses groupId.
- Around line 265-277: currentStopIndex is computed from raw stations so its
indexing can diverge from the deduplicated/stop-filtered list used elsewhere;
update the currentStopIndex useMemo to derive its ordered stops from the same
deduplicated stop list (e.g. use allStops or the same groupId-deduped array used
at lines ~220-225), apply the same !getIsPass filter and direction-based
ordering, then call findIndex on that deduped array using station.groupId; also
update the dependency array to depend on that deduped list (e.g. allStops)
instead of raw stations so shouldAnnounceJrWestStopList and currentStopIndex
remain consistent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2c48a06a-ce4e-43c5-b9df-73370b255060
📒 Files selected for processing (2)
src/hooks/useTTSText.test.tsxsrc/hooks/useTTSText.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/hooks/useTTSText.test.tsx
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/hooks/useTTSText.ts (1)
265-273:groupIdの重複排除は共通化しておくと保守しやすいです。Line 221-225 とここで同じ dedupe を別実装にしているので、次に片方だけ変わると
allStops側とcurrentStopIndex側がまたずれやすいです。小さい helper に寄せて両方から使う形にしておくと安全です。♻️ 例
+const dedupeStationsByGroupId = (stations: Station[]): Station[] => { + const seen = new Set<Station['groupId']>(); + return stations.filter((s) => { + if (seen.has(s.groupId)) { + return false; + } + seen.add(s.groupId); + return true; + }); +}; + // 直通時、同じGroupIDの駅が違う駅として扱われるのを防ぐ(ex. 渋谷の次は渋谷に止まります) - const slicedStations = Array.from( - new Set(slicedStationsOrigin.map((s) => s.groupId)) - ) - .map((gid) => slicedStationsOrigin.find((s) => s.groupId === gid)) - .filter((s) => !!s) as Station[]; + const slicedStations = dedupeStationsByGroupId(slicedStationsOrigin); … - const deduped = Array.from(new Set(ordered.map((s) => s.groupId))) - .map((gid) => ordered.find((s) => s.groupId === gid)) - .filter((s) => !!s) as Station[]; + const deduped = dedupeStationsByGroupId(ordered);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useTTSText.ts` around lines 265 - 273, Extract the duplicated "dedupe by groupId + preserve order + skip passes" logic into a small helper (e.g., dedupeStationsByGroupId or buildAllStops) and replace the inline logic inside currentStopIndex with a call to that helper; ensure the helper accepts the stations array and selectedDirection (or an already-ordered array) and returns the deduped stops array (used previously in allStops and here), then compute currentStopIndex by finding the station.groupId in the helper's result to keep both places consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/hooks/useTTSText.ts`:
- Around line 265-273: Extract the duplicated "dedupe by groupId + preserve
order + skip passes" logic into a small helper (e.g., dedupeStationsByGroupId or
buildAllStops) and replace the inline logic inside currentStopIndex with a call
to that helper; ensure the helper accepts the stations array and
selectedDirection (or an already-ordered array) and returns the deduped stops
array (used previously in allStops and here), then compute currentStopIndex by
finding the station.groupId in the helper's result to keep both places
consistent.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: af156b71-5077-45b2-ba58-0464981bda9d
📒 Files selected for processing (1)
src/hooks/useTTSText.ts
Summary
firstSpeech(初回放送)時のみに限定されていたのを、5駅ごとのサイクル境界で再案内されるように変更Changes
currentStopIndex: 進行方向上で現在何番目の停車駅にいるかを純粋に計算するuseMemoを追加shouldAnnounceJrWestStopList:currentStopIndex % 5 === 0の境界でtrueを返すuseMemoを追加firstSpeech条件から分離し、shouldAnnounceJrWestStopListで制御Test plan
npm run lintパスnpm testパス(既存28テスト + 新規3テスト = 31テスト全パス)npm run typecheckパスundefinedが含まれないことを確認🤖 Generated with Claude Code
Summary by CodeRabbit
リリースノート
バグ修正
テスト