avoid exporting MUI types, update click handler to support async and sync functions. ensure onClick handler passed to MenuItem is sync

Signed-off-by: Mark Dunphy <markd@spotify.com>
This commit is contained in:
Mark Dunphy
2025-04-07 17:35:20 -04:00
parent f2416c2ba6
commit 8048fe0cd2
@@ -20,15 +20,26 @@ import {
createExtensionDataRef,
ExtensionBoundary,
} from '@backstage/frontend-plugin-api';
import MenuItem, { MenuItemProps } from '@material-ui/core/MenuItem';
import MenuItem from '@material-ui/core/MenuItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
/** @alpha */
export type UseProps = () =>
| {
title: React.ReactNode;
href: string;
disabled?: boolean;
}
| {
title: React.ReactNode;
onClick: () => void | Promise<void>;
disabled?: boolean;
};
/** @alpha */
export type EntityContextMenuItemParams = {
useProps: () => Omit<MenuItemProps, 'onClick'> & {
onClick?: () => Promise<void>;
};
useProps: UseProps;
icon: React.JSX.Element;
};
@@ -56,24 +67,22 @@ export const EntityContextMenuItemBlueprint = createExtensionBlueprint({
*factory(params: EntityContextMenuItemParams, { node }) {
const loader = async (): Promise<ContextMenuItemComponent> => {
return ({ onClose }) => {
const {
children,
button,
title,
onClick: onClickProp,
...menuItemProps
} = params.useProps();
let onClick;
const { title, ...menuItemProps } = params.useProps();
let handleClick = undefined;
if (onClickProp !== undefined) {
onClick = async () => {
await onClickProp();
onClose();
if ('onClick' in menuItemProps) {
handleClick = () => {
const result = menuItemProps.onClick();
if (result instanceof Promise) {
result.then(onClose);
} else {
onClose();
}
};
}
return (
<MenuItem {...menuItemProps} onClick={onClick}>
<MenuItem {...menuItemProps} onClick={handleClick}>
<ListItemIcon>{params.icon}</ListItemIcon>
<ListItemText primary={title} />
</MenuItem>