Skip to content

refactor(list): move sortEvents into src/lib/shape/sort.ts - #224

Merged
sydevs-bot merged 8 commits into
mainfrom
claude/refactor-222-sort-events
Sep 22, 2026
Merged

sydevs-bot merged 8 commits into
mainfrom
claude/refactor-222-sort-events

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • sortEvents and calculateOrder move out of DynamicEventsList into src/lib/shape/sort.ts, which already owns the sort order. Neither imports @/config/i18n any more.
  • The active language is an argument. DynamicEventsList passes useLocale().locale, and the sort memo lists it as a dependency.
  • No ordering changes. A visitor sees the same list in the same order, which is the point.

Changes

  • src/lib/shape/sort.ts — both functions, sortEvents(events, order, language). calculateOrder stays module-private. Every predicate comes from ./event.
  • src/components/organisms/EventsList/DynamicEventsList.tsx — keeps its existing useLocale() call and passes locale through. No new hook, no new value on the hook.
  • src/lib/shape/event.ts — gains isSoon, moved from src/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.ts keeps the @/lib/shape barrel import; the @/lib barrel drops isSoon, which no app code consumed.
  • src/lib/shape/sort.test.ts — the old DynamicEventsList.sort.test.ts folded in and extended. No vi.mock('@/config/i18n'). isSoon's own spec follows it into shape/event.test.ts.

Preview

  • App — search a place, then switch Recommended / Closest / Soonest. The ordering is this PR's whole observable surface, and it should not change.

Test results

  • Lean gate: ✓ lint, typecheck, test:run (1617 passed, 124 files)
  • Targeted specs: 57 passed across src/lib/shape/sort.test.ts and src/lib/shape/event.test.ts
  • Build: ✓ pnpm build, pnpm size (standalone 301.9 / loader 2.6 / embed 303.7 KiB, all under budget, no shared chunk), pnpm ladle:build

Manual verification

  1. pnpm dev, search a place, then switch Recommended / Closest / Soonest. The order matches main's, and the network tab shows no refetch on a sort change.
  2. Switch the language from the settings menu with a sorted list on screen, and confirm the list re-sorts.

Notes for reviewer

#222's criterion 3 was relaxed, and b515f31 is the result. The criterion named i18n.resolvedLanguage and ruled out useLocale().locale by name, which collided with criterion 2 (DynamicEventsList.tsx must not import @/config/i18n). The first pass satisfied both by exposing the raw value from the hook. @Ardnived relaxed the criterion, so the hook and docs/rules/i18n-and-state.md are back at main and the sort reads locale.

Why the criterion was the thing that gave. It rests on the undefined row of its own table, and src/config/i18n.ts deletes that row: it bundles the English snapshot and sets initImmediate: false, so init resolves synchronously and resolvedLanguage is 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. sortEvents now takes language: string for the same reason — with no undefined reaching it, accepting one was the same unreachable guard a level down.

Filed on the way: #223. The ticket's open question ("keep the en-US quirk, or fix it here?") is a real bug, and it is not about en-US. preferredLocale can resolve to a regional tag (pt-BR), while an event's languages[0] is always a base code, so 'pt-BR' != 'pt' penalises every Portuguese event ×2 for a pt-BR viewer. en-US is not affected today, because nothing publishes an en-US bundle. Fixing it changes the ordering, so criterion 6 ruled it out here.

Dismissed review findings:

  • Pass useLocale().languageCode instead — 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, and languageCode changes them for any regional viewer.
  • Hoist DateTime.now() out of the decorate loop by inlining isSoon'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 are string now, 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 recommended factors (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 the soonest stage-blindness case could not fail: every fixture shared one schedule, so byNextOccurrence tied 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 undefined against 'en' — an input no caller can now produce. It asserts two reachable languages instead, and goes red two ways: delete the *= 2 penalty, 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.ts reaching for isSoon closed lib/events.ts → @/lib/shape → shape/routing → shape/path → shape/sort → lib/events. Nothing threw — every binding across it is read from a function body, and isSoon is a hoisted declaration — but there is no import/no-cycle rule, so one top-level read would have turned it into Cannot access before initialization. The first fix pointed lib/events.ts at ./shape/filters, which only held the cycle open: nothing but a comment stopped the next barrel import closing it again. isSoon now lives in shape/event.ts instead, beside nextOccurrence and byNextOccurrence, so sort.ts never reaches up into src/lib and the cycle cannot form. A value-import walker over src/ reports none.

Fixture pre-mortem. dueIn builds a schedule carrying only upcomingDates and firstDate. The assumption is that nextOccurrence reads upcomingDates[0] and nothing else — verified in src/lib/shape/event.ts — and that firstDate is the only other field EventScheduleSchema requires, verified in src/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:39 named events.ts as where isSoon lives and now names shape/event.ts; its other two isSoon mentions and src/components/AGENTS.md:424 say src/lib/, which still reads true. docs/rules/i18n-and-state.md needs no change now that the hook is unchanged — its useLocale() bullets already read true. AGENTS.md already places pure domain helpers in src/lib/shape/. No host-facing contract change, so docs/embedding.md and CHANGELOG.md stay untouched.

