From 562670a7cbf9c1ab451f66c1ebf088d282c45d0e Mon Sep 17 00:00:00 2001 From: Marley Powell Date: Thu, 16 Dec 2021 14:38:13 +0000 Subject: [PATCH] feat: Created `useUserTeamIds` hook. Signed-off-by: Marley Powell --- plugins/azure-devops/src/hooks/index.ts | 1 + .../azure-devops/src/hooks/useUserTeamIds.ts | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 plugins/azure-devops/src/hooks/useUserTeamIds.ts diff --git a/plugins/azure-devops/src/hooks/index.ts b/plugins/azure-devops/src/hooks/index.ts index 44b94f3f4b..a3864c21f7 100644 --- a/plugins/azure-devops/src/hooks/index.ts +++ b/plugins/azure-devops/src/hooks/index.ts @@ -20,3 +20,4 @@ export * from './useProjectRepoFromEntity'; export * from './usePullRequests'; export * from './useRepoBuilds'; export * from './useUserEmail'; +export * from './useUserTeamIds'; diff --git a/plugins/azure-devops/src/hooks/useUserTeamIds.ts b/plugins/azure-devops/src/hooks/useUserTeamIds.ts new file mode 100644 index 0000000000..878dbebdfe --- /dev/null +++ b/plugins/azure-devops/src/hooks/useUserTeamIds.ts @@ -0,0 +1,41 @@ +/* + * Copyright 2021 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 { azureDevOpsApiRef } from '../api'; +import { useApi } from '@backstage/core-plugin-api'; +import { useAsync } from 'react-use'; + +export function useUserTeamIds(userId: string | undefined): { + teamIds?: string[]; + loading: boolean; + error?: Error; +} { + const api = useApi(azureDevOpsApiRef); + + const { + value: teamIds, + loading, + error, + } = useAsync(() => { + return userId ? api.getUserTeamIds(userId) : Promise.resolve(undefined); + }, [api]); + + return { + teamIds, + loading, + error, + }; +}