Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "0.5.8-beta-5",
"version": "0.5.8-beta-8",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down
61 changes: 39 additions & 22 deletions src/Shared/Hooks/useGetResourceKindsOptions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { ResponseType, Teams } from '@Common/Types'
import { getTeamListMin } from '@Common/Common.service'
import { get } from '@Common/Api'
import { ClusterType } from '@Shared/Services'
import { EnvironmentType, EnvListMinDTO } from '@Shared/types'
import { EnvListMinDTO } from '@Shared/types'
import { EnvironmentTypeEnum } from '@Shared/constants'
import { ROUTES } from '@Common/Constants'
import { stringComparatorBySortOrder } from '@Shared/Helpers'
import { AppsGroupedByProjectsType, ClusterDTO } from './types'
import { AppsGroupedByProjectsType, ClusterDTO, EnvironmentsGroupedByClustersType } from './types'

export const getAppOptionsGroupedByProjects = async (): Promise<AppsGroupedByProjectsType> => {
const { result } = (await get(ROUTES.APP_LIST_MIN)) as ResponseType<AppsGroupedByProjectsType>
Expand Down Expand Up @@ -70,30 +70,47 @@ export const getClusterOptions = async (): Promise<ClusterType[]> => {
.sort((a, b) => stringComparatorBySortOrder(a.name, b.name))
}

export const getEnvironmentOptions = async (): Promise<EnvironmentType[]> => {
export const getEnvironmentOptionsGroupedByClusters = async (): Promise<EnvironmentsGroupedByClustersType> => {
const { result } = (await get(ROUTES.ENVIRONMENT_LIST_MIN)) as ResponseType<EnvListMinDTO[]>

if (!result) {
return []
}

return result
.map(
({
id,
environment_name: name,
isVirtualEnvironment,
cluster_name: cluster,
default: isDefault,
namespace,
}) => ({
id,
name,
isVirtual: isVirtualEnvironment ?? false,
cluster,
environmentType: isDefault ? EnvironmentTypeEnum.production : EnvironmentTypeEnum.nonProduction,
namespace,
}),
)
.sort((a, b) => stringComparatorBySortOrder(a.name, b.name))
const sortedEnvList = result.map(
({
id,
environment_name: name,
isVirtualEnvironment,
cluster_name: cluster,
default: isDefault,
namespace,
}) => ({
id,
name,
isVirtual: isVirtualEnvironment ?? false,
cluster,
environmentType: isDefault ? EnvironmentTypeEnum.production : EnvironmentTypeEnum.nonProduction,
namespace,
}),
)

const envGroupedByCluster = Object.values(
sortedEnvList.reduce<
Record<EnvironmentsGroupedByClustersType[number]['clusterName'], EnvironmentsGroupedByClustersType[number]>
>((acc, env) => {
if (!acc[env.cluster]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can move env.cluster to variable

acc[env.cluster] = {
clusterName: env.cluster,
envList: [],
}
}

acc[env.cluster].envList.push(env)

return acc
}, {}),
).sort((a, b) => stringComparatorBySortOrder(a.clusterName, b.clusterName))

return envGroupedByCluster
}
11 changes: 8 additions & 3 deletions src/Shared/Hooks/useGetResourceKindsOptions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

// ====== Service Types: Start ====== //

import { ResourceKindType } from '@Shared/types'
import { EnvironmentType, ResourceKindType } from '@Shared/types'
import { ServerErrors } from '@Common/ServerError'
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
import { getAppOptionsGroupedByProjects, getClusterOptions, getProjectOptions } from './service'

export interface AppType {
name: string
Expand All @@ -30,6 +30,11 @@ export type AppsGroupedByProjectsType = {
appList: AppType[]
}[]

export type EnvironmentsGroupedByClustersType = {
clusterName: EnvironmentType['cluster']
envList: EnvironmentType[]
}[]

export interface ClusterDTO {
id: number
cluster_name: string
Expand All @@ -54,7 +59,7 @@ export interface UseGetResourceKindOptionsReturnType {
[ResourceKindType.devtronApplication]: Awaited<ReturnType<typeof getAppOptionsGroupedByProjects>>
[ResourceKindType.project]: Awaited<ReturnType<typeof getProjectOptions>>
[ResourceKindType.cluster]: Awaited<ReturnType<typeof getClusterOptions>>
[ResourceKindType.environment]: Awaited<ReturnType<typeof getEnvironmentOptions>>
[ResourceKindType.environment]: EnvironmentsGroupedByClustersType
}
resourcesOptionsError: ServerErrors
refetchResourcesOptions: () => void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import { useMemo } from 'react'
import { ResourceKindType } from '@Shared/types'
import { useAsync } from '@Common/Helper'
import { getAppOptionsGroupedByProjects, getClusterOptions, getEnvironmentOptions, getProjectOptions } from './service'
import {
getAppOptionsGroupedByProjects,
getClusterOptions,
getEnvironmentOptionsGroupedByClusters,
getProjectOptions,
} from './service'
import { UseGetResourceKindOptionsReturnType, UseGetResourceKindsOptionsProps } from './types'
import { getResourcesToFetchMap } from './utils'

Expand All @@ -43,7 +48,7 @@ const useGetResourceKindsOptions = ({
resourcesToFetchMap[ResourceKindType.devtronApplication] ? getAppOptionsGroupedByProjects() : null,
resourcesToFetchMap[ResourceKindType.project] ? getProjectOptions() : null,
resourcesToFetchMap[ResourceKindType.cluster] ? getClusterOptions() : null,
resourcesToFetchMap[ResourceKindType.environment] ? getEnvironmentOptions() : null,
resourcesToFetchMap[ResourceKindType.environment] ? getEnvironmentOptionsGroupedByClusters() : null,
]),
[resourcesToFetchMap],
resourcesToFetch.length > 0,
Expand Down