Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions routes/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@wordpress/api-fetch": "file:../../packages/api-fetch",
"@wordpress/base-styles": "file:../../packages/base-styles",
"@wordpress/core-data": "file:../../packages/core-data",
"@wordpress/commands": "file:../../packages/commands",
"@wordpress/components": "file:../../packages/components",
"@wordpress/compose": "file:../../packages/compose",
"@wordpress/data": "file:../../packages/data",
Expand Down
27 changes: 15 additions & 12 deletions routes/dashboard/widget-dashboard/components/actions/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ export function Actions(): React.ReactNode {
return () => clearTimeout( exitTimeout );
}, [ editMode, isEditActionsMounted ] );

const { setInserterOpen } = useDashboardUIContext();

const [ isResetDialogOpen, setIsResetDialogOpen ] = useState( false );
const [ isLayoutSettingsOpen, setIsLayoutSettingsOpen ] = useState( false );
const {
setInserterOpen,
layoutSettingsOpen,
setLayoutSettingsOpen,
resetDialogOpen,
setResetDialogOpen,
} = useDashboardUIContext();

const handleEditMode = useCallback( () => {
onEditChange?.( ! editMode );
Expand All @@ -94,13 +97,13 @@ export function Actions(): React.ReactNode {
}, [ commit ] );

const openLayoutSettings = useCallback( () => {
setIsLayoutSettingsOpen( true );
}, [] );
setLayoutSettingsOpen( true );
}, [ setLayoutSettingsOpen ] );

