Skip to content

feat(widget): add sahajcloud live preview for region and event views #40

Closed

Description

@Ardnived

Summary

Support PayloadCMS live preview from the SahajCloud admin for the Region and Event
drawers. When an editor opens the Live Preview tab on a region or event, the admin
iframe loads the standalone Atlas deployment, which renders the full widget
experience — map and drawer — for the doc being edited (including unpublished
draft events) and updates live as the editor types, without saving or reloading.
In event preview, the Register and Share buttons stay interactive (their drawers
open); every other link is inert.

Proposed changes

Preview entry (/preview bootstrap route)

  • The CMS iframe opens {SAHAJATLAS_URL}/preview?collection=regions|events&id=<id>&secret=<SAHAJCLOUD_PREVIEW_SECRET> (same URL shape as WeMeditateWeb's Pages/Meditations previews). A canonical-path URL is not viable: a draft event's webPath is published-gated and can be null.
  • src/main.tsx detects /preview, captures collection/id/secret into a new module singleton src/config/preview.ts (same pattern as src/config/api/auth.ts), and strips the secret from the address bar via history.replaceState before render.
  • Add 'preview' to RESERVED_SLUGS in src/lib/shape/path.ts so the boot pathname never resolves as a region slug. The home-region redirect (App.tsx:89-98) only fires on pathname === '/', so it doesn't conflict.
  • Boot: fetch the raw draft doc by id, derive its canonical path (safePath(webPath) ?? /${id}, mirroring fetch.ts:345), and navigate(path, { replace: true }) — from there the normal resolveStack/DrawerStack machinery renders map + drawer unchanged.

Draft fetching

  • In the existing request interceptor (src/config/api/client.ts:53-61), when preview is active, add draft=true and the x-sahajcloud-preview-secret header to every request. Published-only reads ignore draft harmlessly; the secret disables server caching, which is correct for preview sessions.

Live updates

  • Add @payloadcms/live-preview-react (pinned to the CMS 3.x line). A lazy-loaded PreviewController component (mounted from AppShell only when preview mode is active — zero cost otherwise) runs useLivePreview (handles the ready handshake + message stream), re-shapes each incoming doc through the same shaping code the fetchers use, and injects it with queryClient.setQueryData(['event', id], …) / ['region', slug]. EventView/RegionView re-render with zero changes, including live map re-framing (useFrameOnTop deps include the doc).
  • Extract getEvent's post-parse shaping in src/config/api/fetch.ts into an exported shapeEventDoc(doc); use safeParse in the preview path and keep the last good doc when mid-edit form state is transiently invalid.
  • Regions (no drafts): overlay editable scalars (name, subtitle, level) onto the cached shaped Region — lists/counts/bounds are geojson-derived and can't change from form edits.
  • Add a sparse-relation merge util (src/lib/preview/merge.ts, WeMeditateWeb's mergePreviewData pattern) so live messages that collapse images/region to bare IDs don't drop the populated objects.

Link inerting (no per-component prop threading)

  • Capture-phase click/auxclick anchor guard (WeMeditateWeb usePreviewLinkGuard pattern): preventDefault() every <a> except same-page #hash. This inerts exactly the required set — RegionCard/EventCard hrefs, directions/online/tel: links (EventDetails/details.tsx), sanitized-description links, share-target links, and the external-registration <Button href target="_blank"> (an anchor — deliberately inert per product decision). Internal Register and Share are Button onClick={() => navigate(…)} (EventDetails.tsx:61,78), not anchors — untouched, so their drawers open.
  • Route lock: watch location.pathname; if it leaves the allowed set — event: {path, path/register, path/share}; region: {path} — navigate(previewPath, { replace: true }). Closing register/share pops back to the event; dismissing the previewed drawer itself snaps back instead of stranding the preview on a parent view.
  • Disable the final submit button in RegistrationView when preview is active, so previewing can't create real registrations against a draft event (the drawer itself still opens).

Mode plumbing

  • Add preview: boolean to WidgetMode (src/config/mode.ts) and thread it through App.tsx like standalone/hasMap.

Acceptance criteria

  • Opening Live Preview on an Event in the SahajCloud admin renders the full widget (map + event drawer) framed on the event — including unpublished/draft events
  • Opening Live Preview on a Region renders the map + region drawer at the region's bounds
  • Editing title/description/schedule/address updates the drawer (and map framing for address changes) without saving or reloading
  • Register opens the registration drawer and Share opens the share drawer; closing either returns to the event preview; the registration submit button is disabled in preview
  • All other links are inert in preview: subregion/event cards, directions/online/tel links, description-HTML links, share-target links, and the external-registration button
  • Dismissing the previewed drawer (close button, swipe, peek strip) snaps back to the previewed doc rather than leaving the preview
  • The preview secret is stripped from the address bar after boot and never appears in the bundle; preview mode adds zero behavior/bundle cost to normal standalone/embedded use (PreviewController is lazy-loaded)
  • Lint + typecheck + unit tests + build pass
  • Verified end-to-end against a local SahajCloud with the companion CMS change applied

Known limitations (accepted)

  • The clustered event-dot layer and region bounds/counts come from the published-only geojson feed — an unpublished event contributes no cluster dot (the selection pin + framing still work from the doc's own coordinates), and a region with zero published events won't frame.
  • A brand-new unsaved doc has no id; the boot path shows a "save first" fallback instead of crashing (standard Payload limitation).
  • An image added mid-edit arrives as a bare media ID; the sparse merge keeps prior images and the new one appears after save + reopen.

Files affected

  • src/config/preview.ts (new), src/components/preview/PreviewController.tsx (new), src/lib/preview/merge.ts (new)
  • src/main.tsx, src/App.tsx, src/config/mode.ts
  • src/config/api/client.ts, src/config/api/fetch.ts
  • src/lib/shape/path.ts
  • package.json (@payloadcms/live-preview-react)

References

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