Closes #222

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fo2R5VispZeMcR1xLb2xUu

`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
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

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

View logs

@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 22, 2026
@sydevs-bot

sydevs-bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 no session running
CI fix 3

recent dispatches
  • 15:46Z fix-ci · attempt 1 · session · done
  • 15:46Z fix-ci · attempt 1 · session
  • 15:43Z fix-ci · attempt 1 · session · done
  • 15:43Z fix-ci · attempt 1 · session
  • 15:40Z fix-ci · attempt 1 · session · done

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧐 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 — resolvedLanguage guards an unreachable state. src/hooks/use-locale.ts:75
  • consider — move isSoon into shape/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

Comment thread src/hooks/use-locale.ts Resolved
Comment thread src/lib/events.ts Resolved
Comment thread docs/rules/i18n-and-state.md Resolved
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 22, 2026
`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
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Addressed the adversarial review and pushed a7e6f24.

Adopted

Rebutted, left open

Docs sync caught DESIGN_SYSTEM.md:39, which pointed at events.ts for isSoon. Lean gate, build, size and ladle:build are green, and a value-import walk over src/ reports no cycles.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 22, 2026
@sydevs-bot
sydevs-bot marked this pull request as ready for review September 22, 2026 04:46
@sydevs-bot
sydevs-bot requested a review from Ardnived September 22, 2026 04:46
@sydevs-bot sydevs-bot moved this from Revising to Approved in SYDevelopers Technical Roadmap Sep 22, 2026
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Sep 22, 2026
@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
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Relaxed criterion 3 as asked, and pushed b515f31.

Adopted

  • resolvedLanguage guards an unreachable state — src/hooks/use-locale.ts and docs/rules/i18n-and-state.md revert to main wholesale, and DynamicEventsList passes locale. sortEvents takes language: string: with no undefined reaching it, accepting one was the same unreachable guard a level down. b515f31

sort.test.ts:140 no longer pins undefined, an input no caller can produce. It asserts two reachable languages, and goes red both ways — delete the *= 2 penalty, or hard-code 'en' over the argument.

Lean gate, pnpm build, pnpm size (301.9 / 2.6 / 303.7 KiB, no shared chunk) all green. No ordering changes — locale is what resolvedLanguage always already was.

⚠ 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

@sydevs-bot sydevs-bot added awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 22, 2026
@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed awaiting labels Sep 22, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Smoke is red and no smoke spec ran. Preview discovery gave up after 10m00s with preview_status=pending.

The Cloudflare Pages build for b515f31 is still in_progress — past 13 minutes when I last read the check run, against the 453s measured max the flat deadline is set from. sahajatlas-design deployed fine at 15:30, so only the app project is slow.

This is not this PR's failure. The diff is src/lib, one component, one doc line and specs — nothing that reaches a build. Smoke was green on this same branch earlier today at bb61f36 and a7e6f24.

No fix exists to port, so I pushed nothing. The job's own hint is to re-run Smoke, which fix-ci forbids me. If this recurs, the patch is PREVIEW_TIMEOUT_MS on the discovery step in .github/workflows/ci.yml, never the constant (docs/testing.md). One slow build is not yet evidence for it.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added bot:working Lock: a cloud session is running on this item. Removed when it ends. and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 22, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Third and last automatic CI fix for this PR. I pushed nothing, because nothing here is fixable in code.

The sahajatlas Pages build for b515f31 is still in_progress — 20 minutes now, against the 453s measured max the flat discovery deadline is set from. sahajatlas-design deployed at 15:30, and every other check on this commit is green. Smoke ran no spec, so this is a stuck deploy, not a failing test — see my earlier note for why it is not this PR's.

The CI-fix ceiling is now spent, so no further fix-ci fires. Retrying the sahajatlas deployment in Cloudflare and re-running Smoke are both a human's to do — fix-ci forbids me either.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot added awaiting and removed bot:working Lock: a cloud session is running on this item. Removed when it ends. labels Sep 22, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

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

@sydevs-bot
sydevs-bot added this pull request to the merge queue Sep 22, 2026
Merged via the queue into main with commit eeba425 Sep 22, 2026
34 of 35 checks passed
@sydevs-bot
sydevs-bot deleted the claude/refactor-222-sort-events branch September 22, 2026 16:24
@sydevs-bot sydevs-bot moved this from Approved to Done in SYDevelopers Technical Roadmap Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

refactor(list): move sortEvents into src/lib/shape/sort.ts

3 participants