const moreActionsItems: MoreActionsDropdownItem[] = [
{
label: __( 'Reset to default' ),
onClick: () => setIsResetDialogOpen( true ),
onClick: () => setResetDialogOpen( true ),
disabled: ! onLayoutReset,
},
];
Expand Down Expand Up @@ -178,12 +181,12 @@ export function Actions(): React.ReactNode {
<MoreActionsDropdown items={ moreActionsItems } />

<AlertDialog.Root
open={ isResetDialogOpen }
onOpenChange={ setIsResetDialogOpen }
open={ resetDialogOpen }
onOpenChange={ setResetDialogOpen }
onConfirm={ async () => {
await onLayoutReset?.();
onEditChange?.( false );
setIsResetDialogOpen( false );
setResetDialogOpen( false );
} }
>
<AlertDialog.Popup
Expand All @@ -198,8 +201,8 @@ export function Actions(): React.ReactNode {

{ canEditGridSettings && (
<LayoutSettings
open={ isLayoutSettingsOpen }
onOpenChange={ setIsLayoutSettingsOpen }
open={ layoutSettingsOpen }
onOpenChange={ setLayoutSettingsOpen }
/>
) }
</Stack>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* WordPress dependencies
*/
import { useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
useCommands,
privateApis as commandsPrivateApis,
} from '@wordpress/commands';
import {
columns,
grid,
layout as layoutIcon,
plus,
trash,
} from '@wordpress/icons';

/**
* Internal dependencies
*/
import { unlock } from '../../../lock-unlock';
import { useDashboardInternalContext } from '../../context/dashboard-context';
import { useDashboardUIContext } from '../../context/ui-context';
import { getGridModel } from '../../utils/grid-model-change';
import { usePendingWhenEditMode } from './use-pending-when-edit-mode';

const { useCommandContext } = unlock( commandsPrivateApis );

export const DASHBOARD_COMMAND_CONTEXT = 'dashboard';

type CommandCallback = ( options: { close: () => void } ) => void;

/**
* Registers dashboard-specific command palette commands and sets the
* active command context so they surface under Suggestions by default.
*/
export function DashboardCommands() {
const {
editMode,
onEditChange,
onLayoutReset,
gridSettings,
canEditGridSettings,
commitGridModelChange,
} = useDashboardInternalContext();

const { setInserterOpen, setResetDialogOpen } = useDashboardUIContext();

useCommandContext( DASHBOARD_COMMAND_CONTEXT );

const runWhenInEditMode = usePendingWhenEditMode( {
editMode,
onEditChange,
} );

const gridModel = getGridModel( gridSettings );
const isGridLayout = gridModel === 'grid';
const isMasonryLayout = gridModel === 'masonry';

const switchToMasonry = useCallback< CommandCallback >(
( { close } ) => {
close();
commitGridModelChange( 'masonry' );
},
[ commitGridModelChange ]
);

const switchToGrid = useCallback< CommandCallback >(
( { close } ) => {
close();
commitGridModelChange( 'grid' );
},
[ commitGridModelChange ]
);

const resetToDefault = useCallback< CommandCallback >(
( { close } ) => {
close();
setResetDialogOpen( true );
},
[ setResetDialogOpen ]
);

const addWidgets = useCallback< CommandCallback >(
( { close } ) => {
close();
runWhenInEditMode( () => {
setInserterOpen( true );
} );
},
[ runWhenInEditMode, setInserterOpen ]
);

const customize = useCallback< CommandCallback >(
( { close } ) => {
close();
onEditChange?.( true );
},
[ onEditChange ]
);
Comment on lines +94 to +100

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super mint one: every other callback in this file calls close() first and runs the action after; this one inverts it. Same outcome, but worth unifying so the pattern is consistent across the six commands.


const commands = useMemo(
() => [
{
name: 'core/dashboard/customize',
label: __( 'Customize dashboard' ),
icon: layoutIcon,
category: 'command',
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [ __( 'edit' ), __( 'widgets' ), __( 'layout' ) ],
disabled: ! onEditChange || editMode,
callback: customize,
},
{
name: 'core/dashboard/add-widgets',
label: __( 'Add dashboard widgets' ),
icon: plus,
category: 'command',
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [ __( 'widgets' ), __( 'inserter' ) ],
disabled: ! onEditChange,
callback: addWidgets,
},
{
name: 'core/dashboard/switch-to-masonry-layout',
label: __( 'Switch dashboard to masonry layout' ),
icon: columns,
category: 'command',
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [
__( 'layout' ),
__( 'layout model' ),
__( 'masonry' ),
],
disabled: ! canEditGridSettings || isMasonryLayout || editMode,
callback: switchToMasonry,
},
{
name: 'core/dashboard/switch-to-grid-layout',
label: __( 'Switch dashboard to grid layout' ),
icon: grid,
category: 'command',
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [
__( 'layout' ),
__( 'layout model' ),
__( 'grid' ),
__( 'standard grid' ),
],
disabled: ! canEditGridSettings || isGridLayout || editMode,
callback: switchToGrid,
},
{
name: 'core/dashboard/reset-to-default',
label: __( 'Reset dashboard widgets to default' ),
icon: trash,
category: 'command',
context: DASHBOARD_COMMAND_CONTEXT,
keywords: [ __( 'reset' ), __( 'default' ) ],
disabled: ! onLayoutReset,
callback: resetToDefault,
},
],
[
onEditChange,
editMode,
customize,
addWidgets,
canEditGridSettings,
isMasonryLayout,
isGridLayout,
switchToMasonry,
switchToGrid,
onLayoutReset,
resetToDefault,
]
);

useCommands( commands );

return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
DashboardCommands,
DASHBOARD_COMMAND_CONTEXT,
} from './dashboard-commands';
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { useCallback, useEffect, useRef } from '@wordpress/element';

type usePendingWhenEditModeProps = {
editMode: boolean;
onEditChange?: ( next: boolean ) => void;
};

type usePendingWhenEditModeResult = ( action: () => void ) => void;

/**
* Runs an action immediately when already in edit mode. Otherwise enables
* edit mode and runs the action after edit mode becomes active.
*
* @param {usePendingWhenEditModeProps} params - The parameters for the function.
* @return {usePendingWhenEditModeResult} The function result.
*/
export function usePendingWhenEditMode( {
editMode,
onEditChange,
}: usePendingWhenEditModeProps ): usePendingWhenEditModeResult {
const pendingRef = useRef< ( () => void ) | null >( null );
const wasEditingRef = useRef( editMode );

useEffect( () => {
if ( ! wasEditingRef.current && editMode && pendingRef.current ) {
const action = pendingRef.current;
pendingRef.current = null;
action();
}
wasEditingRef.current = editMode;
}, [ editMode ] );

return useCallback(
( action: () => void ) => {
if ( editMode ) {
action();
return;
}

pendingRef.current = action;
onEditChange?.( true );
},
[ editMode, onEditChange ]
);
}
38 changes: 38 additions & 0 deletions routes/dashboard/widget-dashboard/context/dashboard-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import {
/**
* Internal dependencies
*/
import { computeGridModelChange } from '../utils/grid-model-change';
import type {
ResolveWidgetModule,
WidgetGridModel,
WidgetGridSettings,
DashboardWidget,
WidgetType,
Expand Down Expand Up @@ -123,6 +125,13 @@ interface InternalDashboardContextValue {
*/
commit: ( options?: { exitEditMode?: boolean } ) => void;

/**
* Switches the layout model, updates staging, and publishes
* immediately — equivalent to changing the model in layout
* settings and clicking Save.
*/
commitGridModelChange: ( targetModel: WidgetGridModel ) => void;

/**
* Reverts both staging slices. By default also exits edit mode; pass
* `{ exitEditMode: false }` when dismissing the layout settings drawer.
Expand Down Expand Up @@ -309,6 +318,33 @@ export function WidgetDashboardProvider( {
[ committedLayout, committedGridSettings, onEditChange ]
);

const commitGridModelChange = useCallback(
( targetModel: WidgetGridModel ) => {
const next = computeGridModelChange( {
layout: stagingLayout,
gridSettings: stagingGridSettings,
targetModel,
} );

if ( ! next ) {
return;
}

setStagingLayout( next.layout );
setStagingGridSettings( next.gridSettings );
onLayoutChange( canonicalize( next.layout ) );
onGridSettingsChange?.( next.gridSettings );
onEditChange?.( false );
},
Comment on lines +321 to +338
[
stagingLayout,
stagingGridSettings,
onLayoutChange,
onGridSettingsChange,
onEditChange,
]
);

const resetGridSettings = useCallback( () => {
setStagingGridSettings( DEFAULT_GRID );
}, [] );
Expand Down Expand Up @@ -337,6 +373,7 @@ export function WidgetDashboardProvider( {
canEditGridSettings,
resetGridSettings,
commit,
commitGridModelChange,
cancel,
hasUncommittedChanges,
editMode,
Expand All @@ -351,6 +388,7 @@ export function WidgetDashboardProvider( {
canEditGridSettings,
resetGridSettings,
commit,
commitGridModelChange,
cancel,
hasUncommittedChanges,
editMode,
Expand Down
Loading
Loading