Skip to content

feat(search): offer a country website when no programs are listed - #84

Merged
Ardnived merged 6 commits into
mainfrom
feat/country-site-offer
Jul 30, 2026
Merged

Ardnived merged 6 commits into
mainfrom
feat/country-site-offer

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • A search landing in a country with no Sahaja Yoga programs used to dead-end on "No events found." Most such countries have a national Sahaja Yoga website with local contact details, so the empty state now offers it — flag, localized country name, and an external link.
  • The offer is deliberately narrow: it needs the searched place to resolve to an ISO country, that country to have zero events in the feed (genuinely absent, not filtered out), and to be one of the 95 in the mapping. Every other empty state is untouched.
  • The mapping ships as static data (src/lib/country-sites.ts, scraped from shrimataji.org/map/world.html) — no fetch, no CMS round trip.

Changes

  • src/lib/country-sites.ts — 95 uppercase-ISO → URL entries plus a case-normalizing countrySite() accessor, mirroring the existing PLATFORMS_BY_COUNTRY / platformsForCountry in lib/share/platforms.ts.
  • src/lib/shape/country.ts — countryHasPrograms (a region-subtree test over the full feed) and the canonical isoCountryCode, moved here from config/api/fetch.ts so the data layer, the ?cc reader and the IP guess share one guard.
  • src/lib/geocode.ts — reads country_code off the Mapbox geocode context, which the vendor types omit.
  • src/hooks/use-country-site.ts — composes the whole trigger (mirrors useRegionMatcher), so EmptyResults stays presentational.
  • src/lib/shape/path.ts — SEARCH_COUNTRY_PARAM + parseCenter beside searchPath, which already claimed to co-own the ?center=lng,lat format.
  • src/config/api/index.ts — an eventsQuery factory, so the results list and the SearchView story build one key.

Verification

  • Lint: ✓ No errors (pnpm lint)
  • Types: ✓ Clean (pnpm typecheck)
  • Tests: ✓ Unit lane green (pnpm test:run) — 463 tests
  • Build: ✓ pnpm ladle:build clean; the production build runs in CI

Manual / visual verification

Driven in the running widget (both /search and ?map=0), against the seeded local SahajCloud — which has 25 countries with programs and no Iceland:

Case Result
Iceland (?cc=IS) Offer → sahajayoga.is, target="_blank" rel="noopener noreferrer"
Reykjavík via the live Mapbox geocoder Same offer — country came from context.country, not a country-level result
Vienna after Iceland (live geocoder) ?cc replaced IS → AT; Austria has 30 classes, so no offer
Acre, Brazil (?cc=BR) "No events within 500 km." — Brazil has programs
Iceland + ?langs=zz "No events match your filters." + Clear all
Iceland + ?region=gb "No events within 500 km." — no wrong-country offer
Angola (?cc=AO, no site) Unchanged
?cc=ISL (malformed) Unchanged — guard rejects it
Apply / clear a filter ?cc preserved; offer returns

Both new Ladle cases render, including switching between all four Example values in sequence.

Notes for reviewer

One ordering call worth understanding. The offer is checked before the "< 500 km" branch. getEvents returns the nearest N events with no distance limit, so a program-less country's nearest results are typically ~1900 km away — the cap fires for virtually every such search and would bury the offer entirely. Verified: the plain Iceland search does show the cap pill.

Two deviations from the ticket, both deliberate:

  1. The ticket proposed src/data/country-sites.ts; the repo already homes exactly this kind of static country-keyed table in src/lib/ (share/platforms.ts), so a new top-level tier wasn't warranted.
  2. The ticket proposed an optional search prop on ViewHarness wrapping a nested MemoryRouter. react-router v7 throws on a nested <Router> — which is why src/components/ladle/SeedSearchParams.tsx exists and CalendarView.stories.tsx already uses it for a view. The stories use that instead.

The ticket's trigger list omitted filters; two review passes flagged the gap. The offer now stands down while any filter is active. That restores the "Clear all" affordance to the state that owns it (the offer arrives one click later), and it closes a real bug: a filter edit deliberately preserves ?cc, so searching Reykjavík and then filtering Region = United Kingdom emptied the list because of the filter while still offering Iceland's website.

Security review (triggered — src/config/api/ changed): no High/Critical. The ?cc → href chain is allowlisted end to end and config/api/ changed only mechanically. Two findings acted on:

  • The source page publishes 57 of the 95 as plain http://. All 57 were probed over HTTPS; the 43 that answer 200 without downgrading are now stored as https://. These are presented as a country's authoritative contact point and several carry join-a-class forms, so a plaintext hop let a hostile network serve a convincing fake. The 14 with no working HTTPS stay as published rather than becoming dead links, and a test pins that list so a refresh has to re-probe.
  • referrerPolicy="no-referrer" on the flag <img> (it loads from react-circle-flags' CDN, so the host page's URL was riding along). Applied to CountriesView's flag too, so the two don't drift.

