Skip to content

Commit

Permalink
consolidate pin action into one conditional label
Browse files Browse the repository at this point in the history
  • Loading branch information
vcanales committed Sep 25, 2024
1 parent 57c995e commit c442ce0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
48 changes: 30 additions & 18 deletions packages/dataviews/src/components/dataviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ import DataViewsFooter from '../dataviews-footer';
import DataViewsSearch from '../dataviews-search';
import DataViewsViewConfig from '../dataviews-view-config';
import { normalizeFields } from '../../normalize-fields';
import type { Action, Field, View, SupportedLayouts } from '../../types';
import type {
Action,
Field,
View,
SupportedLayouts,
ActionContext,
} from '../../types';
import type { SelectionOrUpdater } from '../../private-types';
import { __ } from '@wordpress/i18n';
import { pin } from '@wordpress/icons';

type ItemWithId = { id: string };

Expand Down Expand Up @@ -140,35 +147,40 @@ export default function DataViews< Item >( {
( callback: ( items: Item[], context: any ) => void ) => {
return [
{
id: 'pin',
label: __( 'Pin' ),
callback: ( items: Item[], context: any ) => {
items.forEach( ( item ) =>
onPinItem( getItemId( item ) )
);
callback( items, context );
id: 'togglePin',
label: ( items: Item[] ) => {
const itemId = getItemId( items[ 0 ] );
return combinedPinnedItems.includes( itemId )
? __( 'Unpin' )
: __( 'Pin' );
},
},
{
id: 'unpin',
label: __( 'Unpin' ),
callback: ( items: Item[], context: any ) => {
items.forEach( ( item ) =>
onUnpinItem( getItemId( item ) )
);
isPrimary: true,
icon: pin,
callback: (
items: Item[],
context: ActionContext< Item >
) => {
items.forEach( ( item ) => {
const itemId = getItemId( item );
if ( combinedPinnedItems.includes( itemId ) ) {
onUnpinItem( itemId );
} else {
onPinItem( itemId );
}
} );
callback( items, context );
},
},
];
},
[ getItemId, onPinItem, onUnpinItem ]
[ getItemId, onPinItem, onUnpinItem, combinedPinnedItems ]
);

const actionsWithDefaultActions = useMemo( () => {
const defaultActions = createDefaultActions(
( items, context ) => context.onActionPerformed?.( items )
);
return [ ...defaultActions, ...actions ];
return [ ...defaultActions, ...actions ]; // Combine default and custom actions
}, [ actions, createDefaultActions ] );

return (
Expand Down
13 changes: 6 additions & 7 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,16 @@ export interface ActionModal< Item > extends ActionBase< Item > {
modalHeader?: string;
}

export interface ActionContext< Item > {
registry: any;
onActionPerformed?: ( items: Item[] ) => void;
}

export interface ActionButton< Item > extends ActionBase< Item > {
/**
* The callback to execute when the action is triggered.
*/
callback: (
items: Item[],
context: {
registry: any;
onActionPerformed?: ( items: Item[] ) => void;
}
) => void;
callback: ( items: Item[], context: ActionContext< Item > ) => void;
}

export type Action< Item > = ActionModal< Item > | ActionButton< Item >;
Expand Down

0 comments on commit c442ce0

Please sign in to comment.