{"version":3,"file":"dashboard-context.cjs","names":["normalizeGridSettings","DEFAULT_ROW_HEIGHT","resolveDashboardMinColumnWidth","resolveDashboardColumns","WIDGET_DASHBOARD_COLUMN_STEPS","createContext","useContext","DEFAULT_GRID","useState","createRuntimeActionsMap","createPendingActionsMap","useDashboardPolicy","useMemo","useCallback","enforceLayoutPolicy","fastDeepEqual","canonicalizeLayout","useEvent","debounce"],"sources":["../../src/context/dashboard-context.tsx"],"sourcesContent":["import fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport type { ReactNode } from 'react';\nimport { debounce, useEvent } from '@wordpress/compose';\nimport {\n\tcreateContext,\n\tuseCallback,\n\tuseContext,\n\tuseEffect,\n\tuseMemo,\n\tuseState,\n} from '@wordpress/element';\nimport type {\n\tResolveWidgetModule,\n\tWidgetType,\n} from '@wordpress/widget-primitives';\nimport { canonicalizeLayout } from '../utils/canonicalize-layout';\nimport { DEFAULT_GRID } from '../utils/default-grid';\nimport { enforceLayoutPolicy } from '../utils/enforce-layout-policy';\nimport { normalizeGridSettings } from '../utils/normalize-grid-settings';\nimport {\n\tresolveDashboardColumns,\n\tresolveDashboardMinColumnWidth,\n} from '../utils/resolve-dashboard-column-count/resolve-dashboard-column-count';\nimport { DEFAULT_ROW_HEIGHT } from '../utils/row-height-presets';\nimport { createPendingActionsMap } from '../utils/pending-actions-map';\nimport type { PendingActionsMap } from '../utils/pending-actions-map';\nimport { createRuntimeActionsMap } from '../utils/runtime-actions-map';\nimport type { RuntimeActionsMap } from '../utils/runtime-actions-map';\nimport { useDashboardPolicy } from '../components/dashboard-policy';\nimport { WIDGET_DASHBOARD_COLUMN_STEPS } from '../types';\nimport type {\n\tCanPerformDashboardOperation,\n\tWidgetGridSettings,\n\tDashboardWidget,\n} from '../types';\n\n/*\n * Validates the host's column policy and hands it to the surface: the\n * `@wordpress/grid` surface measures its own width and resolves the count.\n * The default steps only apply when the host set no count and no floor.\n */\nfunction resolveGridSettings(\n\tsettings: WidgetGridSettings\n): WidgetGridSettings {\n\tconst normalized = normalizeGridSettings( settings, DEFAULT_ROW_HEIGHT );\n\tconst minColumnWidth = resolveDashboardMinColumnWidth(\n\t\tnormalized.minColumnWidth\n\t);\n\tconst columns = resolveDashboardColumns(\n\t\tnormalized.columns,\n\t\tminColumnWidth\n\t);\n\n\treturn {\n\t\t...normalized,\n\t\tcolumns:\n\t\t\tcolumns === undefined && minColumnWidth === undefined\n\t\t\t\t? WIDGET_DASHBOARD_COLUMN_STEPS\n\t\t\t\t: columns,\n\t\tminColumnWidth,\n\t};\n}\n\nconst DEFAULT_RESOLVE_WIDGET_MODULE: ResolveWidgetModule = ( moduleId ) =>\n\timport( /* webpackIgnore: true */ /* @vite-ignore */ moduleId );\n\n/**\n * Inline widget-instance edits stage live, then publish once the user pauses.\n * A single global timer, so editing several widgets settles into one save.\n */\nconst AUTO_SAVE_DELAY_MS = 5000;\n\n/**\n * Rich state distributed to every compound component inside `WidgetDashboard`.\n * Internal — compounds reach the full state via `useDashboardInternalContext()`.\n *\n * `layout`/`onLayoutChange` here operate on the staging layer, not the\n * committed props. Mutations from compound children stay in staging until\n * `commit` publishes them on the consumer.\n */\ninterface InternalDashboardContextValue {\n\twidgetTypes: WidgetType[];\n\tisResolvingWidgetTypes: boolean;\n\tlayout: DashboardWidget[];\n\tonLayoutChange: ( layout: DashboardWidget[] ) => void;\n\tonLayoutReset?: () => void;\n\tgridSettings: WidgetGridSettings;\n\n\t/**\n\t * Publishes the staged layout when it differs from the committed\n\t * prop. By default also exits edit mode; pass `{ exitEditMode: false }`\n\t * for inline auto-saves that keep the current mode.\n\t */\n\tcommit: ( options?: CommitOptions ) => void;\n\n\t/**\n\t * Reverts the staged layout and exits edit mode.\n\t */\n\tcancel: () => void;\n\n\t/**\n\t * Debounced auto-save for inline widget-instance edits. Controls call it\n\t * after staging a change; a single global timer publishes once the edits\n\t * settle. The settings drawer does not use it (it commits on Save).\n\t */\n\tscheduleAutoSave: () => void;\n\n\t/**\n\t * Publishes any pending auto-save immediately. Called when leaving the\n\t * inline surface (opening the drawer, entering customize) so staged inline\n\t * edits do not commingle with the drawer's explicit-save flow.\n\t */\n\tflushAutoSave: () => void;\n\n\thasUncommittedChanges: boolean;\n\teditMode: boolean;\n\tonEditChange?: ( next: boolean ) => void;\n\tresolveWidgetModule: ResolveWidgetModule;\n\n\t/**\n\t * Resolved policy. Every compound asks here, never the policy provider,\n\t * so further policy sources compose at this single point.\n\t */\n\tcanPerform: CanPerformDashboardOperation;\n\n\t/**\n\t * The actions each mounted instance declares at runtime.\n\t */\n\truntimeActions: RuntimeActionsMap;\n\n\t/**\n\t * The callback actions of each instance still settling.\n\t */\n\tpendingActions: PendingActionsMap;\n}\n\nconst ALLOW_EVERY_OPERATION: CanPerformDashboardOperation = () => true;\n\ninterface CommitOptions {\n\texitEditMode?: boolean;\n}\n\nconst Context = createContext< InternalDashboardContextValue | null >( null );\n\n/**\n * Compound-internal hook — exposes the full provider state.\n * Not part of the public API; lives in the same module\n * so compound components can reach the state directly.\n */\nexport function useDashboardInternalContext(): InternalDashboardContextValue {\n\tconst ctx = useContext( Context );\n\tif ( ! ctx ) {\n\t\tthrow new Error(\n\t\t\t'Dashboard compound used outside a WidgetDashboard subtree.'\n\t\t);\n\t}\n\treturn ctx;\n}\n\ninterface ProviderProps {\n\twidgetTypes: WidgetType[];\n\tisResolvingWidgetTypes?: boolean;\n\tlayout: DashboardWidget[];\n\n\t/**\n\t * Fired on commit when the staged layout differs from `layout`.\n\t */\n\tonLayoutChange: ( layout: DashboardWidget[] ) => void;\n\n\t/**\n\t * Optional reset action surfaced by the bundled `Actions`.\n\t */\n\tonLayoutReset?: () => void;\n\n\teditMode?: boolean;\n\tonEditChange?: ( next: boolean ) => void;\n\n\t/**\n\t * Overrides the default `import()` resolution of\n\t * `WidgetType.renderModule`.\n\t */\n\tresolveWidgetModule?: ResolveWidgetModule;\n\n\tgridSettings?: WidgetGridSettings;\n\n\tchildren: ReactNode;\n}\n\n/**\n * Provider for the dashboard's staging layer. Owns the staging copy of\n * `layout`; `commit` publishes it when it differs from the committed\n * prop, `cancel` reverts it.\n *\n * Staging re-syncs from the committed prop on prop change. In-flight\n * edits are dropped silently when an external update (cross-tab commit,\n * reset, websocket push) lands. Consumers that cannot tolerate this\n * loss should mediate the prop updates before forwarding them here.\n *\n * @param {ProviderProps} props Component props.\n */\nexport function WidgetDashboardProvider( {\n\twidgetTypes,\n\tisResolvingWidgetTypes = false,\n\tlayout: committedLayout,\n\tonLayoutChange,\n\tonLayoutReset,\n\teditMode = false,\n\tonEditChange,\n\tresolveWidgetModule = DEFAULT_RESOLVE_WIDGET_MODULE,\n\tgridSettings: committedGridSettings = DEFAULT_GRID,\n\tchildren,\n}: ProviderProps ): React.ReactNode {\n\tconst [ stagingLayout, setStagingLayout ] =\n\t\tuseState< DashboardWidget[] >( committedLayout );\n\tconst [ runtimeActions ] = useState( createRuntimeActionsMap );\n\tconst [ pendingActions ] = useState( createPendingActionsMap );\n\n\tconst policy = useDashboardPolicy();\n\n\t// Normalized once, so every surface reads a boolean: an `undefined`\n\t// answer denies everywhere instead of hiding the controls while the grid\n\t// still lets the tile drag and resize.\n\tconst canPerform = useMemo< CanPerformDashboardOperation >(\n\t\t() =>\n\t\t\tpolicy\n\t\t\t\t? ( request ) => !! policy( request )\n\t\t\t\t: ALLOW_EVERY_OPERATION,\n\t\t[ policy ]\n\t);\n\n\t// Every mutation stages through here, diffed against the current\n\t// staging so every change the policy denies is re-asserted before it\n\t// lands: what the interface hides, the staging layer rejects. Without\n\t// a policy there is nothing to enforce, and staging stays byte-equal\n\t// to what the trigger wrote.\n\tconst stageLayout = useCallback(\n\t\t( next: DashboardWidget[] ) => {\n\t\t\tsetStagingLayout( ( previous ) =>\n\t\t\t\tcanPerform === ALLOW_EVERY_OPERATION\n\t\t\t\t\t? next\n\t\t\t\t\t: enforceLayoutPolicy( {\n\t\t\t\t\t\t\tprevious,\n\t\t\t\t\t\t\tnext,\n\t\t\t\t\t\t\tcanPerform,\n\t\t\t\t\t\t\twidgetTypes,\n\t\t\t\t\t\t} )\n\t\t\t);\n\t\t},\n\t\t[ canPerform, widgetTypes ]\n\t);\n\n\t// External change in `layout` (consumer-side reset, cross-tab sync,\n\t// websocket push, etc.) drops any in-flight staging edits without\n\t// surfacing a warning. See the provider JSDoc for the trade-off.\n\tuseEffect( () => {\n\t\tsetStagingLayout( committedLayout );\n\t}, [ committedLayout ] );\n\n\tconst gridSettings = useMemo(\n\t\t() => resolveGridSettings( committedGridSettings ),\n\t\t[ committedGridSettings ]\n\t);\n\n\tconst hasLayoutChanges = useMemo(\n\t\t() =>\n\t\t\t! fastDeepEqual(\n\t\t\t\tcanonicalizeLayout( committedLayout ),\n\t\t\t\tcanonicalizeLayout( stagingLayout )\n\t\t\t),\n\t\t[ committedLayout, stagingLayout ]\n\t);\n\n\tconst hasUncommittedChanges = hasLayoutChanges;\n\n\tconst commit = useCallback(\n\t\t( options?: CommitOptions ) => {\n\t\t\tif ( hasLayoutChanges ) {\n\t\t\t\tonLayoutChange( canonicalizeLayout( stagingLayout ) );\n\t\t\t}\n\n\t\t\tif ( options?.exitEditMode !== false ) {\n\t\t\t\tonEditChange?.( false );\n\t\t\t}\n\t\t},\n\t\t[ hasLayoutChanges, onLayoutChange, stagingLayout, onEditChange ]\n\t);\n\n\t// Auto-save for inline edits.\n\t// A single debounced timer publishes through `useEvent`, so it always\n\t// reads the latest `commit` (and so the current staging) without\n\t// resetting on staging re-renders.\n\tconst publishAutoSave = useEvent( () => commit( { exitEditMode: false } ) );\n\n\tconst scheduleAutoSave = useMemo(\n\t\t() => debounce( publishAutoSave, AUTO_SAVE_DELAY_MS ),\n\t\t[ publishAutoSave ]\n\t);\n\n\tconst flushAutoSave = useCallback(\n\t\t() => scheduleAutoSave.flush(),\n\t\t[ scheduleAutoSave ]\n\t);\n\n\t// Entering customize flushes any pending inline save first, so it does not\n\t// commingle with the layout edit flow.\n\tuseEffect( () => {\n\t\tif ( ! editMode ) {\n\t\t\treturn;\n\t\t}\n\n\t\tscheduleAutoSave.flush();\n\t}, [ editMode, scheduleAutoSave ] );\n\n\t// Flush, not cancel, on unmount: an edit still inside the debounce window\n\t// must persist when the user navigates away from the dashboard.\n\tuseEffect( () => () => scheduleAutoSave.flush(), [ scheduleAutoSave ] );\n\n\tconst cancel = useCallback( () => {\n\t\tsetStagingLayout( committedLayout );\n\t\tonEditChange?.( false );\n\t}, [ committedLayout, onEditChange ] );\n\n\tuseEffect( () => {\n\t\tif (\n\t\t\tstagingLayout.length === 0 &&\n\t\t\tcanPerform( { operation: 'customize' } )\n\t\t) {\n\t\t\tonEditChange?.( true );\n\t\t}\n\n\t\t// Only react to the layout count flipping to zero; firing on every\n\t\t// onEditChange identity change would also reopen edit mode after the\n\t\t// user explicitly closed it on a non-empty layout.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, [ stagingLayout.length === 0 ] );\n\n\tconst value = useMemo< InternalDashboardContextValue >(\n\t\t() => ( {\n\t\t\twidgetTypes,\n\t\t\tisResolvingWidgetTypes,\n\t\t\tlayout: stagingLayout,\n\t\t\tonLayoutChange: stageLayout,\n\t\t\tonLayoutReset,\n\t\t\tgridSettings,\n\t\t\tcommit,\n\t\t\tcancel,\n\t\t\tscheduleAutoSave,\n\t\t\tflushAutoSave,\n\t\t\thasUncommittedChanges,\n\t\t\teditMode,\n\t\t\tonEditChange,\n\t\t\tresolveWidgetModule,\n\t\t\tcanPerform,\n\t\t\truntimeActions,\n\t\t\tpendingActions,\n\t\t} ),\n\t\t[\n\t\t\twidgetTypes,\n\t\t\tisResolvingWidgetTypes,\n\t\t\tstagingLayout,\n\t\t\tstageLayout,\n\t\t\tonLayoutReset,\n\t\t\tgridSettings,\n\t\t\tcommit,\n\t\t\tcancel,\n\t\t\tscheduleAutoSave,\n\t\t\tflushAutoSave,\n\t\t\thasUncommittedChanges,\n\t\t\teditMode,\n\t\t\tonEditChange,\n\t\t\tresolveWidgetModule,\n\t\t\tcanPerform,\n\t\t\truntimeActions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