Skip to content

feat(widget): open a draft preview session from any view path #206

Closed

Description

@sydevs-bot

Summary

A translator cannot preview an unsaved translation. SahajCloud's Live Preview repoints the panel at a real view per tab — /search, /filters, /calendar (sydevs/SahajCloud#773, open) — but the widget unlocks drafts only when the boot URL is exactly /preview, so every repointed panel renders published copy.

Approach

  • The secret is the signal. readPreviewParams (src/config/preview.ts:44) returns null for any pathname but PREVIEW_PATH. Replace that pathname gate with a secret gate: a session exists when the query carries a non-empty secret, on any route. No preview=1 flag — nothing else unlocks a draft, so the secret's presence is the request. /preview stays the document boot route that also carries collection and id, and stays in RESERVED_SLUGS (src/lib/shape/path.ts:238) so it never reads as a region.
  • State this behaviour change out loud. /preview with no secret stops being a session. Today it returns { active: true, …nulls } and the controller paints PreviewFallback over the screen — the defect feat(fields): declarative live-preview targets, repointed by the open tab SahajCloud#773's critic round found ("the Atlas base URL landed on an overlay"). After this it renders the base view, via resolveStack's reserved-slug skip (src/lib/shape/path.test.ts:188). src/config/preview.test.ts:32 pins the old behaviour and must be rewritten.
  • Keep the scrub, narrow it. capturePreview (src/config/preview.ts:64) history.replaceStates to PREVIEW_PATH, dropping the whole query and any hash. It must delete only secret and re-write pathname + search + hash. Losing the hash was invisible while the target was always /preview; on a real view it is an observable side-effect. The secret still lives in memory only. src/main.tsx:28's comment needs rewording — the key/map read order above it still holds.
  • A document-less session must not take the screen. PreviewController (src/components/preview/PreviewController.tsx:253,257) falls through to PreviewFallback — "Save this document to preview it." over a click-swallowing fixed inset-0. Distinguish "the URL named no document" from "named one we cannot boot": readPreviewParams must record that distinction itself (a document flag — collection is nulled for an unrecognised value, so it cannot carry it). Render null for the first, keep PreviewFallback for the second.
  • Scope the two session-wide hooks to a document session. usePreviewLinkGuard and usePinnedPreviewQueries run ahead of the dispatch (PreviewController.tsx:248-249). The link guard inerts every <a> so a preview cannot leave the previewed doc; a translations session has no doc to leave, and an inert widget stops a translator reaching the screen a string lives on. The pinned ['event']/['region'] defaults protect live-overlaid docs a translations session never has.
  • Standalone only. capturePreview is called from src/main.tsx:28 and nowhere else. Keep it there: src/Widget.tsx must never read secret off a host page's URL. This ticket widens the route, never the entry.
  • Nothing else is needed to reach drafts. applyRequestContext (src/config/api/client.ts:45) already attaches x-sahajcloud-preview-secret and draft=true once preview.active && preview.secret. On the CMS side hasValidPreviewSecret (SahajCloud src/lib/utilities/previewSecret.ts) compares against SAHAJCLOUD_PREVIEW_SECRET, so a wrong secret is simply not a preview — drafts stay locked and the client select/populate gate still applies. A bad value in a shared URL adds no failure mode.

Acceptance criteria

  • readPreviewParams('/search', '?secret=…') returns an active session with a null collection and id.
  • readPreviewParams('/preview', '') returns null — no secret, no session. This replaces today's active-with-nulls.
  • /preview?collection=events&id=…&secret=… behaves exactly as it does today.
  • After boot the address bar carries no secret, and the pathname, the remaining query parameters and the hash are unchanged.
  • With a session active on /search, every SahajCloud request carries the secret header and draft=true.
  • A saved-but-unpublished sy-atlas-translations draft renders in the widget on a view path.
  • A document-less session renders no PreviewFallback, no link guard and no pinned query defaults.
  • src/Widget.tsx never activates preview from the host page's query string.

Verification checklist

  • pnpm test:run — new readPreviewParams and capturePreview cases plus the rewritten preview.test.ts:32, each watched failing against the old behaviour first (docs/testing.md: a regression spec is not finished until it has failed).
  • pnpm lint && pnpm typecheck && pnpm build && pnpm size.
  • Open <cloudflare-preview>/search?secret=<SAHAJCLOUD_PREVIEW_SECRET> by hand and confirm a saved draft string renders. ⚠ The CMS cannot produce this URL yet — see Downstream impact.
  • Read the address bar after that boot: no secret, still /search.
  • Open <cloudflare-preview>/preview with no query: the base country list renders, with no "Save this document to preview it." overlay.

