Skip to content

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

Closed

Description

@sydevs-bot

Summary

sortEvents and calculateOrder are pure list ordering living in a data-connected component (src/components/organisms/EventsList/DynamicEventsList.tsx:45-88). src/lib/shape/sort.ts already owns the sort order — the SortOrder type, SORT_ORDERS, DEFAULT_SORT and the ?sort= codec — and AGENTS.md:165 puts pure domain helpers there, "no React or i18n".

Deferred out of #221's review thread. Filed so the deferral outlives that PR body.

⚠ This ticket described #221's branch, not main

Three of the original premises hold only on claude/feat-220-unverified-badge (#221, open and still draft):

Premise as filed On main
sortEvents is exported only so its spec can reach it Not exported. DynamicEventsList.tsx:71, module-private, as is calculateOrder at :45
DynamicEventsList.sort.test.ts imports a component module to test a pure function The file does not exist
recommended carries an unverified ×1.5 factor Three factors only: language ×2, soon ×0.5, online ×1.5

So the work cannot start before #221 merges — it would conflict inside the same function. See the Blocked by: line.

Approach

The i18n coupling is one line, not a signature change to calculateOrder. The original body said calculateOrder reads i18n.resolvedLanguage directly. It does not, on either branch: it already takes language: string | undefined as its second parameter (DynamicEventsList.tsx:45; :46 on #221's branch). The single read sits in sortEvents's default arm — :80, and :83 on the branch — which is what forces the spec's vi.mock('@/config/i18n'), and it is the only use of the i18n import at :32.

  1. Thread the language through sortEvents. sortEvents(events, order, language); delete the read at :80 and the import at :32. DynamicEventsList already holds the value — useLocale() at :101 — so this adds no hook.
  2. Add it to the memo deps. :116 is useMemo(() => sortEvents(events, order), [events, order]), becoming [events, order, language]. Today a language switch re-sorts only indirectly: eventsQuery(latitude, longitude, filters, locale) at :109 keys on the locale, so switching yields a new events reference. Making the dependency explicit costs nothing and stops that being load-bearing.
  3. Move both functions into src/lib/shape/sort.ts with their comments, and drop feat(widget): badge unverified listings before registration #221's export from DynamicEventsList.
  4. Fold the spec into src/lib/shape/sort.test.ts. Mechanical: the branch spec's vi.mock('@/config/i18n', () => ({ default: { resolvedLanguage: 'en' } })) becomes a third argument, 'en'. mockEventSlim.languages is ['en'] (src/mocks/events.ts:75), so those fixtures carry no language penalty either way and the assertions port unchanged.

⚠ Which value gets passed is the decision this turns on

The original body offered "useLocale()'s value (or i18n.resolvedLanguage, matching today's behaviour exactly)" as interchangeable. They are not. useLocale().locale is i18n.resolvedLanguage || 'en' (src/hooks/use-locale.ts:72), and the penalty is a loose compare, if (language != languageCode) order *= 2:

passed for an event whose languages[0] is 'en'
i18n.resolvedLanguage, before it resolves (undefined) undefined != 'en' → ×2
useLocale().locale → 'en' no penalty

i18n.resolvedLanguage is the value that satisfies the byte-identical criterion. useLocale().locale changes the ordering in the window before a locale resolves.

Acceptance criteria

  • sortEvents and calculateOrder live in src/lib/shape/sort.ts. DynamicEventsList exports neither.
  • Neither function imports @/config/i18n, and DynamicEventsList.tsx no longer imports it either. The resolved language is a parameter.
  • The value passed is i18n.resolvedLanguage, not useLocale().locale and not languageCode, unless the open question below is settled otherwise.
  • language is in the useMemo dependency list at DynamicEventsList.tsx:116.
  • src/lib/shape/sort.test.ts covers the three branches with no vi.mock of @/config/i18n, and DynamicEventsList.sort.test.ts is deleted.
  • The three orderings are byte-identical for the same input: recommended keeps every factor and weight — language ×2, soon ×0.5, online ×1.5, and feat(widget): badge unverified listings before registration #221's unverified ×1.5 — and closest and soonest stay the literal comparators.
  • The list still re-sorts without refetching when ?sort= changes.
  • pnpm lint, pnpm typecheck, pnpm test:run pass.

Verification checklist

  • pnpm test:run.
  • Reintroduce each moved factor's defect at its new site in sort.ts and watch the spec go red — a moved assertion is a new assertion (docs/testing.md).
  • Pass undefined as the language in a spec case and assert the ×2 penalty still lands on an 'en' event. This is what pins criterion 3: a spec that only ever passes 'en' cannot see the difference.
  • Drop language from the :116 memo deps and confirm something goes red. If nothing does, that dependency is untested.
  • pnpm dev, search a place, then switch Recommended / Closest / Soonest: the order matches main's, and the network tab shows no refetch.

Open questions

  • Keep the en-US quirk, or fix it here? useLocale() also returns languageCode (locale.split('-')[0], use-locale.ts:109). Passing that would fix a real pre-existing bug — an en-US browser gets 'en-US' != 'en' → ×2 on every English event — but it changes the ordering, so it cannot land under the byte-identical criterion above. Fix it here and relax that criterion, or keep today's behaviour and file it separately?

Notes

Blocked by: #220 — #221 adds the export, the spec file, and the unverified factor that this ticket moves.

  • No host-facing contract change: nothing here is observable through <sahaj-atlas>'s attributes, origins or URL shape, so docs/embedding.md and CHANGELOG.md stay untouched. A visitor sees no reordering either — that is the point.
  • The "no React or i18n" rule for src/lib/ is in the root AGENTS.md:165 (CLAUDE.md symlinks to it), not src/AGENTS.md as first cited. src/lib/ has no runtime @/config/i18n import today, so sort.ts reaching for one would be the first.
  • revealRows owns segmenting and slicing after the sort — DynamicEventsList.tsx:138-141, not :71-72 as first written, which points inside sortEvents. Out of scope. Do not move it in the same PR.
  • The original body framed step 1 as moving the read "from once per sortEvents call to once per render of the list". sortEvents already runs only inside the :116 memo, and useLocale() at :101 is already subscribed, so neither shape costs anything. The decision above is the real review surface.

Related

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

Activity

  1. added theissue type on Sep 21, 2026
  2. added
    proposal
    bot:working
    on Sep 21, 2026
  3. sydevs-bot commented on Sep 21, 2026

    @sydevs-bot
    ContributorAuthor

    🤖 no session running

    recent dispatches
    • 03:58Z implement · attempt 1 · session · done
    • 03:58Z implement · attempt 1 · session
    • 23:31Z revise · attempt 1 · session · done
    • 23:31Z revise · attempt 1 · session

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

  4. sydevs-bot commented on Sep 21, 2026

    @sydevs-bot
    ContributorAuthor

    Revised against the code. Three premises here described #221's branch, not main.

    On main neither function is exported, DynamicEventsList.sort.test.ts does not exist, and there is no unverified factor. All three arrive with #221. Added a Blocked by: line on #220 — starting first would conflict inside the same function.

    Corrected the central claim. calculateOrder does not read i18n.resolvedLanguage. It already takes language as a parameter. The read is one line in sortEvents (:80). Step 1 threads one argument.

    Added the decision the ticket was missing. useLocale().locale is i18n.resolvedLanguage || 'en', so the two are not interchangeable. An unresolved locale penalises an 'en' event ×2. A resolved 'en' does not. Criterion 3 now names the byte-identical value.

    One open question: an en-US browser penalises every English event today. Fixing it changes the ordering.

    Effort stays Easy, Priority Low.

    @sydevs-bot implement starts the work. This revision authorises nothing.

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


    Generated by Claude Code

  5. added and removed
    bot:working
    on Sep 21, 2026
  6. moved this from Proposed to Revising in SYDevelopers Technical Roadmapon Sep 21, 2026
  7. sydevs-bot commented on Sep 22, 2026

    @sydevs-bot
    ContributorAuthor

    @Ardnived unblocked: every blocker is closed. Say @sydevs-bot implement to start.

    🤖 sydevs dispatcher

  8. Ardnived commented on Sep 22, 2026

    @Ardnived
    Contributor

    @sydevs-bot implement

  9. added
    bot:working
    and removed
    proposal
    on Sep 22, 2026
  10. moved this from Revising to Approved in SYDevelopers Technical Roadmapon Sep 22, 2026
  11. removed
    bot:working
    on Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    Low

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions