"use client" import * as React from "react" import { isPlatformCreatorWorkspace, setRuntimeProductAuthoring, } from "@exxatdesignux/product-framework/product-authoring-gate" import { isProductAuthoringEnabled, isProductAuthoringRuntimeAllowed, } from "@exxatdesignux/product-framework/product-authoring" import { BuilderDevSync } from "./builder-dev-sync" const ProductAuthoringContext = React.createContext( undefined, ) export type ProductAuthoringProviderProps = { /** * When set, controls Settings product authoring for this subtree. * - `true` — **Builder** (tenant admin / workspace author): Add product in Settings. * - `false` — **End user**: products visible, no authoring UI. * - `undefined` — fall back to dev / env flags (legacy). */ enabled?: boolean children: React.ReactNode } export function ProductAuthoringProvider({ enabled, children, }: ProductAuthoringProviderProps) { const resolved = React.useMemo( () => resolveProductAuthoring(enabled), [enabled], ) React.useEffect(() => { setRuntimeProductAuthoring(enabled === undefined ? null : enabled) return () => setRuntimeProductAuthoring(null) }, [enabled]) return ( {children} ) } export function resolveProductAuthoring(explicit?: boolean): boolean { // Explicit prop from / // always wins — even in production. Customer apps wire this to real session auth // (e.g. session.canAuthorProducts) so their workspace builders see "Add product" // in their production deploy. The prod floor below only applies when no explicit // value is provided (i.e. the DS demo app falling back to env / dev defaults). if (explicit !== undefined) return explicit // No explicit override: apply the prod floor so the DS Cloudflare / V0 demo // can never accidentally expose product authoring at build time. if (!isProductAuthoringRuntimeAllowed()) return false return isProductAuthoringEnabled() } /** * Whether the current user may use Settings → Products authoring. * Prefer this hook in React; use {@link isProductAuthoringEnabled} only outside React. * * When a parent `ProductAuthoringProvider` (or `ProductProvider authoring=`) sets an * explicit value, that value is returned as-is — even in a production build — so that * customer apps can enable "Add product" for their workspace builders by wiring the * prop to real session auth (e.g. `session.canAuthorProducts`). * * The prod floor (`isProductAuthoringRuntimeAllowed`) only applies when no explicit * context value is present, i.e. when the DS demo app falls back to env / dev defaults. */ export function useProductAuthoringEnabled(): boolean { const ctx = React.useContext(ProductAuthoringContext) // Context value comes from resolveProductAuthoring which already honoured the // explicit prop — return it directly, regardless of whether this is a prod build. if (ctx !== undefined) return ctx // No provider in the tree: apply the prod floor so the DS demo can't re-enable // authoring via env vars at build time. if (!isProductAuthoringRuntimeAllowed()) return false return isProductAuthoringEnabled() } export type ProductAuthoringRole = "platform-creator" | "builder" | "end-user" /** Labels the three personas for Settings copy and onboarding. */ export function getProductAuthoringRole( authoringEnabled: boolean, ): ProductAuthoringRole { if (isPlatformCreatorWorkspace()) return "platform-creator" if (authoringEnabled) return "builder" return "end-user" }