Skip to content

refactor(stats): centralise good/bad trend logic in format.ts#317

Merged
Amund211 merged 1 commit into
mainfrom
consolidate-stat-format
Jul 10, 2026
Merged

refactor(stats): centralise good/bad trend logic in format.ts#317
Amund211 merged 1 commit into
mainfrom
consolidate-stat-format

Conversation

@Amund211

@Amund211 Amund211 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

What

Follows the pattern of #316 (which introduced src/stats/format.ts as the single home for stat-kind formatting knowledge) by moving the good/bad stat functionality into that same shared helper file, and consolidating the trend logic that was open-coded in two places on the session page.

Moved into src/stats/format.ts

  • BAD_STATS + isBadStat(stat) — previously a module-level constant in src/routes/session/$uuid.tsx. This is stat semantics (which stats get error colours when rising), so it now sits next to STAT_FORMAT_KIND.
  • getTrendDirection(from, to) — the three-way up/down/flat numeric comparison, which was open-coded as an identical if/else chain in both SessionStatCard and ProgressionValueAndMilestone.
  • getTrendSentiment(stat, direction)good/bad/neutral, folding in isBadStat. This is the (direction, isBad) → good/bad rule that was duplicated (as an inline (dir === "up") === badStat expression) at both call sites.

Call sites

SessionStatCard and ProgressionValueAndMilestone each did the same two steps — compare two numbers into a direction, then map (direction, isBad) to a MUI colour. Both now:

const direction = getTrendDirection(from, to);
const color = SENTIMENT_COLOR[getTrendSentiment(stat, direction)];

The MUI-specific sentiment → color mapping (good → success, bad → error, neutral → undefined) is a single module-level SENTIMENT_COLOR record in the route (kept out of format.ts so the shared file stays free of MUI). The change is behaviour-preserving — same colours, same icons.

Other consolidation candidates (left out, deliberately)

  • isLinearStat / getRelatedStats — pure stat metadata, but each is used in only one file ($uuid.tsx), so moving them would be relocation without de-duplication. Happy to move them if you'd prefer all stat metadata to live in format.ts.
  • ProgressionCaption's inline .toLocaleString(undefined, { min/maxFractionDigits: 2 }) — repeated ~20× for per-day rates. These are rate values, not stat values, so they don't map to a StatKey format kind; unifying them is a larger refactor and there's already a TODO on the quotient cases. Out of scope here.

Test plan

  • pnpm tsc + pnpm tsc:node — clean
  • pnpm lint:check (oxlint + oxfmt) — clean
  • pnpm test:unit — 786 pass (incl. new src/stats/format.unit.test.ts covering isBadStat / getTrendDirection / getTrendSentiment)
  • pnpm test (browser) — 987 pass

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings July 9, 2026 17:54
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 9, 2026

Copy link
Copy Markdown

Deploying rainbow with  Cloudflare Pages  Cloudflare Pages

Latest commit: 60ff204
Status:⚡️  Build in progress...

View logs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors session-page stat trend rendering by centralizing “bad stat” semantics and trend direction/sentiment logic into src/stats/format.ts, aligning with the earlier formatting consolidation in #316.

Changes:

  • Added BAD_STATS, isBadStat, getTrendDirection, and getTrendSentiment to src/stats/format.ts.
  • Updated session route components to use shared trend direction/sentiment helpers plus a local (MUI-only) SENTIMENT_COLOR mapping.
  • Added unit tests covering bad-stat classification and trend direction/sentiment helpers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/stats/format.ts Centralizes bad-stat semantics and shared trend direction/sentiment helpers.
src/routes/session/$uuid.tsx Replaces duplicated trend logic with shared helpers and a route-local sentiment→MUI-color map.
src/stats/format.unit.test.ts Adds unit coverage for isBadStat, getTrendDirection, and getTrendSentiment.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/routes/session/$uuid.tsx Outdated
Comment thread src/stats/format.unit.test.ts
@Amund211 Amund211 force-pushed the consolidate-stat-format branch from e7edcdb to e38738a Compare July 9, 2026 19:36
Move the good/bad stat semantics next to the stat-kind formatting knowledge
from #316: BAD_STATS now lives in src/stats/format.ts alongside a new
isBadStat helper, plus the two pure trend primitives that were previously
open-coded twice in the session route:

- getTrendDirection(from, to): three-way up/down/flat comparison
- getTrendSentiment(stat, direction): good/bad/neutral, folding in isBadStat

SessionStatCard and ProgressionValueAndMilestone each duplicated both the
numeric->direction if/else chain and the (direction, isBad)->colour mapping.
Both now derive direction/sentiment from the shared helpers and map the
sentiment to a MUI colour through a single module-level SENTIMENT_COLOR
record. Behaviour-preserving.

Adds src/stats/format.unit.test.ts covering the new pure helpers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Amund211 Amund211 force-pushed the consolidate-stat-format branch from e38738a to 60ff204 Compare July 10, 2026 19:38
@Amund211 Amund211 merged commit e4183d7 into main Jul 10, 2026
6 of 7 checks passed
@Amund211 Amund211 deleted the consolidate-stat-format branch July 10, 2026 19:38
Amund211 added a commit that referenced this pull request Jul 10, 2026
The detail page hand-rolled stat formatting: .toFixed(2) for FKDR/KDR
and Math.round(x * 100) + "%" for win rate, duplicating the stat-kind
knowledge that #316/#317 centralised behind formatStatValue. Route the
FKDR/KDR/win-rate/stars values (cards, mode breakdown, chart tooltip,
highlights) through it so precision and locale formatting stay in one
place.

Win rate now shows one decimal (e.g. 42.0%) instead of a rounded
integer, and decimals pick up locale grouping — matching the sessions
list and history chart. Derived, non-stat values (durations, k XP,
fk/hr, the FKDR axis ticks, and the LinearProgress numeric prop) are
left as-is.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants