diff --git a/.changeset/swift-mails-sing.md b/.changeset/swift-mails-sing.md new file mode 100644 index 0000000000..3df53e0340 --- /dev/null +++ b/.changeset/swift-mails-sing.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs': patch +--- + +The panels of `TechDocsCustomHome` now use the `useEntityOwnership` hook to resolve ownership when the `'ownedByUser'` filter predicate is used. diff --git a/plugins/techdocs/src/home/components/TechDocsCustomHome.tsx b/plugins/techdocs/src/home/components/TechDocsCustomHome.tsx index d2f0266af6..7023560e83 100644 --- a/plugins/techdocs/src/home/components/TechDocsCustomHome.tsx +++ b/plugins/techdocs/src/home/components/TechDocsCustomHome.tsx @@ -15,21 +15,16 @@ */ import React, { useState } from 'react'; -import useAsync, { AsyncState } from 'react-use/lib/useAsync'; +import useAsync from 'react-use/lib/useAsync'; import { makeStyles } from '@material-ui/core'; import { CSSProperties } from '@material-ui/styles'; import { CATALOG_FILTER_EXISTS, catalogApiRef, CatalogApi, - isOwnerOf, + useEntityOwnership, } from '@backstage/plugin-catalog-react'; -import { - DEFAULT_NAMESPACE, - Entity, - parseEntityRef, - UserEntity, -} from '@backstage/catalog-model'; +import { Entity } from '@backstage/catalog-model'; import { DocsTable } from './Tables'; import { DocsCardGrid } from './Grids'; import { TechDocsPageWrapper } from './TechDocsPageWrapper'; @@ -43,8 +38,7 @@ import { SupportButton, ContentHeader, } from '@backstage/core-components'; - -import { identityApiRef, useApi } from '@backstage/core-plugin-api'; +import { useApi } from '@backstage/core-plugin-api'; const panels = { DocsTable: DocsTable, @@ -104,16 +98,16 @@ const CustomPanel = ({ }, }); const classes = useStyles(); - const { value: user } = useOwnUser(); + const { loading: loadingOwnership, isOwnedEntity } = useEntityOwnership(); const Panel = panels[config.panelType]; const shownEntities = entities.filter(entity => { if (config.filterPredicate === 'ownedByUser') { - if (!user) { + if (loadingOwnership) { return false; } - return isOwnerOf(user, entity); + return isOwnedEntity(entity); } return ( @@ -225,23 +219,3 @@ export const TechDocsCustomHome = (props: TechDocsCustomHomeProps) => { ); }; - -function useOwnUser(): AsyncState { - const catalogApi = useApi(catalogApiRef); - const identityApi = useApi(identityApiRef); - - return useAsync(async () => { - const identity = await identityApi.getBackstageIdentity(); - // TODO(freben): Defensively parse with defaults even though getEntityByRef - // supports the string form, since some auth resolvers have been known to - // return incomplete refs (just the name part) historically. This can be - // simplified in the future to just pass the ref immediately to - // getEntityByRef. - return catalogApi.getEntityByRef( - parseEntityRef(identity.userEntityRef, { - defaultKind: 'User', - defaultNamespace: DEFAULT_NAMESPACE, - }), - ) as Promise; - }, [catalogApi, identityApi]); -}