-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dynamic-plugins): make mountpoints and layout declarative
Signed-off-by: Tomas Coufal <tcoufal@redhat.com>
- Loading branch information
Showing
9 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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
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
124 changes: 102 additions & 22 deletions
124
packages/app/src/components/catalog/EntityPage/index.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 |
---|---|---|
@@ -1,27 +1,107 @@ | ||
import { EntitySwitch, isKind } from '@backstage/plugin-catalog'; | ||
import React from 'react'; | ||
import getMountPointData from '../../../utils/dynamicUI/getMountPointData'; | ||
import { EntityAboutCard, EntityLayout, EntityLinksCard, EntitySwitch, isKind } from '@backstage/plugin-catalog'; | ||
import { ScalprumMountPointConfig } from '../../DynamicRoot/DynamicRootContext'; | ||
import { hasAnnotation, isType } from '../utils'; | ||
import { Entity } from '@backstage/catalog-model'; | ||
import { makeStyles } from 'tss-react/mui'; | ||
|
||
import { | ||
componentPage, | ||
defaultEntityPage, | ||
apiPage, | ||
groupPage, | ||
userPage, | ||
systemPage, | ||
domainPage, | ||
resourcePage, | ||
} from './Pages'; | ||
|
||
const createConditionsArray = (conditions: ({ | ||
[key: string]: string | string[]; | ||
} | Function)[]) => { | ||
return conditions.map(c => { | ||
if (typeof c === 'function') { | ||
return (entity: Entity) => Boolean(c(entity)) | ||
} | ||
if (c.isKind) { | ||
return isKind(c.isKind) | ||
} | ||
if (c.isType) { | ||
return isType(c.isType) | ||
} | ||
if (c.hasAnnotation) { | ||
return hasAnnotation(c.hasAnnotation as string) | ||
} | ||
return () => false | ||
}) | ||
} | ||
|
||
const entitySwitchCaseIf = (conditional: ScalprumMountPointConfig['if']) => (e: Entity) => { | ||
if (conditional?.allOf) { | ||
return createConditionsArray(conditional.allOf).every(f => f(e)) | ||
} | ||
if (conditional?.anyOf) { | ||
return createConditionsArray(conditional.anyOf).some(f => f(e)) | ||
} | ||
if (conditional?.oneOf) { | ||
return createConditionsArray(conditional.oneOf).filter(f => f(e)).length === 1 | ||
} | ||
return true | ||
} | ||
|
||
const useStyles = makeStyles()(theme => ({ | ||
grid: { | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(12, 1fr)', | ||
gridGap: theme.spacing(3), | ||
} | ||
})); | ||
const Grid = ({ children }: React.PropsWithChildren<{}>) => { | ||
const { classes } = useStyles(); | ||
|
||
return <div className={classes.grid}>{children}</div> | ||
} | ||
|
||
const tab = (mountPoint: string, children: React.ReactNode) => ( | ||
<> | ||
{getMountPointData<React.ComponentType>(`${mountPoint}/context`).reduce( | ||
(acc, { component: Component }) => ( | ||
<Component> | ||
{acc} | ||
</Component> | ||
), | ||
<Grid> | ||
{children} | ||
{getMountPointData<React.ComponentType>(`${mountPoint}/cards`).map( | ||
({ component: Component, config }, index) => ( | ||
<EntitySwitch key={index}> | ||
<EntitySwitch.Case if={entitySwitchCaseIf(config.if)}> | ||
<div style={ config.layout }> | ||
<Component /> | ||
</div> | ||
</EntitySwitch.Case> | ||
</EntitySwitch> | ||
) | ||
)} | ||
</Grid>) | ||
} | ||
</> | ||
) | ||
|
||
export const entityPage = ( | ||
<EntitySwitch> | ||
<EntitySwitch.Case if={isKind('component')} children={componentPage} /> | ||
<EntitySwitch.Case if={isKind('api')} children={apiPage} /> | ||
<EntitySwitch.Case if={isKind('group')} children={groupPage} /> | ||
<EntitySwitch.Case if={isKind('user')} children={userPage} /> | ||
<EntitySwitch.Case if={isKind('system')} children={systemPage} /> | ||
<EntitySwitch.Case if={isKind('domain')} children={domainPage} /> | ||
<EntitySwitch.Case if={isKind('resource')} children={resourcePage} /> | ||
|
||
<EntitySwitch.Case>{defaultEntityPage}</EntitySwitch.Case> | ||
</EntitySwitch> | ||
<EntityLayout> | ||
<EntityLayout.Route path="/" title="Overview"> | ||
{tab('entity.page.overview', <> | ||
<div style={{gridColumnStart: 'span 6'}}> | ||
<EntityLinksCard /> | ||
</div> | ||
<div style={{gridColumnStart: 'span 6', gridRowStart: 2}}> | ||
<EntityAboutCard /> | ||
</div> | ||
</>)} | ||
</EntityLayout.Route> | ||
</EntityLayout> | ||
// <EntitySwitch> | ||
// <EntitySwitch.Case if={isKind('component')} children={componentPage} /> | ||
// <EntitySwitch.Case if={isKind('api')} children={apiPage} /> | ||
// <EntitySwitch.Case if={isKind('group')} children={groupPage} /> | ||
// <EntitySwitch.Case if={isKind('user')} children={userPage} /> | ||
// <EntitySwitch.Case if={isKind('system')} children={systemPage} /> | ||
// <EntitySwitch.Case if={isKind('domain')} children={domainPage} /> | ||
// <EntitySwitch.Case if={isKind('resource')} children={resourcePage} /> | ||
|
||
// <EntitySwitch.Case>{defaultEntityPage}</EntitySwitch.Case> | ||
// </EntitySwitch> | ||
|
||
); |
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
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
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