Skip to content

fix(chat): turn times follow the local day change - #973

Merged
devswha merged 2 commits into
mainfrom
fix/turn-time-midnight
Oct 13, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/turn-time-midnight

Conversation

@devswha

@devswha devswha commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

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

TurnTime in src/components/ChatView.tsx called formatChatTimestamp(ts, Date.now(), ...) during render. Nothing schedules a re-render at the day change, and Turn is memoized (a turn that did not change keeps its object across polls), so the label computed at the last render stayed.

Change

  • New src/lib/localDay.ts: localDayKey (start of the local day), msUntilNextLocalMidnight (calendar-based, so DST days are right), and createDayStore, 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 via useSyncExternalStore. TurnTime formats 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.ts and scripts/chat-virtual-browser-qa.ts pass. scripts/chat-browser-qa.ts fails 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 unmodified origin/main, so it is unrelated to this change.

Review

  • Low: no test checked that TurnTime actually uses the day store, so reverting it to Date.now() would pass CI. Fixed in 85c52e4: TurnTime moved to src/components/TurnTime.tsx, useLocalDay reads its store from DayStoreContext (the browser clock by default), and src/components/TurnTime.test.tsx renders 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 against Date.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

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]>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80e74945-1859-4ba8-b117-8a80d4f3fb13
📥 Commits

Reviewing files that changed from the base of the PR and between 9dce921 and b914f38.

📒 Files selected for processing (4)
  • changelog.d/turn-time-midnight.md
  • src/components/ChatView.tsx
  • src/lib/localDay.test.ts
  • src/lib/localDay.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Chat turn times now update from “today” to “yesterday” at local midnight without reopening the chat.

The change adds a shared local-day store that updates subscribed consumers when the local date changes. Chat turn-time formatting uses the shared day value so labels can change after midnight without reopening the chat.

Changes: Chat turn-time updates

Layer / File(s) Summary
Local-day store and checks
src/lib/localDay.ts, src/lib/localDay.test.ts
Local-day helpers and a subscribed store track calendar-day changes, re-arm timers, and check again after resume events. Tests cover timing, subscriptions, and resume behavior.
Chat timestamp integration
src/lib/localDay.ts, src/components/ChatView.tsx, src/lib/localDay.test.ts, changelog.d/turn-time-midnight.md
The browser store connects timers and resume events to useLocalDay. TurnTime uses its day value for formatting. A test checks the label change, and a changelog entry describes the behavior.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to b914f

Chat turn-time labels should update after local midnight without reopening the chat. No issue requiring a fix before merge was established.

Pre-merge checks · Passed 5
Passed Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 3 files. (1 skipped: 1 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the main change: chat turn times now follow local day changes.
Description check Passed The description includes the required Change and Validation sections and provides clear problem, root cause, implementation, test coverage, and known unrelated failures.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

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]>
@devswha
devswha merged commit 2e14abe into main Oct 13, 2026
15 of 17 checks passed
@devswha
devswha deleted the fix/turn-time-midnight branch October 13, 2026 18:44
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.

1 participant