Merge branch 'master' into feat/home-plugin-group-starred-entities-by-kind
Signed-off-by: Antonio Bergas <anthonyprincedom@gmail.com>
This commit is contained in:
@@ -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('<FeaturedDocsCard />', () => {
|
||||
const mockCatalogApi = {
|
||||
getEntities: jest
|
||||
.fn()
|
||||
.mockImplementation(async () => ({ items: docsEntities })),
|
||||
};
|
||||
let Wrapper: React.ComponentType<React.PropsWithChildren<{}>>;
|
||||
|
||||
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>
|
||||
<Content
|
||||
filter={{
|
||||
'spec.type': 'documentation',
|
||||
'metadata.name': 'getting-started-with-idp',
|
||||
}}
|
||||
emptyState={undefined}
|
||||
/>
|
||||
</Wrapper>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
'/home': entityRouteRef,
|
||||
},
|
||||
},
|
||||
);
|
||||
const docsCardContent = getByTestId('docs-card-content');
|
||||
const docsEntity = getByText('Getting Started Docs');
|
||||
|
||||
expect(docsCardContent).toContainElement(docsEntity);
|
||||
});
|
||||
});
|
||||
@@ -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, Typography } from '@material-ui/core';
|
||||
|
||||
/**
|
||||
* Props customizing the <FeaturedDocsCard/> 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 <Progress />;
|
||||
}
|
||||
if (error) {
|
||||
return <ErrorPanel error={error} />;
|
||||
}
|
||||
|
||||
return entities?.length ? (
|
||||
<>
|
||||
{entities.map(d => (
|
||||
<div
|
||||
key={`${d.metadata.name}-${d.kind}-${d.metadata.namespace}`}
|
||||
data-testid="docs-card-content"
|
||||
>
|
||||
<Link
|
||||
className={styles.docsTitleLink}
|
||||
data-testid="docs-card-title"
|
||||
to={
|
||||
linkDestination ||
|
||||
`/docs/${d.metadata.namespace || 'default'}/${d.kind}/${
|
||||
d.metadata.name
|
||||
}/`
|
||||
}
|
||||
>
|
||||
{d.metadata.title}
|
||||
</Link>
|
||||
{d.metadata.description && (
|
||||
<Typography className={styles.docDescription}>
|
||||
{d.metadata.description}
|
||||
</Typography>
|
||||
)}
|
||||
<Link
|
||||
className={styles.docSubLink}
|
||||
data-testid="docs-card-sub-link"
|
||||
to={
|
||||
linkDestination ||
|
||||
`/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
|
||||
to="https://backstage.io/docs/features/techdocs/getting-started"
|
||||
variant="contained"
|
||||
>
|
||||
DOCS
|
||||
</LinkButton>
|
||||
}
|
||||
/>
|
||||
)
|
||||
);
|
||||
};
|
||||
@@ -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<PropsWithChildren<{}>>) =>
|
||||
wrapInTestApp(
|
||||
<TestApiProvider apis={[[catalogApiRef, mockCatalogApi]]}>
|
||||
<Story />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
'/catalog/:namespace/:kind/:name': entityRouteRef,
|
||||
},
|
||||
},
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
export const Default = () => {
|
||||
return (
|
||||
<Grid item xs={12} md={6}>
|
||||
<FeaturedDocsCard
|
||||
filter={{
|
||||
'spec.type': 'documentation',
|
||||
'metadata.name': 'getting-started-with-backstage',
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
@@ -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';
|
||||
@@ -18,4 +18,5 @@ 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';
|
||||
export type { StarredEntitiesProps } from './StarredEntities';
|
||||
|
||||
@@ -34,6 +34,7 @@ export {
|
||||
HeaderWorldClock,
|
||||
HomePageTopVisited,
|
||||
HomePageRecentlyVisited,
|
||||
FeaturedDocsCard,
|
||||
} from './plugin';
|
||||
export * from './components';
|
||||
export * from './assets';
|
||||
|
||||
@@ -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';
|
||||
import { StarredEntitiesProps } from './homePageComponents/StarredEntities/Content';
|
||||
@@ -211,3 +215,16 @@ export const HomePageRecentlyVisited = homePlugin.provide(
|
||||
import('./homePageComponents/VisitedByType/RecentlyVisited'),
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* A component to display specific Featured Docs.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const FeaturedDocsCard = homePlugin.provide(
|
||||
createCardExtension<FeaturedDocsCardProps>({
|
||||
name: 'FeaturedDocsCard',
|
||||
title: 'Featured Docs',
|
||||
components: () => import('./homePageComponents/FeaturedDocsCard'),
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user