Skip to content

Commit 6ee47ec

Browse files
committed
feat: add useGetResourceKindsOptions for resource list
1 parent ab4f6f0 commit 6ee47ec

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

src/Shared/Hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
*/
1616

1717
export * from './UsePrompt'
18+
export * from './useGetResourceKindsOptions'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as useGetResourceKindsOptions } from './useGetResourceKindsOptions'
2+
export type { UseGetResourceKindsOptionsProps, UseGetResourceKindOptionsReturnType } from './types'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ResponseType, Teams } from '@Common/Types'
2+
import { getTeamListMin } from '@Common/Common.service'
3+
import { get } from '@Common/Api'
4+
import { ClusterType } from '@Shared/Services'
5+
import { EnvironmentType, EnvListMinDTO } from '@Shared/types'
6+
import { EnvironmentTypeEnum } from '@Shared/constants'
7+
import { ROUTES } from '@Common/Constants'
8+
import { AppsGroupedByProjectsType, ClusterDTO } from './types'
9+
10+
export const getAppOptionsGroupedByProjects = async (): Promise<AppsGroupedByProjectsType> => {
11+
const { result } = (await get('app/min')) as ResponseType<AppsGroupedByProjectsType>
12+
13+
return result ?? []
14+
}
15+
16+
export const getProjectOptions = async (): Promise<Pick<Teams, 'id' | 'name'>[]> => {
17+
const { result } = await getTeamListMin()
18+
19+
return (
20+
result?.map(({ id, name }) => ({
21+
id,
22+
name,
23+
})) ?? []
24+
)
25+
}
26+
27+
export const getClusterOptions = async (): Promise<ClusterType[]> => {
28+
const { result } = (await get('cluster/autocomplete')) as ResponseType<ClusterDTO[]>
29+
30+
return (
31+
result?.map((cluster) => ({
32+
id: cluster.id,
33+
name: cluster.cluster_name,
34+
isVirtual: cluster.isVirtualCluster ?? false,
35+
})) ?? []
36+
)
37+
}
38+
39+
export const getEnvironmentOptions = async (): Promise<EnvironmentType[]> => {
40+
const { result } = (await get(ROUTES.ENVIRONMENT_LIST_MIN)) as ResponseType<EnvListMinDTO[]>
41+
42+
return (
43+
result?.map((environment) => ({
44+
id: environment.id,
45+
name: environment.cluster_name,
46+
isVirtual: environment.isVirtualEnvironment ?? false,
47+
cluster: environment.cluster_name,
48+
environmentType: environment.default ? EnvironmentTypeEnum.production : EnvironmentTypeEnum.nonProduction,
49+
namespace: environment.namespace,
50+
})) ?? []
51+
)
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ====== Service Types: Start ====== //
2+
3+
import { ResourceKindType } from '@Shared/types'
4+
import { ServerErrors } from '@Common/ServerError'
5+
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
6+
7+
export interface AppType {
8+
name: string
9+
}
10+
11+
export type AppsGroupedByProjectsType = {
12+
projectId: number
13+
projectName: string
14+
appList: AppType[]
15+
}[]
16+
17+
export interface ClusterDTO {
18+
id: number
19+
cluster_name: string
20+
isVirtualCluster: boolean
21+
}
22+
23+
// ====== Service Types: End ====== //
24+
25+
export interface UseGetResourceKindsOptionsProps {
26+
resourcesToFetch: Extract<
27+
ResourceKindType,
28+
| ResourceKindType.devtronApplication
29+
| ResourceKindType.project
30+
| ResourceKindType.cluster
31+
| ResourceKindType.environment
32+
>[]
33+
}
34+
35+
export interface UseGetResourceKindOptionsReturnType {
36+
isResourcesOptionsLoading: boolean
37+
resourcesOptionsMap: {
38+
[ResourceKindType.devtronApplication]: Awaited<ReturnType<typeof getAppOptionsGroupedByProjects>>
39+
[ResourceKindType.project]: Awaited<ReturnType<typeof getProjectOptions>>
40+
[ResourceKindType.cluster]: Awaited<ReturnType<typeof getClusterOptions>>
41+
[ResourceKindType.environment]: Awaited<ReturnType<typeof getEnvironmentOptions>>
42+
}
43+
resourcesOptionsError: ServerErrors
44+
refetchResourcesOptions: () => void
45+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useMemo } from 'react'
2+
import { ResourceKindType } from '@Shared/types'
3+
import { useAsync } from '@Common/Helper'
4+
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
5+
import { UseGetResourceKindOptionsReturnType, UseGetResourceKindsOptionsProps } from './types'
6+
import { getResourcesToFetchMap } from './utils'
7+
8+
/**
9+
* Generic hook to fetch the options list for the supported resource kinds.
10+
*
11+
* Note: no call would be made for empty resource kind list
12+
*
13+
* @example Usage
14+
* ```tsx
15+
* const data = useGetResourceKindsOptions({
16+
* resourcesToFetch: [ResourceKindType.devtronApplication, ResourceKindType.environment]
17+
* })
18+
* ```
19+
*/
20+
const useGetResourceKindsOptions = ({
21+
resourcesToFetch,
22+
}: UseGetResourceKindsOptionsProps): UseGetResourceKindOptionsReturnType => {
23+
const resourcesToFetchMap = useMemo(() => getResourcesToFetchMap(resourcesToFetch), [resourcesToFetch])
24+
const [isResourcesOptionsLoading, resourcesOptions, resourcesOptionsError, refetchResourcesOptions] = useAsync(
25+
() =>
26+
Promise.all([
27+
resourcesToFetchMap[ResourceKindType.devtronApplication] ? getAppOptionsGroupedByProjects() : null,
28+
resourcesToFetchMap[ResourceKindType.project] ? getProjectOptions() : null,
29+
resourcesToFetchMap[ResourceKindType.cluster] ? getClusterOptions() : null,
30+
resourcesToFetchMap[ResourceKindType.environment] ? getEnvironmentOptions() : null,
31+
]),
32+
[resourcesToFetchMap],
33+
resourcesToFetch.length > 0,
34+
)
35+
36+
return {
37+
isResourcesOptionsLoading,
38+
resourcesOptionsMap: {
39+
[ResourceKindType.devtronApplication]: resourcesOptions?.[0] ?? [],
40+
[ResourceKindType.project]: resourcesOptions?.[1] ?? [],
41+
[ResourceKindType.cluster]: resourcesOptions?.[2] ?? [],
42+
[ResourceKindType.environment]: resourcesOptions?.[3] ?? [],
43+
},
44+
resourcesOptionsError,
45+
refetchResourcesOptions,
46+
}
47+
}
48+
49+
export default useGetResourceKindsOptions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ResourceKindType } from '@Shared/types'
2+
import { UseGetResourceKindsOptionsProps } from './types'
3+
4+
export const getResourcesToFetchMap = (resourcesToFetch: UseGetResourceKindsOptionsProps['resourcesToFetch']) =>
5+
resourcesToFetch.reduce<Record<UseGetResourceKindsOptionsProps['resourcesToFetch'][0], boolean>>(
6+
(acc, resource) => {
7+
acc[resource] = true
8+
9+
return acc
10+
},
11+
{
12+
[ResourceKindType.devtronApplication]: false,
13+
[ResourceKindType.project]: false,
14+
[ResourceKindType.cluster]: false,
15+
[ResourceKindType.environment]: false,
16+
},
17+
)

0 commit comments

Comments
 (0)