Repository navigation
fix(chat): turn times follow the local day change - #973
Conversation
TurnTime computed its day-aware label from Date.now() during render, and the memoized Turn around it never re-renders on its own, so a turn shown as "11:59 PM" stayed that way after midnight instead of reading "yesterday at". Add src/lib/localDay.ts: a shared useSyncExternalStore day-key store with one timer to the next local midnight while anything listens (re-checked when the page is shown or focused again, since a sleeping device's timer clock can stop). TurnTime formats against the day key, so only the time elements re-render, and only when the day changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Summary
Merge Risk: ⚪ Minimal · up to
|
Move TurnTime into its own module and let useLocalDay read its store from DayStoreContext (the browser clock by default), so a test can render the component inside a memoized wrapper with a fake clock and check that the label turns from today's time into "yesterday at" after midnight. Reverting to Date.now() or dropping useLocalDay now fails the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Follow-up to #951.
Problem
Chat turn times are day-aware ("11:59 PM" today, "yesterday at 11:59 PM", a date for older turns), but the label never updated after local midnight: a turn shown with today's time kept that label on the next day until the chat was reopened or the turn changed.
Root cause
TurnTimeinsrc/components/ChatView.tsxcalledformatChatTimestamp(ts, Date.now(), ...)during render. Nothing schedules a re-render at the day change, andTurnis memoized (a turn that did not change keeps its object across polls), so the label computed at the last render stayed.Change
src/lib/localDay.ts:localDayKey(start of the local day),msUntilNextLocalMidnight(calendar-based, so DST days are right), andcreateDayStore, a shared store with one timer to the next local midnight while anything listens and none otherwise. The snapshot is read from the clock each time, and the store re-checks when the page is shown or focused again, because a sleeping device's timer clock can stop. A timer that fires early re-arms instead of announcing the day.useLocalDay()exposes it viauseSyncExternalStore.TurnTimeformats against the day key, so only the<time>elements re-render, and only when the day changes; the memoized turns do not.changelog.d/turn-time-midnight.md(Fixed).Validation
src/lib/localDay.test.ts(fake clock with injected timers): day key and wait to midnight; listeners notified once at midnight and not before, then again the next midnight; one shared timer and resume listener for 50 subscribers, none after they leave; early timer re-arms; waking past midnight after the timer slept notifies once and the stale timer is replaced; a turn at 23:59 reads "11:59 PM" and then "yesterday at 11:59 PM" after midnight.bun run check fast: all ok (3462 unit tests pass).scripts/chat-history-browser-qa.tsandscripts/chat-virtual-browser-qa.tspass.scripts/chat-browser-qa.tsfails at the composer layout assertion ("the message on top, one row of controls under it, one round button"), and it fails the same way on unmodifiedorigin/main, so it is unrelated to this change.Review
TurnTimeactually uses the day store, so reverting it toDate.now()would pass CI. Fixed in 85c52e4:TurnTimemoved tosrc/components/TurnTime.tsx,useLocalDayreads its store fromDayStoreContext(the browser clock by default), andsrc/components/TurnTime.test.tsxrenders it inside a memoized wrapper with a fake clock set years from the real one, checking the label reads the time before midnight and "yesterday at" after. Verified the test fails when the component formats againstDate.now().bun run check fast: typecheck, build and the new tests pass; four unrelated unit files failed under load and pass when run alone.🤖 Generated with Claude Code