diff --git a/.changeset/wet-cats-allow.md b/.changeset/wet-cats-allow.md new file mode 100644 index 0000000000..5c642109b0 --- /dev/null +++ b/.changeset/wet-cats-allow.md @@ -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 diff --git a/plugins/home/package.json b/plugins/home/package.json index 5db7347f7c..49d61d0c16 100644 --- a/plugins/home/package.json +++ b/plugins/home/package.json @@ -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", diff --git a/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.stories.tsx b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.stories.tsx new file mode 100644 index 0000000000..c7b9a77be5 --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.stories.tsx @@ -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(() => ({ + 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>) => + wrapInTestApp( + + + , + { + mountedRoutes: { + '/catalog/:namespace/:kind/:name': entityRouteRef, + }, + }, + ), + ], +}; + +export const Default = () => { + return ( + + + + ); +}; + +export const ExampleCustomized = () => { + const styles = useStyles(); + const cardTitle = ( + <> + +   Important + + ); + return ( + + + + ); +}; diff --git a/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.test.tsx b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.test.tsx new file mode 100644 index 0000000000..435e4a3686 --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.test.tsx @@ -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('', () => { + const mockCatalogApi = { + getEntities: jest + .fn() + .mockImplementation(async () => ({ items: docsEntities })), + }; + let Wrapper: React.ComponentType; + + beforeAll(() => { + Wrapper = ({ children }: { children?: React.ReactNode }) => ( + + {children} + + ); + }); + it('should show expected featured doc and title', async () => { + const { getByTestId, getByText } = await renderInTestApp( + + + , + { + 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(); + }); +}); diff --git a/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.tsx b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.tsx new file mode 100644 index 0000000000..459dc7bdb6 --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocs/FeaturedDocs.tsx @@ -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 | undefined; + emptyState?: React.ReactNode | undefined; + subLinkText?: string | undefined; + title?: React.ReactNode | string | undefined; +}; + +const useStyles = makeStyles(() => ({ + 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 | 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 ; + } + + if (error) { + return ( + + + + ); + } + + return ( + + {entities?.length + ? entities.map(d => ( +
+ + {d.metadata.title} + + + {d.metadata.description} + + + {linkText} + +
+ )) + : emptyState || ( + + DOCS + + } + /> + )} +
+ ); +}; diff --git a/plugins/home/src/homePageComponents/FeaturedDocs/index.ts b/plugins/home/src/homePageComponents/FeaturedDocs/index.ts new file mode 100644 index 0000000000..08ca030dca --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocs/index.ts @@ -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'; diff --git a/plugins/home/src/index.ts b/plugins/home/src/index.ts index a80f8bd93d..c7ae5c2467 100644 --- a/plugins/home/src/index.ts +++ b/plugins/home/src/index.ts @@ -34,6 +34,7 @@ export { HeaderWorldClock, HomePageTopVisited, HomePageRecentlyVisited, + FeaturedDocs, } from './plugin'; export * from './components'; export * from './assets'; diff --git a/plugins/home/src/plugin.ts b/plugins/home/src/plugin.ts index ab8c46baa1..d4ee89b707 100644 --- a/plugins/home/src/plugin.ts +++ b/plugins/home/src/plugin.ts @@ -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), + }, + }), +); diff --git a/yarn.lock b/yarn.lock index d5abffa914..6683bdd76c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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