diff --git a/.changeset/eleven-baboons-sparkle.md b/.changeset/eleven-baboons-sparkle.md new file mode 100644 index 0000000000000..7dba4792bcb19 --- /dev/null +++ b/.changeset/eleven-baboons-sparkle.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-react': minor +--- + +added useOwnedEntities hook to get the list of entities of the logged-in user diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index c4aa254226507..ef469e3b02262 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -9,6 +9,7 @@ import { ApiRef } from '@backstage/core-plugin-api'; import { AsyncState } from 'react-use/lib/useAsync'; import { CATALOG_FILTER_EXISTS } from '@backstage/catalog-client'; import { CatalogApi } from '@backstage/catalog-client'; +import { CatalogListResponse } from '@backstage/catalog-client'; import { ComponentEntity } from '@backstage/catalog-model'; import { ComponentProps } from 'react'; import { Context } from 'react'; @@ -847,6 +848,12 @@ export function useEntityOwnership(): { // @public export function useEntityTypeFilter(): EntityTypeReturn; +// @public +export function useOwnedEntities(allowedKinds?: string[]): { + loading: boolean; + ownedEntities: CatalogListResponse | undefined; +}; + // Warning: (ae-missing-release-tag) "useOwnUser" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public diff --git a/plugins/catalog-react/src/hooks/index.ts b/plugins/catalog-react/src/hooks/index.ts index bbbca3a50126a..a6684732ce0e0 100644 --- a/plugins/catalog-react/src/hooks/index.ts +++ b/plugins/catalog-react/src/hooks/index.ts @@ -42,3 +42,4 @@ export { useEntityOwnership, loadIdentityOwnerRefs, } from './useEntityOwnership'; +export { useOwnedEntities } from './useOwnedEntities'; diff --git a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/useOwnedEntities.ts b/plugins/catalog-react/src/hooks/useOwnedEntities.ts similarity index 82% rename from plugins/scaffolder/src/components/fields/OwnedEntityPicker/useOwnedEntities.ts rename to plugins/catalog-react/src/hooks/useOwnedEntities.ts index ecdfc5c90f12e..f9191364cb72c 100644 --- a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/useOwnedEntities.ts +++ b/plugins/catalog-react/src/hooks/useOwnedEntities.ts @@ -13,17 +13,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { catalogApiRef } from './../api'; import { - catalogApiRef, loadCatalogOwnerRefs, loadIdentityOwnerRefs, -} from '@backstage/plugin-catalog-react'; +} from './useEntityOwnership'; import { identityApiRef, useApi } from '@backstage/core-plugin-api'; import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model'; import { CatalogListResponse } from '@backstage/catalog-client'; import { useAsync } from 'react-use'; import { useMemo } from 'react'; +/** + * Takes the relevant parts of the Backstage identity, and translates them into + * a list of entities which are owned by the user. Takes an optional parameter + * to filter the entities based on allowedKinds + * + * @public + * + * @param allowedKinds - Array of allowed kinds to filter the entities + * @returns CatalogListResponse + */ export function useOwnedEntities(allowedKinds?: string[]): { loading: boolean; ownedEntities: CatalogListResponse | undefined; @@ -52,6 +62,7 @@ export function useOwnedEntities(allowedKinds?: string[]): { ); return catalogs; }, []); + const ownedEntities = useMemo(() => { return refs; }, [refs]); diff --git a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/OwnedEntityPicker.tsx b/plugins/scaffolder/src/components/fields/OwnedEntityPicker/OwnedEntityPicker.tsx index 06b637a79ca7b..3c2fc2b495c36 100644 --- a/plugins/scaffolder/src/components/fields/OwnedEntityPicker/OwnedEntityPicker.tsx +++ b/plugins/scaffolder/src/components/fields/OwnedEntityPicker/OwnedEntityPicker.tsx @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { formatEntityRefTitle } from '@backstage/plugin-catalog-react'; +import { + formatEntityRefTitle, + useOwnedEntities, +} from '@backstage/plugin-catalog-react'; import { TextField } from '@material-ui/core'; import FormControl from '@material-ui/core/FormControl'; import Autocomplete from '@material-ui/lab/Autocomplete'; import { FieldProps } from '@rjsf/core'; import React from 'react'; -import { useOwnedEntities } from './useOwnedEntities'; export const OwnedEntityPicker = ({ onChange,