add featured docs

Signed-off-by: nikolar <reyna.nikolayev@autodesk.com>
This commit is contained in:
nikolar
2023-10-03 17:26:59 -07:00
parent 12683b2a85
commit 302316d231
9 changed files with 391 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': patch
---
Added a new Featured Docs component to plugin-home, which can display any entity given a filter
+2
View File
@@ -47,6 +47,7 @@
"clean": "backstage-cli package clean"
},
"dependencies": {
"@backstage/catalog-client": "workspace:^",
"@backstage/catalog-model": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/core-app-api": "workspace:^",
@@ -60,6 +61,7 @@
"@material-ui/core": "^4.12.2",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "4.0.0-alpha.61",
"@material-ui/styles": "^4.11.5",
"@rjsf/core": "5.13.0",
"@rjsf/material-ui": "5.13.0",
"@rjsf/utils": "5.13.0",
@@ -0,0 +1,118 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FeaturedDocs } from '../../plugin';
import React, { ComponentType, PropsWithChildren } from 'react';
import { wrapInTestApp, TestApiProvider } from '@backstage/test-utils';
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
import { Grid, makeStyles, Theme } from '@material-ui/core';
import WarningIcon from '@material-ui/icons/Warning';
const docsEntities = [
{
apiVersion: '1',
kind: 'Location',
metadata: {
name: 'getting-started-with-backstage',
title: 'Getting Started Docs',
description:
'An awesome doc you want to feature to help out your customers. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pretium magna ut molestie lacinia. Nullam eget bibendum est, vitae finibus neque.',
},
spec: {
type: 'documentation',
},
},
];
const mockCatalogApi = {
getEntities: async () => ({ items: docsEntities }),
};
const useStyles = makeStyles<Theme>(() => ({
cardTitleIcon: {
verticalAlign: 'bottom',
marginLeft: '-4px',
},
docDescription: {
marginBottom: '16px',
marginTop: '12px',
},
docSubLink: {
fontSize: 10,
fontWeight: 500,
lineHeight: 2,
},
docsTitleLink: {
fontSize: 18,
fontWeight: 600,
lineHeight: 3,
},
}));
export default {
title: 'Plugins/Home/Components/FeaturedDocs',
decorators: [
(Story: ComponentType<PropsWithChildren<{}>>) =>
wrapInTestApp(
<TestApiProvider apis={[[catalogApiRef, mockCatalogApi]]}>
<Story />
</TestApiProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
),
],
};
export const Default = () => {
return (
<Grid item xs={12} md={6}>
<FeaturedDocs
filter={{
'spec.type': 'documentation',
'metadata.name': 'getting-started-with-backstage',
}}
title="Featured Doc"
/>
</Grid>
);
};
export const ExampleCustomized = () => {
const styles = useStyles();
const cardTitle = (
<>
<WarningIcon fontSize="large" className={styles.cardTitleIcon} />
&nbsp; Important
</>
);
return (
<Grid item xs={12} md={6}>
<FeaturedDocs
filter={{
'spec.type': 'documentation',
'metadata.name': 'getting-started-with-backstage',
}}
title={cardTitle}
customStyles={styles}
subLinkText="More Details"
color="secondary"
/>
</Grid>
);
};
@@ -0,0 +1,77 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FeaturedDocs } from './FeaturedDocs';
import React from 'react';
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
const docsEntities = [
{
apiVersion: '1',
kind: 'Location',
metadata: {
name: 'getting-started-with-idp',
title: 'Getting Started Docs',
},
spec: {
type: 'documentation',
},
},
];
describe('<FeaturedDocs />', () => {
const mockCatalogApi = {
getEntities: jest
.fn()
.mockImplementation(async () => ({ items: docsEntities })),
};
let Wrapper: React.ComponentType;
beforeAll(() => {
Wrapper = ({ children }: { children?: React.ReactNode }) => (
<TestApiProvider apis={[[catalogApiRef, mockCatalogApi]]}>
{children}
</TestApiProvider>
);
});
it('should show expected featured doc and title', async () => {
const { getByTestId, getByText } = await renderInTestApp(
<Wrapper>
<FeaturedDocs
data-testid="docs-card-content"
filter={{
'spec.type': 'documentation',
'metadata.name': 'getting-started-with-idp',
}}
emptyState={undefined}
title="Featured Doc"
/>
</Wrapper>,
{
mountedRoutes: {
'/home': entityRouteRef,
},
},
);
const docsCardContent = getByTestId('docs-card-content');
const docsEntity = getByText('Getting Started Docs');
const docsTitle = getByText('Featured Doc');
expect(docsCardContent).toContainElement(docsEntity);
expect(docsTitle).toBeInTheDocument();
});
});
@@ -0,0 +1,154 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import useAsync from 'react-use/lib/useAsync';
import {
LinkButton,
EmptyState,
Link,
CodeSnippet,
InfoCard,
Progress,
WarningPanel,
} from '@backstage/core-components';
import { catalogApiRef, CatalogApi } from '@backstage/plugin-catalog-react';
import { useApi } from '@backstage/core-plugin-api';
import { EntityFilterQuery } from '@backstage/catalog-client';
import { ClassNameMap } from '@material-ui/styles';
import { makeStyles, Theme, Typography } from '@material-ui/core';
type DocsCardProps = {
filter: EntityFilterQuery;
color?: 'inherit' | 'primary' | 'secondary' | undefined;
customStyles?: ClassNameMap<string> | undefined;
emptyState?: React.ReactNode | undefined;
subLinkText?: string | undefined;
title?: React.ReactNode | string | undefined;
};
const useStyles = makeStyles<Theme>(() => ({
docDescription: {
fontSize: 16,
fontWeight: 400,
marginBottom: '16px',
marginTop: '12px',
},
docSubLink: {
fontSize: 12,
fontWeight: 700,
lineHeight: 2,
},
docsTitleLink: {
fontSize: 16,
fontWeight: 600,
lineHeight: 2,
},
}));
/**
* A component to display specific Featured Docs.
* @param {EntityFilterQuery} filter - The entity filter used to display only the intended item/s
* @param {'inherit' | 'primary' | 'secondary' | undefined} [color] - An optional color which can be customized through themes
* @param {ClassNameMap<string> | undefined} [customStyles] - An optional ClassNameMap created with makeStyles
* @param {React.ReactNode | undefined} [emptyState] - An optional ReactNode for empty states
* @param {string | undefined} [subLinkText] - An optional string to customize sublink text
* @param {React.ReactNode | string | undefined} [title] - An optional string or ReactNode to customize the card title
*
* @public
*/
export const FeaturedDocs = (props: DocsCardProps) => {
const { color, customStyles, emptyState, filter, subLinkText, title } = props;
const linkText = subLinkText || 'LEARN MORE';
const defaultStyles = useStyles();
const styles = customStyles || defaultStyles;
const catalogApi: CatalogApi = useApi(catalogApiRef);
const {
value: entities,
loading,
error,
} = useAsync(async () => {
const response = await catalogApi.getEntities({
filter: filter,
});
return response.items;
});
if (loading) {
return <Progress />;
}
if (error) {
return (
<WarningPanel
severity="error"
title="Could not load available documentation."
>
<CodeSnippet language="text" text={error.toString()} />
</WarningPanel>
);
}
return (
<InfoCard variant="gridItem" title={title || 'Featured Docs'}>
{entities?.length
? entities.map(d => (
<div key={d.metadata.name} data-testid="docs-card-content">
<Link
className={styles.docsTitleLink}
data-testid="docs-card-title"
color={color || 'primary'}
to={`/docs/${d.metadata.namespace || 'default'}/${d.kind}/${
d.metadata.name
}/`}
>
{d.metadata.title}
</Link>
<Typography className={styles.docDescription}>
{d.metadata.description}
</Typography>
<Link
className={styles.docSubLink}
data-testid="docs-card-sub-link"
color={color || 'primary'}
to={`/docs/${d.metadata.namespace || 'default'}/${d.kind}/${
d.metadata.name
}/`}
>
{linkText}
</Link>
</div>
))
: emptyState || (
<EmptyState
missing="data"
title="No documents to show"
description="Create your own document. Check out our Getting Started Information"
action={
<LinkButton
color={color || 'primary'}
to="https://backstage.io/docs/features/techdocs/getting-started"
variant="contained"
>
DOCS
</LinkButton>
}
/>
)}
</InfoCard>
);
};
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { FeaturedDocs } from './FeaturedDocs';
+1
View File
@@ -34,6 +34,7 @@ export {
HeaderWorldClock,
HomePageTopVisited,
HomePageRecentlyVisited,
FeaturedDocs,
} from './plugin';
export * from './components';
export * from './assets';
+15
View File
@@ -210,3 +210,18 @@ export const HomePageRecentlyVisited = homePlugin.provide(
import('./homePageComponents/VisitedByType/RecentlyVisited'),
}),
);
/**
* A component to display specific Featured Docs.
*
* @public
*/
export const FeaturedDocs = homePlugin.provide(
createComponentExtension({
name: 'FeaturedDocs',
component: {
lazy: () =>
import('./homePageComponents/FeaturedDocs').then(m => m.FeaturedDocs),
},
}),
);
+2
View File
@@ -7246,6 +7246,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/plugin-home@workspace:plugins/home"
dependencies:
"@backstage/catalog-client": "workspace:^"
"@backstage/catalog-model": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
@@ -7262,6 +7263,7 @@ __metadata:
"@material-ui/core": ^4.12.2
"@material-ui/icons": ^4.9.1
"@material-ui/lab": 4.0.0-alpha.61
"@material-ui/styles": ^4.11.5
"@rjsf/core": 5.13.0
"@rjsf/material-ui": 5.13.0
"@rjsf/utils": 5.13.0