import type { Meta, StoryObj } from '@storybook/react-vite';
import type { ComponentType } from 'react';
import {
createContext,
useContext,
useMemo,
useState,
} from '@wordpress/element';
import { lock } from '@wordpress/icons';
import {
// eslint-disable-next-line @wordpress/use-recommended-components
Button,
} from '@wordpress/ui';
import type {
ResolveWidgetModule,
WidgetRenderProps,
WidgetType,
} from '@wordpress/widget-primitives';
import { useDashboardInternalContext } from '../context/dashboard-context';
import { useWidgetContext } from '../context/widget-context';
import { WidgetDashboard } from '../widget-dashboard';
import type {
CanPerformDashboardOperation,
DashboardInstanceOperation,
DashboardWidget,
} from '../types';
/*
* Debug fixture: the rogue triggers write to staging through the internal
* context, which is what only a composed trigger can do. Not a consumer
* example. Every tile is a lock card showing its live canonical position
* and one chip per instance operation; the story controls edit each
* card's denied operations. Customize mode shows the same locks on the
* chrome.
*/
const INSTANCE_OPERATIONS: DashboardInstanceOperation[] = [
'move',
'resize',
'remove',
'edit',
];
type TileLocks = Record< string, DashboardInstanceOperation[] | undefined >;
/* Demo plumbing so the cards re-render when the controls change. */
const TileLocksContext = createContext< TileLocks >( {} );
/* Stable instance name from the uuid: `card-alpha` reads as `Alpha`. */
const cardName = ( uuid: string ) => {
const raw = uuid.replace( /^card-/, '' ).replace( /-/g, ' ' );
return raw.charAt( 0 ).toUpperCase() + raw.slice( 1 );
};
/*
* Reads the demo's lock map by its own uuid purely to visualize it; real
* widgets never see the policy.
*/
function LockCardWidget( {
attributes,
}: WidgetRenderProps< { label?: string } > ) {
const { label = 'Card' } = attributes ?? {};
const widget = useWidgetContext();
const locks = useContext( TileLocksContext )[ widget?.uuid ?? '' ] ?? [];
return (
);
}
const meta: Meta< typeof WidgetDashboard > = {
id: 'widget-dashboard-playground-debug',
title: 'Widgets/Dashboard/Playground/Debug',
component: WidgetDashboard,
tags: [ 'status-experimental' ],
parameters: {
// FIXME: Sortable widget chrome nests interactive controls (nested-interactive).
// See: https://github.com/WordPress/gutenberg/issues/81596
a11y: { test: 'todo' },
},
};
export default meta;
const lockControl = {
control: 'check' as const,
options: INSTANCE_OPERATIONS,
};
export const StagingEnforcement: StoryObj< StagingEnforcementArgs > = {
render: ( args ) => ,
args: {
alpha: [],
bravo: [ 'move' ],
charlie: [ 'resize' ],
delta: [ 'remove', 'edit' ],
},
argTypes: {
alpha: {
...lockControl,
description: 'Operations denied on the Alpha card.',
},
bravo: {
...lockControl,
description: 'Operations denied on the Bravo card.',
},
charlie: {
...lockControl,
description: 'Operations denied on the Charlie card.',
},
delta: {
...lockControl,
description: 'Operations denied on the Delta card.',
},
},
parameters: {
docs: {
description: {
story: `
Debug fixture. Every tile is a **Lock Card**: it shows its live position in the canonical order and one chip per instance operation, ✓ when the policy allows it and ✕ when it denies it. The controls edit each card's denied operations; by default Bravo cannot move, Charlie cannot resize, Delta cannot be removed or edited, and Alpha is free. Enter Customize to see the same locks on the chrome: a move-denied card refuses to drag and holds its position while the others reorder around it, a resize-denied card offers no resize handle or width menu, a remove-denied card has no Remove control, and the inserter rejects Goal Progress.
The buttons bypass the chrome and write to the engine's staging directly, which is what the staging enforcement exists for: with the default locks, reversing holds Bravo in place, the width sweep skips Charlie, the rename skips Delta, and removing Delta re-asserts it, while the rejected insertion never lands and the allowed one does. Relax a lock from the controls and the same trigger goes through.
`,
},
},
},
};