Repository navigation
Closed
Description
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'swebPathis published-gated and can be null. src/main.tsxdetects/preview, capturescollection/id/secretinto a new module singletonsrc/config/preview.ts(same pattern assrc/config/api/auth.ts), and strips the secret from the address bar viahistory.replaceStatebefore render.- Add
'preview'toRESERVED_SLUGSinsrc/lib/shape/path.tsso the boot pathname never resolves as a region slug. The home-region redirect (App.tsx:89-98) only fires onpathname === '/', so it doesn't conflict. - Boot: fetch the raw draft doc by id, derive its canonical path (
safePath(webPath) ?? /${id}, mirroringfetch.ts:345), andnavigate(path, { replace: true })— from there the normalresolveStack/DrawerStackmachinery renders map + drawer unchanged.
Draft fetching
- In the existing request interceptor (
src/config/api/client.ts:53-61), when preview is active, adddraft=trueand thex-sahajcloud-preview-secretheader to every request. Published-only reads ignoredraftharmlessly; 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-loadedPreviewControllercomponent (mounted fromAppShellonly when preview mode is active — zero cost otherwise) runsuseLivePreview(handles thereadyhandshake + message stream), re-shapes each incoming doc through the same shaping code the fetchers use, and injects it withqueryClient.setQueryData(['event', id], …)/['region', slug].EventView/RegionViewre-render with zero changes, including live map re-framing (useFrameOnTopdeps include the doc). - Extract
getEvent's post-parse shaping insrc/config/api/fetch.tsinto an exportedshapeEventDoc(doc); usesafeParsein 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 shapedRegion— 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'smergePreviewDatapattern) so live messages that collapseimages/regionto bare IDs don't drop the populated objects.
Link inerting (no per-component prop threading)
- Capture-phase
click/auxclickanchor guard (WeMeditateWebusePreviewLinkGuardpattern):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 areButton 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
RegistrationViewwhen preview is active, so previewing can't create real registrations against a draft event (the drawer itself still opens).
Mode plumbing
- Add
preview: booleantoWidgetMode(src/config/mode.ts) and thread it throughApp.tsxlikestandalone/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 (
PreviewControlleris 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.tssrc/config/api/client.ts,src/config/api/fetch.tssrc/lib/shape/path.tspackage.json(@payloadcms/live-preview-react)
References
- Companion SahajCloud issue: feat(admin): add live preview for regions and events via sahaj atlas SahajCloud#575
- Related: SDK-migration issue refactor(api): migrate data layer from axios+qs to @payloadcms/sdk #41 — if it lands first, the draft-fetch seam becomes the SDK client factory (per-client preview headers, as in WeMeditateWeb's
createPayloadClient) instead of the axios interceptor; neither ticket blocks the other - WeMeditateWeb reference implementation:
pages/preview/(useLivePreview,previewNavigation.tslink guard,mergePreviewData.ts) - SahajCloud draft-unlock contract:
src/plugins/access/accessConfigs.ts,src/lib/utilities/previewSecret.ts
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Hold Until
None yet
Activity
refactor(preview): drop the WidgetMode.preview axis, read the singleton
refactor(preview): authed populate round-trip, drop the live-preview dep
feat(widget): add sahajcloud live preview for region and event views (#…