Repository navigation
refactor(list): move sortEvents into src/lib/shape/sort.ts - #224
Conversation
`sortEvents` and `calculateOrder` were pure list ordering living in a data-connected component. `src/lib/shape/sort.ts` already owns the sort order — the `SortOrder` type, `SORT_ORDERS`, `DEFAULT_SORT` and the `?sort=` codec — and `AGENTS.md` puts pure domain helpers there, "no React or i18n". The one thing that kept them in the component was a single line: `recommended` read `i18n.resolvedLanguage` out of `@/config/i18n`. `calculateOrder` already took `language` as a parameter, so threading it one level further up is all the move needed. `sortEvents(events, order, language)` now takes it, and neither function nor `DynamicEventsList` imports `@/config/i18n`. `useLocale` exposes `resolvedLanguage` for it — i18next's own value, before the `|| 'en'` fallback that `locale` carries. The two are not interchangeable: the penalty is a loose compare, so an unresolved language penalises an English event x2 while a resolved `'en'` does not. Passing `locale` would have changed the ordering in that window. It comes off the existing subscription, which now snapshots the raw value and derives `locale` from it, so a hook every list card calls does not gain a second `useSyncExternalStore`. The language is also an explicit `useMemo` dependency now. A language switch re-sorted only indirectly before, because `eventsQuery` keys on the locale and so yields a new `events` reference. That still holds; it is no longer what the re-sort depends on. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014pn6dtpARpasKzySRp2p5J
`DynamicEventsList.sort.test.ts` imported a component module to reach a pure function, and mocked `@/config/i18n` because that module boots i18next on import. Both reasons are gone, so the cases move beside the codec they belong with and the mock disappears. Four cases are new, because every factor the move carried has to be able to fail at its new site, and two of them could not: - The online weight and the soon weight had no assertion at all. Each new case overrides only the field whose factor it isolates, and fixes the other events' dates so the remaining factors cancel — both listings two weeks out for the online case, since offline's "soon" window is a week and online's is an hour. - `closest` and `soonest` were asserted only against each other, one list with verification stages and one without. That cannot see a missing `.sort`: it mutates both lists the same way. Each is now also pinned against the field it orders by. The language case passes `undefined` as well as `'en'`, which is what pins the argument to `i18n.resolvedLanguage`. `useLocale().locale` is `resolvedLanguage || 'en'`, so passing that would make both columns identical and the case vacuous. Verified by reintroducing each of the six defects in turn — the four `recommended` factors and the two comparators — and watching the spec go red. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014pn6dtpARpasKzySRp2p5J
…nest case Two real findings out of the review pass, plus a comment sweep. `src/lib/shape/sort.ts` reaching for `isSoon` closed a four-module cycle: `lib/events.ts` imports the `@/lib/shape` barrel, that barrel re-exports `./sort`, and `./sort` now imports back into `src/lib`. Nothing threw, because every binding across it is read from a function body and `isSoon` is a hoisted declaration — one top-level read away from `Cannot access before initialization`, and no `import/no-cycle` rule to catch it. `lib/events.ts` now takes `timePeriodRanges` from `./shape/filters` directly, which opens the loop: `shape/filters` reaches neither `./sort` nor `src/lib`. `sort.ts` also imports `@/lib/events` rather than the `@/lib` barrel, which `src/lib/index.ts` reserves for app code. The stage-blindness case for `soonest` could not fail. It compares a list carrying verification stages against a stage-blind copy, and every fixture shared one schedule, so `byNextOccurrence` returned 0 for every pair and both sides were the input order. It held for any implementation at all, the partition it exists to refuse included. The schedules now differ. Verified by adding a stage partition to each literal ordering and watching both cases go red. Comments: the moved `sortEvents` block repeated the module header's own "presentation concern, never refetches" three ways, and the fixture header narrated the diff rather than the fixture. `useLocale`'s eleven lines over three lines of code are down to three. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014pn6dtpARpasKzySRp2p5J
The hook now returns `resolvedLanguage` beside `locale`, and the rule doc is where a reader looks before reaching for the more raw-sounding one. `locale` is the value for anything user-facing. The raw one exists for `sortEvents`' wrong- language penalty, which must not take the `|| 'en'` fallback as a match. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_014pn6dtpARpasKzySRp2p5J
Deploying sahajatlas-design with
|
| Latest commit: |
b515f31
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://02481e15.sahajatlas-design.pages.dev |
| Branch Preview URL: | https://claude-refactor-222-sort-eve.sahajatlas-design.pages.dev |
Deploying sahajatlas with
|
| Latest commit: |
b515f31
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a79b39e2.sahajatlas.pages.dev |
| Branch Preview URL: | https://claude-refactor-222-sort-eve.sahajatlas.pages.dev |
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The move is right, nothing reorders, and the new spec beats the one it replaces.
Two of the seven files exist only for criterion 3, which rests on a premise src/config/i18n.ts makes false: resolvedLanguage can never differ from locale here. Dropping it removes use-locale.ts, the doc rule and a spec case. The cycle repair is correct but treats the symptom. No contract surface is touched.
Findings
- should-fix —
resolvedLanguageguards an unreachable state.src/hooks/use-locale.ts:75 - consider — move
isSoonintoshape/event.ts.src/lib/events.ts:7 - consider — the doc bullet describes an impossible window.
docs/rules/i18n-and-state.md:88
What was checked
#222 and #223 against the diff. Every touched file read whole, plus shape/event.ts and both barrels. src/config/i18n.ts: resolvedLanguage cannot be undefined at render. A value-import walk over src/ reports no cycles. Grepped every sortEvents, isSoon and resolvedLanguage site. Each spec case checked for vacuity.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
`sort.ts` reached up into `@/lib/events` for `isSoon` alone, and that one edge closed `lib/events.ts -> @/lib/shape -> shape/routing -> shape/path -> shape/sort -> lib/events`. Pointing `lib/events.ts` at `./shape/filters` only held the cycle open; nothing but a comment stopped the next barrel import closing it again, and #153 is what losing that bet costs. `isSoon` is a pure event-time predicate that belongs beside `nextOccurrence` and `byNextOccurrence`, and the `@/lib` barrel exported it to no app consumer. Moving it makes the cycle unformable, so `lib/events.ts` takes the barrel import back. Its spec follows it into `shape/event.test.ts`, and fails there when the bounds check is removed. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AKHWUuYJKpnA4Ris2GyURz
The rule described `resolvedLanguage` as `undefined` until i18next resolves, so a reader would go looking for a gap this app does not have. `src/config/i18n.ts` bundles the English snapshot and sets `initImmediate: false`, so `init` resolves synchronously and the raw value is a string before the first render. The distinction the parameter encodes is still correct. It is just unobservable here, and the rule now says so rather than implying a state to defend against. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AKHWUuYJKpnA4Ris2GyURz
|
Addressed the adversarial review and pushed Adopted
Rebutted, left open
Docs sync caught 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
@Ardnived relaxed #222's criterion 3, which named `i18n.resolvedLanguage` and ruled out `useLocale().locale` by name. The criterion rested on a row its own table has but this app does not: `src/config/i18n.ts` bundles the English snapshot and sets `initImmediate: false`, so `init` resolves synchronously and the raw value is a string before the first render. The two can never differ. So the second return value guarded nothing, at the cost of a value on a hook ~40 sites destructure, a rule in `docs/rules/i18n-and-state.md` and a spec case pinning an input no caller can produce. `use-locale.ts` and the doc rule revert to `main` wholesale, and `sortEvents` takes `language: string` — with no `undefined` reaching it, accepting one was the same unreachable guard one level down. The spec case it replaces now asserts the penalty against a reachable language. Both assertions were re-proven by reintroducing the defect at its own site: removing the `*= 2` penalty fails it, and hard-coding `'en'` in place of the argument fails it too, so neither rides on a value baked into the module. No ordering changes for any visitor: `locale` is what `resolvedLanguage` always already was. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Fo2R5VispZeMcR1xLb2xUu
|
Relaxed criterion 3 as asked, and pushed Adopted
Lean gate, ⚠ Your approval predates this push, so it may need re-issuing. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
Smoke is red and no smoke spec ran. Preview discovery gave up after 10m00s with The Cloudflare Pages build for This is not this PR's failure. The diff is No fix exists to port, so I pushed nothing. The job's own hint is to re-run Smoke, which 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
Third and last automatic CI fix for this PR. I pushed nothing, because nothing here is fixable in code. The The CI-fix ceiling is now spent, so no further 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
CI is still red after 3 fix attempts (failing: Smoke). I have stopped retrying. A comment or a push from you starts me again. 🤖 sydevs dispatcher |
Summary
sortEventsandcalculateOrdermove out ofDynamicEventsListintosrc/lib/shape/sort.ts, which already owns the sort order. Neither imports@/config/i18nany more.DynamicEventsListpassesuseLocale().locale, and the sort memo lists it as a dependency.Changes
src/lib/shape/sort.ts— both functions,sortEvents(events, order, language).calculateOrderstays module-private. Every predicate comes from./event.src/components/organisms/EventsList/DynamicEventsList.tsx— keeps its existinguseLocale()call and passeslocalethrough. No new hook, no new value on the hook.src/lib/shape/event.ts— gainsisSoon, moved fromsrc/lib/events.ts. That is what makes the import cycle below unformable rather than merely open (see Notes).src/lib/events.ts/src/lib/index.ts—lib/events.tskeeps the@/lib/shapebarrel import; the@/libbarrel dropsisSoon, which no app code consumed.src/lib/shape/sort.test.ts— the oldDynamicEventsList.sort.test.tsfolded in and extended. Novi.mock('@/config/i18n').isSoon's own spec follows it intoshape/event.test.ts.Preview
Test results
test:run(1617 passed, 124 files)src/lib/shape/sort.test.tsandsrc/lib/shape/event.test.tspnpm build,pnpm size(standalone 301.9 / loader 2.6 / embed 303.7 KiB, all under budget, no shared chunk),pnpm ladle:buildManual verification
pnpm dev, search a place, then switch Recommended / Closest / Soonest. The order matchesmain's, and the network tab shows no refetch on a sort change.Notes for reviewer
#222's criterion 3 was relaxed, and
b515f31is the result. The criterion namedi18n.resolvedLanguageand ruled outuseLocale().localeby name, which collided with criterion 2 (DynamicEventsList.tsxmust not import@/config/i18n). The first pass satisfied both by exposing the raw value from the hook. @Ardnived relaxed the criterion, so the hook anddocs/rules/i18n-and-state.mdare back atmainand the sort readslocale.Why the criterion was the thing that gave. It rests on the
undefinedrow of its own table, andsrc/config/i18n.tsdeletes that row: it bundles the English snapshot and setsinitImmediate: false, soinitresolves synchronously andresolvedLanguageis a string before the first render. The two values can never differ here, so the second return value guarded nothing while costing a value on a hook ~40 sites destructure, a doc rule, and a spec case pinning an input no caller can produce.sortEventsnow takeslanguage: stringfor the same reason — with noundefinedreaching it, accepting one was the same unreachable guard a level down.Filed on the way: #223. The ticket's open question ("keep the
en-USquirk, or fix it here?") is a real bug, and it is not abouten-US.preferredLocalecan resolve to a regional tag (pt-BR), while an event'slanguages[0]is always a base code, so'pt-BR' != 'pt'penalises every Portuguese event ×2 for apt-BRviewer.en-USis not affected today, because nothing publishes anen-USbundle. Fixing it changes the ordering, so criterion 6 ruled it out here.Dismissed review findings:
useLocale().languageCodeinstead — it is the root cause. Correct, and exactly what fix(list): recommended ignores language on a regional locale #223 is for. Criterion 6 requires byte-identical orderings, andlanguageCodechanges them for any regional viewer.DateTime.now()out of the decorate loop by inliningisSoon's arithmetic. Real cost on a walk of the whole matching set, but it duplicates a shared predicate and risks the byte-identical criterion. Pre-existing, not introduced here.!=→!==in the language compare. Both sides arestringnow, so the two are equivalent, and the line is a pre-existing one inside a verbatim move.Detail
Every moved behaviour was verified by reintroducing it as a defect. The four
recommendedfactors (language ×2, soon ×0.5, online ×1.5, unverified ×1.5) and both literal comparators each make the spec go red when removed. Two of the six had no assertion at all before this PR, and thesooneststage-blindness case could not fail: every fixture shared one schedule, sobyNextOccurrencetied every pair and both sides were the input order. The schedules now differ, and a stage partition added to either literal ordering turns both cases red.isSoon's moved spec was re-proven the same way at its new site.The language case was re-proven after the criterion was relaxed. It used to pin
undefinedagainst'en'— an input no caller can now produce. It asserts two reachable languages instead, and goes red two ways: delete the*= 2penalty, or hard-code'en'in place of the argument. The second matters on its own, since the first alone would pass for a module that ignored the argument and read a baked-in language.The import cycle.
sort.tsreaching forisSoonclosedlib/events.ts → @/lib/shape → shape/routing → shape/path → shape/sort → lib/events. Nothing threw — every binding across it is read from a function body, andisSoonis a hoisted declaration — but there is noimport/no-cyclerule, so one top-level read would have turned it intoCannot access before initialization. The first fix pointedlib/events.tsat./shape/filters, which only held the cycle open: nothing but a comment stopped the next barrel import closing it again.isSoonnow lives inshape/event.tsinstead, besidenextOccurrenceandbyNextOccurrence, sosort.tsnever reaches up intosrc/liband the cycle cannot form. A value-import walker oversrc/reports none.Fixture pre-mortem.
dueInbuilds a schedule carrying onlyupcomingDatesandfirstDate. The assumption is thatnextOccurrencereadsupcomingDates[0]and nothing else — verified insrc/lib/shape/event.ts— and thatfirstDateis the only other fieldEventScheduleSchemarequires, verified insrc/types/event.ts.Security review skipped: no path in the diff matches
securityReview.triggerPattern, and no added line matches its content pattern.Docs checked:
DESIGN_SYSTEM.md:39namedevents.tsas whereisSoonlives and now namesshape/event.ts; its other twoisSoonmentions andsrc/components/AGENTS.md:424saysrc/lib/, which still reads true.docs/rules/i18n-and-state.mdneeds no change now that the hook is unchanged — itsuseLocale()bullets already read true.AGENTS.mdalready places pure domain helpers insrc/lib/shape/. No host-facing contract change, sodocs/embedding.mdandCHANGELOG.mdstay untouched.Closes #222
🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code
https://claude.ai/code/session_01Fo2R5VispZeMcR1xLb2xUu