-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/janus-idp/backstage-showcase …
…into add-scaffolder-from-processor
- Loading branch information
Showing
2 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/app/src/components/catalog/EntityPage/ContextMenuAwareEntityLayout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { ReactNode, useMemo, useState } from 'react'; | ||
import { EntityLayout } from '@backstage/plugin-catalog'; | ||
import getMountPointData from '../../../utils/dynamicUI/getMountPointData'; | ||
import { MenuIcon } from '../../Root/Root'; | ||
import { IconComponent } from '@backstage/core-plugin-api'; | ||
|
||
const makeIcon = | ||
(iconName: string): IconComponent => | ||
() => <MenuIcon icon={iconName} />; | ||
|
||
export const ContextMenuAwareEntityLayout = (props: { | ||
children?: ReactNode; | ||
}) => { | ||
const contextMenuElements = | ||
getMountPointData<React.ComponentType<React.PropsWithChildren<any>>>( | ||
`entity.context.menu`, | ||
); | ||
|
||
const [openStates, setOpenStates] = useState( | ||
new Array<boolean>(contextMenuElements.length).fill(false), | ||
); | ||
|
||
const changeValueAt = ( | ||
values: boolean[], | ||
index: number, | ||
newValue: boolean, | ||
): boolean[] => values.map((v, i) => (i === index ? newValue : v)); | ||
|
||
const extraMenuItems = useMemo( | ||
() => | ||
contextMenuElements.map((e, index) => { | ||
const Icon = makeIcon(e.config.props?.icon ?? 'icon'); | ||
return { | ||
title: e.config.props?.title ?? '<title>', | ||
Icon, | ||
onClick: () => { | ||
setOpenStates(changeValueAt(openStates, index, true)); | ||
}, | ||
}; | ||
}), | ||
[contextMenuElements, openStates], | ||
); | ||
|
||
return ( | ||
<> | ||
<EntityLayout | ||
UNSTABLE_extraContextMenuItems={extraMenuItems} | ||
UNSTABLE_contextMenuOptions={{ | ||
disableUnregister: 'visible', | ||
}} | ||
> | ||
{props.children} | ||
</EntityLayout> | ||
{contextMenuElements.map(({ Component }, index) => ( | ||
<Component | ||
key={`entity.context.menu-${Component.displayName}`} | ||
open={openStates[index]} | ||
onClose={() => setOpenStates(changeValueAt(openStates, index, false))} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters