Skip to content

fix(#767): consolidate relative-time formatting into a single shared helper - #772

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
Maqbool61:main
Aug 22, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
Maqbool61:main

Conversation

@Maqbool61

@Maqbool61 Maqbool61 commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #767

Four pages (topics, prompts, insights, traffic) each had their own
local implementation of the same relative-time helper. They had drifted into
three user-facing bugs:

Bug Where
Math.round instead of Math.floor → 90s showed as "2m ago", 45min as "1h ago" prompts/page.tsx
Hardcoded 'Just now' (capital J) instead of 'just now' prompts/page.tsx
No 30-day cliff → timestamps showed as "412d ago" indefinitely insights/page.tsx, traffic/page.tsx
Hardcoded English strings, no i18n insights/page.tsx, traffic/page.tsx

Changes

  • web/src/lib/format-relative.ts — single canonical implementation.
    Math.floor throughout, 30-day cliff falls back to toLocaleDateString(),
    i18n via common.relative.* keys.
  • web/src/lib/format-relative.test.ts — 14-case test suite covering
    null/undefined, sub-minute, the 90s floor-vs-round regression, the 45min
    boundary, hour/day boundaries, the 30-day cliff, and the "412d ago"
    regression.
  • web/messages/en.json — relative.* keys moved from topics namespace
    to common so all four pages share them.
  • topics/page.tsx — removed local formatRelative, uses shared helper.
  • prompts/page.tsx — removed local formatRelative, uses shared helper.
  • insights/page.tsx — removed local formatTimeAgo, uses shared helper.
  • traffic/page.tsx — removed local timeAgo, uses shared helper.

Intentional behaviour change

Insights and Traffic previously showed timestamps older than 30 days as e.g.
412d ago. They now render as a locale date string (e.g. 15 Jul 2025),
matching the existing behaviour of Topics and Prompts.

Testing

  • pnpm tsc --noEmit — 0 errors
  • pnpm lint — 0 errors, 7 warnings all pre-existing
  • pnpm test — 112/112 passing

- Extract canonical formatRelative() to web/src/lib/format-relative.ts
- Math.floor throughout; locale date past 30 days on all four pages
- Move relative.* i18n keys from topics namespace to common
- Delete local copies from topics, prompts, insights, traffic pages
- Add 14-case test suite following csv.test.ts pattern

Intentional behaviour change: Insights and Traffic timestamps older
than 30 days now render as a locale date instead of e.g. '412d ago'.
@gkhngyk

gkhngyk commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

The consolidation itself looks right — the floor-vs-round fix and the 30-day cliff are both pinned by tests, and moving the keys to common is the correct home for them.

Two small things before merge:

  • web/messages/en.json: the closing curly quotes in agent.emptyExample1 and emptyExample2 turned into opening ones (...doing?“). Unrelated to this change and visible in the agent empty state — please restore the ”.
  • topics/page.tsx already declares const common = useTranslations('common') a line above the new tCommon; one of the two can go.

@Maqbool61

Copy link
Copy Markdown
Contributor Author

Both items addressed:

  1. en.json — closing curly quotes (") restored on emptyExample1 and
    emptyExample2. The earlier replacement swapped both sides to opening quotes
    by mistake; confirmed via codepoint inspection (0x201d on both closing
    positions).

  2. topics/page.tsx — removed the redundant tCommon declaration. The
    existing common variable was already in scope; call site updated to use it.

@gkhngyk
gkhngyk merged commit 6ef219c into ansvisor:main Aug 22, 2026
4 checks passed
@gkhngyk

gkhngyk commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Both fixes verified and merged — the codepoint check on the quotes was the right instinct. Four copies down to one, with the boundaries pinned by tests. Thanks!

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.

bug: four copies of the relative-time helper disagree about the same timestamp

2 participants