Knowingly not fixed:

  • Six entries sit on re-registrable free-tier subdomains (*.weebly.com, *.blogspot.com, freeservers.com). The real fix is the ticket's own out-of-scope item — moving the mapping into SahajCloud so it's editable without a redeploy. Documented in the module.
  • ?cc isn't cross-checked against ?center, so a hand-crafted ?center=<Berlin>&cc=IS mislabels the country. Unclosable here by design: a program-less country has no feed features and so no geometry to resolve a point against, which is why the geocoder's answer has to ride in the URL. The destination is always one of the 95, so it's confusing rather than a redirect.
  • Pre-existing, worth its own ticket: the host-supplied locale prop (and ?locale=) reaches i18n.changeLanguage unvalidated, so a bad tag throws RangeError out of Intl.DisplayNames and drops the whole widget to the ErrorBoundary. supportedLngs in config/i18n.ts would close it.

Closes #82

🤖 Generated with Claude Code

Ardnived and others added 6 commits July 29, 2026 08:56
A search landing in a country with no Sahaja Yoga programs dead-ended on
"No events found." Ship shrimataji.org's national-site list as static data
and, when the searched country is genuinely empty, offer its own site.

- `src/data/country-sites.ts` — 95 uppercase ISO alpha-2 → URL entries
- `src/lib/geocode.ts` — the guarded `?cc` reader; hosts the canonical
  `isoCountryCode` (fetch.ts now shares it instead of a private copy)
- `src/lib/shape/country.ts` — `countryHasPrograms`, a region-subtree test
  over the full feed; an unknown country resolves to NO programs (which is
  why it can't reuse `buildRegionMatcher`)
- `CountrySiteOffer` molecule on the existing Alert + Link atoms
- `EmptyResults` gates offer → distance cap → filters: the offer must win,
  since a program-less country's nearest results are usually beyond the
  500 km cap and would otherwise bury it
- `?cc` written by SearchField + the accepted geolocation suggestion; the
  re-encode in `preserveSearchState` drops it, so it never leaks forward

Closes #82

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The two SearchView cases both need a URL, so they seed one onto the
decorator's own router via the existing SeedSearchParams helper (react-router
v7 throws on a nested <Router>, which is why that helper exists) and derive
their query key from the same string — seed and read can't drift.

Adds `mockRegionNodes`, the wholesale region tree the harness now seeds for
every view story. Country slugs are lowercase ISO codes, matching the live
data post-SahajCloud#556 — the invariant `countryHasPrograms` looks a country
up by — and Iceland is deliberately absent.

Closes #82

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Applied the cleanup findings from the four review lenses:

- Fixed a real gap the simplification lens caught: the empty-state gate
  guarded `geojson` but NOT `regions`, so a loaded feed with an unloaded
  region tree made `countryHasPrograms` answer false and offered a foreign
  website to a country that HAS classes. The (now-false) comment claimed the
  guard was unnecessary. Both reads are required-loaded in one place.
- `src/data/` → `src/lib/country-sites.ts` with a case-normalizing
  `countrySite()` accessor, mirroring the existing `PLATFORMS_BY_COUNTRY` /
  `platformsForCountry` in `lib/share/platforms.ts` — the repo already homes
  this exact kind of static country-keyed table there, so `src/data/` was a
  new tier for nothing.
- Extracted `useCountrySite` (src/hooks/), mirroring `useRegionMatcher`:
  the URL read + table lookup + two cache reads + the gate leave the
  organism, so `EmptyResults` is presentational again. Dropped the two
  `enabled` flags with it — both keys already have ungated observers on this
  screen (ActiveFilterPills, GeolocationSuggestion), so they saved nothing.
- Added the `eventsQuery` factory (config/api) so the list's suspense read
  and the story's cache seed build ONE key — the drift the story's comment
  claimed to prevent was still two constructions.
- `parseCenter` moved beside `searchPath` (lib/shape/path.ts), whose docblock
  already claimed to own the `?center=lng,lat` format jointly with it;
  `SEARCH_COUNTRY_PARAM` named there too, replacing three `'cc'` literals.
- `isoCountryCode` moved from the Mapbox-shaped `lib/geocode.ts` to
  `lib/shape/country.ts`, so the data layer no longer imports a geocoder
  module for a string guard — and `useViewerCountry` now uses it too, which
  stops an unvalidated `"usa"` reaching `platformsForCountry`.
- `countryHasPrograms` takes required inputs (loading is the caller's
  concern); mock Cambridge derives from the feed's own region ref.

Skipped: rebuilding `countryHasPrograms` on `buildRegionMatcher` — behaviour
is identical today, but it would couple "has programs" to the filter's
unknown-slug policy, which must stay opposite. Skipped a `geojsonQuery()`
factory (7 pre-existing inline copies — its own PR).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Two independent review passes (correctness + git-history) converged on the
filters interaction, and the history pass found the commit whose affordance
the first cut dropped:

