Skip to content

feat(widget): nested region URLs and a unified Region model #26

Closed

Description

@Ardnived

Summary

SahajCloud now treats Countries, Regions, Areas, and Venues as one regions
collection with a level discriminator (country | region | city | center).
The widget still carries four parallel view-models, fetchers, page components,
and flat URL prefixes for them. This collapses all four into a single Region
abstraction — one schema, one fetcher, one page — and switches routing to
hierarchical, ancestry-based URLs so the URL mirrors the region tree
instead of a flat /areas/<slug> scheme.

New URL scheme (variable depth = the region's actual breadcrumb slug chain).
The region and venue levels are optional, so the same rule must handle every
depth, e.g. /belgium/antwerp (no region), /belgium/flanders/antwerp/12345
(event, no venue), /belgium/flanders/antwerp/downtown-hall/12345 (full chain):

  • Country: /belgium
  • Region: /belgium/flanders
  • Area (city): /belgium/flanders/antwerp — or /belgium/antwerp if no region
  • Venue (center): /belgium/flanders/antwerp/downtown-hall
  • Event: .../<region-chain>/12345 (numeric tail = event id)

Region slugs are globally unique across levels (confirmed). That is what
makes variable depth tractable: resolution reads only the terminal segment,
never the path shape. A numeric terminal is an event id; any other terminal is a
region slug. The intermediate country/region/city/venue segments are contextual
only — they need no positional parsing, so region-optional and venue-optional
URLs resolve identically.

Current behavior

  • src/types/{country,area,venue}.ts + the region view-models in
    src/types/region.ts define four nearly-identical shapes that differ only in
    the geo accessor (bounds vs center) and the child collection
    (children/areas: RegionListItem[] vs events: EventSlim[]).
  • src/config/api/fetch.ts has getCountry/getRegion/getArea/getVenue,
    each hard-coding a level into getRegionDoc(slug, level) and shaping a
    different schema; the aggregation helpers (countUnder, boundsUnder,
    eventsUnder) are already level-agnostic.
  • src/pages/{country,region,area,venue}.tsx are four Panel wrappers that
    fetch, set the map boundary/center, and render either a <List> of children
    or an <EventsList>.
  • Routing uses four flat prefixes via LEVEL_ROUTES in src/lib/shape/path.ts;
    regionPath(level, slug) and eventPath(id) build single-segment paths.

Proposed changes

Types — one Region view-model (src/types/region.ts)

  • Introduce a single view-model: id, slug, name, level, subtitle?, countryCode?, eventCount, bounds: BBox | null, center: Position | null, path, parentPath?, subregions: RegionListItem[], events: EventSlim[]. subregions is populated
    for country/region; events for city/center; center is derived for
    all (used by venue today); countryCode is set only for country.
  • Delete src/types/country.ts, src/types/area.ts, src/types/venue.ts; fold
    the home-list shape (today's CountrySlim) into RegionListItem (keep
    countryCode for the flag). Update the barrel src/types/index.ts.
  • Extend BreadcrumbSchema so each crumb carries the ancestor slug + level
    (backend breadcrumbs are { doc, url, label, id } where doc can populate to
    a full Region — see Data dependencies).

Data layer — one fetcher (src/config/api/fetch.ts)

  • Replace the four hierarchy fetchers with getRegion(slug): fetch the region
    doc by slug (level comes from the response, not a filter), derive
    eventCount/bounds/center, and populate subregions (via
    getChildRegions) when level ∈ {country, region} else events (via
    eventsUnder). Single query key ['region', slug].
  • Keep getCountries for the home/search list; return RegionListItems with
    countryCode.
  • Surface the ancestor slug chain so paths can be built (populate breadcrumb
    doc with slug/level, or walk parent), on both getRegionDoc and the
    geojson feed (REGION_POPULATE) so per-event paths in toSlim are nestable.

Path helpers — nested build + parse (src/lib/shape/path.ts)

  • Replace LEVEL_ROUTES + flat regionPath with a nested builder that joins the
    ordered ancestor slug chain: regionPath(region) → "/belgium/flanders/antwerp".
  • eventPath(event) appends the numeric id to its region's chain.
  • Add a parser resolvePath(pathname) → { kind: 'region', slug } | { kind: 'event', id } | null driven only by the terminal segment: all
    digits → event id; otherwise → region slug. Depth-independent, so every shape
    in the list above resolves through one code path (region slugs are unique and
    never purely numeric, so there is no region/event ambiguity).
  • Because intermediate segments are cosmetic, an incoming URL with a partial or
    stale ancestry still resolves; canonicalize by redirecting to the target's
    true breadcrumb path (regionPath(region)) so the address bar stays
    consistent. This also subsumes the legacy-flat-URL redirects.

Pages — one RegionPage (src/pages/region.tsx, delete the other three)

  • Branch on level: country/region render <List> of subregions;
    city/center render <EventsList events>.
  • Preserve today's per-level behaviors: fitBounds(bounds) when bounds exist,
    center-only moveMap fallback for center level, the venue redirect when
    < 2 events, mapWindow={240} for center, the country flag + localized
    name via countryCode, and the area subheader.
  • Keep EventPage; update its back link to the region's nested parentPath.

Routing (src/App.tsx)

  • Replace the four region routes + /events/:id with a splat route (path="*")
    that runs resolvePath and renders RegionPage or EventPage. Keep
    /search. Update initialPath (from client.region) to the nested path.
  • Update the cluster-click navigate in
    src/components/organisms/Mapbox/Map.tsx to build the nested event path from
    the feature's region ancestry.
  • Redirect the legacy flat paths (/{countries,regions,areas,venues}/:slug,
    /events/:id) to their nested equivalents — handled by the same
    canonicalization step (resolve terminal → redirect to regionPath/eventPath).

Data dependencies

  • Slug uniqueness across the regions collection: confirmed — terminal
    slug resolves exactly one region, so no positional/chain disambiguation is
    needed.
  • Ancestor slugs are retrievable — breadcrumb doc populates with
    slug/level (or reconstructable via parent) on both region reads and
    the geojson feed. Needed to build nested paths for regions and events.
    (Verify the populate shape against a live SahajCloud response.)

Acceptance criteria

  • A single Region zod view-model + getRegion(slug) fetcher replace
    Country/Area/Venue schemas and the four getCountry/Region/Area/Venue
    fetchers; src/types/{country,area,venue}.ts are removed and the barrel
    updated.
  • A single RegionPage renders all four levels (children list for
    country/region, events list for city/center) with the venue redirect,
    country flag/name, area subheader, and map framing preserved.
  • URLs are hierarchical and match the region ancestry for all four levels and
    for events (numeric tail); the <SearchBar> back button and event-page back
    link navigate to the correct parent path.
  • Variable-depth URLs all resolve via the terminal segment — region-optional
    (/country/city) and venue-optional (/country/city/12345) shapes work,
    and any incoming path canonicalizes (redirects) to the target's true
    breadcrumb path.
  • Map cluster clicks and the home/search country list navigate to correct
    nested URLs; initialPath (client home region) resolves to a nested path.
  • Legacy flat URLs (/countries|regions|areas|venues/:slug, /events/:id)
    redirect to their nested equivalents.
  • No regression to map boundary/center framing, event counts, or the
    online-only filter.
  • Zod parse errors still surface backend shape changes (no silent fallbacks).
  • pnpm lint && pnpm typecheck && pnpm test:run && pnpm build all pass.

Files affected

  • src/types/region.ts, src/types/index.ts (+ delete country.ts,
    area.ts, venue.ts)
  • src/config/api/fetch.ts
  • src/lib/shape/path.ts (+ src/lib/shape barrel)
  • src/pages/region.tsx (+ delete country.tsx, area.tsx, venue.tsx),
    src/pages/event.tsx
  • src/App.tsx, src/components/organisms/Mapbox/Map.tsx

References

  • Backend Region type: src/types/payload/payload-types.ts (level,
    breadcrumbs, slug)
  • Level→route mapping being replaced: src/lib/shape/path.ts
  • Ancestry aggregation helpers (reused as-is): src/lib/shape/hierarchy.ts

Activity

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

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions