diff --git a/.changeset/wet-cats-allow.md b/.changeset/wet-cats-allow.md new file mode 100644 index 0000000000..12d0eb7069 --- /dev/null +++ b/.changeset/wet-cats-allow.md @@ -0,0 +1,22 @@ +--- +'@backstage/plugin-home': patch +--- + +Added a new Featured Docs component to `plugin-home`, which can display any entity given a filter. + +``` +import { FeaturedDocsCard } from '@backstage/plugin-home'; + + +``` + +See the [storybook examples](https://backstage.io/storybook/?path=/story/plugins-home-components-featureddocs--default) diff --git a/plugins/home/api-report.md b/plugins/home/api-report.md index 14f685cfc1..18049cae84 100644 --- a/plugins/home/api-report.md +++ b/plugins/home/api-report.md @@ -14,6 +14,7 @@ import { CardSettings as CardSettings_2 } from '@backstage/plugin-home-react'; import { ComponentParts as ComponentParts_2 } from '@backstage/plugin-home-react'; import { ComponentRenderer as ComponentRenderer_2 } from '@backstage/plugin-home-react'; import { createCardExtension as createCardExtension_2 } from '@backstage/plugin-home-react'; +import { EntityFilterQuery } from '@backstage/catalog-client'; import { ErrorApi } from '@backstage/core-plugin-api'; import { IdentityApi } from '@backstage/core-plugin-api'; import { JSX as JSX_2 } from 'react'; @@ -101,6 +102,20 @@ export type CustomHomepageGridProps = { preventCollision?: boolean; }; +// @public +export const FeaturedDocsCard: ( + props: CardExtensionProps_2, +) => JSX_2.Element; + +// @public +export type FeaturedDocsCardProps = { + filter: EntityFilterQuery; + emptyState?: React_2.JSX.Element; + linkDestination?: string; + responseLimit?: number; + subLinkText?: string; +}; + // @public export const HeaderWorldClock: (props: { clockConfigs: ClockConfig[]; diff --git a/plugins/home/package.json b/plugins/home/package.json index 4112e9a30b..7c943d998b 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:^", diff --git a/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.test.tsx b/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.test.tsx new file mode 100644 index 0000000000..427a1484d5 --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.test.tsx @@ -0,0 +1,73 @@ +/* + * 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 { Content } from './Content'; +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'); + + expect(docsCardContent).toContainElement(docsEntity); + }); +}); diff --git a/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.tsx b/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.tsx new file mode 100644 index 0000000000..dfa83ce074 --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocsCard/Content.tsx @@ -0,0 +1,158 @@ +/* + * 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, + Progress, + ErrorPanel, +} 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 { makeStyles, Theme, Typography } from '@material-ui/core'; + +/** + * Props customizing the component. + * + * @public + */ +export type FeaturedDocsCardProps = { + /** The entity filter used to display only the intended item/s */ + filter: EntityFilterQuery; + /** An optional ReactNode for empty states */ + emptyState?: React.JSX.Element; + /** An optional linkDestination to set for the Featured Doc */ + linkDestination?: string; + /** An optional limit to set for link destination */ + responseLimit?: number; + /** An optional string to customize sublink text */ + subLinkText?: string; +}; + +const useStyles = makeStyles( + theme => ({ + docDescription: { + fontSize: theme.typography.body1.fontSize, + fontWeight: theme.typography.body1.fontWeight, + marginBottom: theme.spacing(2), + marginTop: theme.spacing(2), + }, + docSubLink: { + fontSize: theme.typography.subtitle1.fontSize, + fontWeight: theme.typography.subtitle1.fontWeight, + lineHeight: theme.typography.subtitle1.lineHeight, + }, + docsTitleLink: { + fontSize: theme.typography.h6.fontSize, + fontWeight: theme.typography.h6.fontWeight, + lineHeight: theme.typography.h6.lineHeight, + }, + }), + { name: 'HomeFeaturedDocsCard' }, +); + +/** + * A component to display specific Featured Docs. + * + * @public + */ +export const Content = (props: FeaturedDocsCardProps): JSX.Element => { + const { emptyState, filter, linkDestination, responseLimit, subLinkText } = + props; + const linkText = subLinkText || 'LEARN MORE'; + const styles = useStyles(); + const catalogApi: CatalogApi = useApi(catalogApiRef); + const { + value: entities, + loading, + error, + } = useAsync(async () => { + const response = await catalogApi.getEntities({ + filter: filter, + limit: responseLimit || 10, + }); + return response.items; + }); + + if (loading) { + return ; + } + if (error) { + return ; + } + + return entities?.length ? ( + <> + {entities.map(d => ( +
+ + {d.metadata.title} + + {d.metadata.description && ( + + {d.metadata.description} + + )} + + {linkText} + +
+ ))} + + ) : ( + emptyState || ( + + DOCS + + } + /> + ) + ); +}; diff --git a/plugins/home/src/homePageComponents/FeaturedDocsCard/FeaturedDocsCard.stories.tsx b/plugins/home/src/homePageComponents/FeaturedDocsCard/FeaturedDocsCard.stories.tsx new file mode 100644 index 0000000000..91f0dbf27e --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocsCard/FeaturedDocsCard.stories.tsx @@ -0,0 +1,71 @@ +/* + * 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 { FeaturedDocsCard } 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 } from '@material-ui/core'; + +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 }), +}; + +export default { + title: 'Plugins/Home/Components/FeaturedDocsCard', + decorators: [ + (Story: ComponentType>) => + wrapInTestApp( + + + , + { + mountedRoutes: { + '/catalog/:namespace/:kind/:name': entityRouteRef, + }, + }, + ), + ], +}; + +export const Default = () => { + return ( + + + + ); +}; diff --git a/plugins/home/src/homePageComponents/FeaturedDocsCard/index.ts b/plugins/home/src/homePageComponents/FeaturedDocsCard/index.ts new file mode 100644 index 0000000000..5151fa370c --- /dev/null +++ b/plugins/home/src/homePageComponents/FeaturedDocsCard/index.ts @@ -0,0 +1,18 @@ +/* + * 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 { Content } from './Content'; +export type { FeaturedDocsCardProps } from './Content'; diff --git a/plugins/home/src/homePageComponents/index.ts b/plugins/home/src/homePageComponents/index.ts index 9149afa295..dc611291fb 100644 --- a/plugins/home/src/homePageComponents/index.ts +++ b/plugins/home/src/homePageComponents/index.ts @@ -18,3 +18,4 @@ export type { ToolkitContentProps, Tool } from './Toolkit'; export type { ClockConfig } from './HeaderWorldClock'; export type { WelcomeTitleLanguageProps } from './WelcomeTitle'; export type { VisitedByTypeProps, VisitedByTypeKind } from './VisitedByType'; +export type { FeaturedDocsCardProps } from './FeaturedDocsCard'; diff --git a/plugins/home/src/index.ts b/plugins/home/src/index.ts index a80f8bd93d..154186f169 100644 --- a/plugins/home/src/index.ts +++ b/plugins/home/src/index.ts @@ -34,6 +34,7 @@ export { HeaderWorldClock, HomePageTopVisited, HomePageRecentlyVisited, + FeaturedDocsCard, } from './plugin'; export * from './components'; export * from './assets'; diff --git a/plugins/home/src/plugin.ts b/plugins/home/src/plugin.ts index ab8c46baa1..5361ecd9c0 100644 --- a/plugins/home/src/plugin.ts +++ b/plugins/home/src/plugin.ts @@ -23,7 +23,11 @@ import { storageApiRef, } from '@backstage/core-plugin-api'; import { createCardExtension } from '@backstage/plugin-home-react'; -import { ToolkitContentProps, VisitedByTypeProps } from './homePageComponents'; +import { + ToolkitContentProps, + VisitedByTypeProps, + FeaturedDocsCardProps, +} from './homePageComponents'; import { rootRouteRef } from './routes'; import { VisitsStorageApi, visitsApiRef } from './api'; @@ -210,3 +214,16 @@ export const HomePageRecentlyVisited = homePlugin.provide( import('./homePageComponents/VisitedByType/RecentlyVisited'), }), ); + +/** + * A component to display specific Featured Docs. + * + * @public + */ +export const FeaturedDocsCard = homePlugin.provide( + createCardExtension({ + name: 'FeaturedDocsCard', + title: 'Featured Docs', + components: () => import('./homePageComponents/FeaturedDocsCard'), + }), +); diff --git a/yarn.lock b/yarn.lock index 15365689ff..714a17d713 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7492,6 +7492,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:^"