Open questions

  • Should a translations session leave in-widget links live, as the Approach assumes, or keep them inert so the panel stays on the tab's target?
  • Does the scrub keep collection/id in the address bar on /preview? This assumes yes — they are not credentials.

Downstream impact

sydevs/SahajCloud#773 must follow with the secret restored to both translations livePreview.url builders. Its critic round removed it because "the one route that does read one is also the only one that scrubs it" — this ticket makes every route scrub it, which is what re-earns it. translations-globals.int.spec.ts currently asserts the opposite, that locale is the only parameter either URL sends, so that follow-up flips the assertion. It lands after this.

Notes

  • The previous revision's "unsaved edit" criterion was wrong, and this ticket no longer claims it. sy-atlas-translations declares versions.drafts with no autosave (SahajCloud src/globals/SahajAtlasTranslations/SahajAtlasTranslations.ts:13), so draft=true returns the last saved draft, never the admin's in-form state. In-form edits arrive only as payload-live-preview messages, and usePreviewMessages is mounted solely by the event and region controllers, through populatePreviewDoc, typed 'events' | 'regions' and unable to take a global. Rendering a genuinely unsaved translation needs a handler that shapes form state back through i18n.addResourceBundle (src/config/locale.ts:111) — a different mechanism, and its own ticket.
  • /preview cannot be retired alongside this change, asked on the thread. The route carries two jobs and this removes only the draft gate. Naming the document stays: webPath is virtual and publish-gated (SahajCloud src/fields/publicUrlFields.ts:104,163, and Events.ts:331 takes the requirePublished: true default), so the very draft Live Preview exists to show reads webPath: null — the CMS has no view path to point the panel at. PreviewController.tsx:121 resolves it after fetching by id, and 'preview' stays in RESERVED_SLUGS while it does. Retiring the route needs a draft-visible path in SahajCloud plus a route-keyed controller here, as its own ticket after this one.
  • Filed from feat(fields): declarative live-preview targets, repointed by the open tab SahajCloud#773 at the reviewer's request.

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