- The offer now stands down while ANY filter is active (076bf43's "clear all
  filters" escape hatch is that state's, so the offer waits for a clear
  rather than replacing it). This also kills a wrong-country offer: a filter
  edit deliberately preserves `?cc`, so searching Reykjavík and then
  filtering Region=United Kingdom used to empty the list *because of the
  filter* and still offer Iceland's website. It stays ahead of the "< N km"
  branch, which is what makes the feature reachable at all.
- `countryHasPrograms` now requires the matched node to be `level: 'country'`
  — slugs are unique across the whole tree, not per level, so a two-letter
  city slug (`as`) would otherwise answer for its namesake country.
- Memoized the feed scan: the geocoder rewrites `?q` per keystroke, so the
  region index + subtree set were rebuilt once per character typed.
- `RegionedFeature.region` is required, matching the feed schema
  (`AgnosticFeedEventSchema.region` is non-nullable) — the old `null` branch
  and its test asserted a shape that can't occur. Replaced with the real
  case: an online class filed under a country DOES count as a program.
- Story fixtures: seed every case's key (switching the Example control paired
  the previous case's URL with the new case's client for one render, missing
  cache and hitting the absent backend); `mockIpLocation` carries a
  `country_code`, so the second `?cc` writer has coverage.
- Copy uses Alert's `description` slot, not `title`, so it renders at the
  weight of the empty states it replaces.
- Corrected two now-false comments: the FilterView story's "no region tree
  here" (the harness seeds one now) and the `http://` count (57, not 45).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…rrer leak

From the security review (no High/Critical; the `?cc` → href chain is
allowlisted end to end, and src/config/api/ changed only mechanically).

- The source page publishes 57 of the 95 sites as plain `http://`. Probed all
  57 over HTTPS: 43 answer 200 without downgrading in the redirect chain, so
  those are now stored as `https://`. The widget presents these as a
  country's authoritative contact point — several carry join-a-class forms —
  so a plaintext hop on a hostile network let an attacker serve a convincing
  fake while borrowing the trust the widget just lent it. The 14 with no
  working HTTPS stay as published rather than becoming dead links, and a test
  pins that list so a future refresh has to re-probe rather than regress.
- `referrerPolicy="no-referrer"` on the flag `<img>`, which loads from
  react-circle-flags' own CDN — the embedding host's URL was riding along to
  a third party on every render. Applied to CountriesView's flag too, so the
  two surfaces don't drift.
- `COUNTRY_SITES` is typed `string | undefined`: with
  `noUncheckedIndexedAccess` off a bare index typed as `string` while
  returning undefined, and the story was doing exactly that.
- Two anchoring cases for `isoCountryCode` — it's the only gate between an
  attacker-supplied `?cc` and both the record index and the CDN path segment.

Noted and deliberately not changed: six entries sit on re-registrable
free-tier subdomains (the fix is the ticket's own out-of-scope "move the
mapping into SahajCloud"); `?cc` isn't cross-checked against `?center`, which
is unclosable here by design — a program-less country has no geometry to
resolve a point against, which is why the code rides in the URL at all.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ag CSP

- README: `img-src https://react-circle-flags.pages.dev` in the embedding-host
  CSP section, which only covered Turnstile — a host with a strict `img-src`
  silently lost the country flags. Notes what blocking it costs (glyphs only)
  and that the requests are `no-referrer`.
- `.claude/rules/i18n-and-state.md`: the searched-location params as their own
  URL slice, including `?cc` — why they're read raw rather than codec'd
  (replaced by a new search, not merged) and why the country can't simply be
  re-derived at the read site.
- `.claude/rules/data-layer.md`: the `eventsQuery` factory beside `eventQuery`.
- `DESIGN_SYSTEM.md`: the `CountrySiteOffer` molecule row.
- `CLAUDE.md`: added the missing `src/lib/` entry — it's where the static
  country-keyed tables live, which is the question this branch had to answer —
  and corrected `config/api/`'s stale "axios client" (it's the Payload SDK).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sahajatlas-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9cd73f7
Status: ✅  Deploy successful!
Preview URL: https://cd35e5ad.sahajatlas-design.pages.dev
Branch Preview URL: https://feat-country-site-offer.sahajatlas-design.pages.dev

View logs

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9cd73f7
Status: ✅  Deploy successful!
Preview URL: https://c1a74be0.sahajatlas.pages.dev
Branch Preview URL: https://feat-country-site-offer.sahajatlas.pages.dev

View logs

@Ardnived
Ardnived merged commit 02d0c0b into main Jul 30, 2026
4 checks passed
@Ardnived
Ardnived deleted the feat/country-site-offer branch July 30, 2026 15:08
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.

feat(search): offer a country website when no programs are listed

1 participant