Activity

  1. added
    proposal
    on Sep 13, 2026
  2. Ardnived commented on Sep 14, 2026

    @Ardnived
    Contributor

    @sydevs-bot Revise this. Instead of looking for a preview=1 query string, consider the present of the secret header as a request for a preview.

  3. added
    bot:working
    and removed
    proposal
    on Sep 14, 2026
  4. moved this from Proposed to Revising in SYDevelopers Technical Roadmapon Sep 14, 2026
  5. sydevs-bot commented on Sep 14, 2026

    @sydevs-bot
    ContributorAuthor

    🤖 no session running

    recent dispatches
    • 16:51Z answer · attempt 1 · session · done
    • 16:51Z answer · attempt 1 · session
    • 04:32Z revise · attempt 1 · session · done
    • 04:32Z revise · attempt 1 · session

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

  6. sydevs-bot commented on Sep 14, 2026

    @sydevs-bot
    ContributorAuthor

    Revised: the secret is the signal, no preview=1.

    • readPreviewParams gates on a secret param, on any pathname. /preview keeps collection and id.
    • Behaviour change: /preview with no secret stops being a session. Today it paints "Save this document to preview it." over the screen — the defect feat(fields): declarative live-preview targets, repointed by the open tab SahajCloud#773's critic round found.
    • The scrub now keeps the pathname, the other parameters and the hash, and deletes only secret.
    • The old "unsaved edit" criterion was wrong. That global has no autosave, so draft=true returns the last saved draft. In-form edits need a live-preview handler for a global — a second ticket. Comment @sydevs-bot split to carve it out.
    • New ## Downstream impact: feat(fields): declarative live-preview targets, repointed by the open tab SahajCloud#773 restores the secret to both livePreview.url builders after this lands.
    • Two open questions in the body.

    Effort stays Moderate. Type unchanged. @sydevs-bot implement starts work.

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


    Generated by Claude Code

  7. added and removed
    bot:working
    on Sep 14, 2026
  8. Ardnived commented on Sep 14, 2026

    @Ardnived
    Contributor

    @sydevs-bot Answer: can we further simplify the codebase by removing the /preview endpoints alongside this change?

  9. added
    bot:working
    and removed on Sep 14, 2026
  10. sydevs-bot commented on Sep 14, 2026

    @sydevs-bot
    ContributorAuthor

    No — not in this ticket. /preview does two jobs. This retires only one.

    Gone after this: the pathname as the draft gate. The secret unlocks drafts, on any route.

    Still needed: naming which document to preview. The CMS cannot name the doc's own view path: webPath is virtual and publish-gated (SahajCloud src/fields/publicUrlFields.ts:104,163) and Events takes that default, so the very draft Live Preview exists to show reads webPath: null. The widget resolves the path after fetching by id (PreviewController.tsx:121). That is why Events.ts:127 and Regions.ts:113 still build /preview?collection=…&id=…, and why preview stays in RESERVED_SLUGS.

    Dropping the route is a separate cross-repo change: expose a draft-visible path, key PreviewController off the route, retire collection/id. It also churns the panel — livePreview.url re-runs on form state, so a path from editable fields reloads on edits an id ignores.

    Recorded under Notes. Comment to have the follow-up filed.

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


    Generated by Claude Code

  11. added and removed
    bot:working
    on Sep 14, 2026
  12. Ardnived commented on Sep 16, 2026

    @Ardnived
    Contributor

    Delivered by #208 (merged 2026-09-16), alongside sydevs/SahajCloud#787 / #788 and sydevs/WeMeditateWeb#99.

    ⚠ The mechanism is not the one this ticket describes. The body says "the secret is the signal" and gates on a non-empty secret parameter. That approach was rejected during implementation, because widening the route gate without verifying turns any atlas URL into a denial of service: an active session inerts every <a>, snaps navigation back, pins all queries to staleTime: Infinity, and adds draft=true plus the credential to every request — including POST /events/:id/register. public/_redirects serves the SPA shell for every path, so sahajatlas.com/anything?live-preview=x would have qualified. Share that link and the recipient gets a page where nothing is clickable, with no error.

    What shipped instead: SahajCloud signs a short-lived Ed25519 token, the widget verifies it with a committed public key, and verification is the gate. A forged or expired token opens no session at all.

    This ticket said What shipped
    ?secret=<shared secret> ?live-preview=<Ed25519 JWS>, 45–90 min
    Presence gates the session Signature verification gates it
    hasValidPreviewSecret compares against SAHAJCLOUD_PREVIEW_SECRET that secret is retired; the CMS holds the only signing key

    Acceptance criteria

    • An active session opens on any view path, with a null collection and id.
    • No credential, no session — replacing the old active-with-nulls.
    • /preview?collection=event-submissions&id=… still behaves as before (kept for that one collection; see Event submission preview — render a user-submission proposal from postMessage #163).
    • After boot the address bar carries no credential, and the pathname, remaining parameters and the hash are unchanged. The old scrub dropped the whole query string and the hash.
    • Every SahajCloud request carries the credential header and draft=true while a session is active.
    • A saved-but-unpublished sy-atlas-translations draft renders on a view path — though incidentally; see below.
    • Widget.tsx never activates preview from the host page's URL. Now asserted rather than assumed: Widget.standalone.test.ts walks the real import graph, static and dynamic, and refuses the widget any path to boot.ts/token.ts.
    • No PreviewFallback — deleted. Payload never offers live preview for an unsaved document (handleLivePreview guards operation !== 'create'), so it was unreachable.
    • A document-less session renders no link guard and no pinned query defaults. Not done → fix(preview): a translations session should not inert the widget it is previewing #211.

    useLivePreviewLinkGuard() and usePinnedLivePreviewQueries() still run above the document check, so a translations session gets a widget where nothing is clickable — the exact failure this ticket named. scope is never read. Split out rather than left here, because everything else landed and the body no longer describes the system.

    Answering the open questions

    Beyond the ticket

    • The panel now points at each document's own page, so identity comes from the route rather than a ?collection=&id= pair — a second unauthenticated claim about the URL that no longer has to be trusted.
    • A live defect was fixed in passing: preview wrote to ['event', id] while the drawers read ['event', id, locale], so no live edit had ever rendered in the atlas.
    • Capture is synchronous and activation async, failing closed in between — no draft=true leaves the browser before the token is proven.
    • Verified in a real headless Chromium against the production build with tokens minted by the production key: forged and expired tokens each produced 0 draft requests and 0 credential headers out of 5 CMS calls; a valid token produced 5 of 5.
    • feat(preview): verify SahajCloud's live-preview token before opening a session #208 also replaced the hand-rolled transport with @payloadcms/live-preview-react, so the merge and relationship population are Payload's again.

    sydevs/SahajCloud#776 is unblocked by this and closes alongside it.

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

    Fields

    Priority

    Medium

    Hold Until

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions