|
| 1 | +import { useMemo, useState } from 'react' |
| 2 | +import { generatePath, Route, Switch, useLocation, useRouteMatch } from 'react-router-dom' |
| 3 | + |
| 4 | +import { ReactComponent as ICError } from '@Icons/ic-error.svg' |
| 5 | +import { getAppEnvDeploymentConfigList } from '@Shared/Components/DeploymentConfigDiff' |
| 6 | +import { useAsync } from '@Common/Helper' |
| 7 | +import { EnvResourceType, getAppEnvDeploymentConfig } from '@Shared/Services' |
| 8 | +import { groupArrayByObjectKey } from '@Shared/Helpers' |
| 9 | +import ErrorScreenManager from '@Common/ErrorScreenManager' |
| 10 | +import { Progressing } from '@Common/Progressing' |
| 11 | +import { useUrlFilters } from '@Common/Hooks' |
| 12 | +import { GenericEmptyState, InfoColourBar } from '@Common/index' |
| 13 | + |
| 14 | +import { DeploymentHistoryConfigDiffCompare } from './DeploymentHistoryConfigDiffCompare' |
| 15 | +import { DeploymentHistoryConfigDiffProps, DeploymentHistoryConfigDiffQueryParams } from './types' |
| 16 | +import { |
| 17 | + getPipelineDeploymentsWfrIds, |
| 18 | + getPipelineDeployments, |
| 19 | + parseDeploymentHistoryDiffSearchParams, |
| 20 | + isDeploymentHistoryConfigDiffNotFoundError, |
| 21 | + getDeploymentHistoryConfigDiffError, |
| 22 | +} from './utils' |
| 23 | +import { renderDeploymentHistoryConfig } from './helpers' |
| 24 | + |
| 25 | +export const DeploymentHistoryConfigDiff = ({ |
| 26 | + appName, |
| 27 | + envName, |
| 28 | + pipelineId, |
| 29 | + wfrId, |
| 30 | + triggerHistory, |
| 31 | + setFullScreenView, |
| 32 | + runSource, |
| 33 | + resourceId, |
| 34 | + renderRunSource, |
| 35 | +}: DeploymentHistoryConfigDiffProps) => { |
| 36 | + // HOOKS |
| 37 | + const { path, params } = useRouteMatch() |
| 38 | + const { pathname, search } = useLocation() |
| 39 | + |
| 40 | + // CONSTANTS |
| 41 | + const pipelineDeployments = useMemo(() => getPipelineDeployments(triggerHistory), [triggerHistory]) |
| 42 | + const { currentWfrId, previousWfrId } = useMemo( |
| 43 | + () => getPipelineDeploymentsWfrIds({ pipelineDeployments, wfrId }), |
| 44 | + [pipelineDeployments, wfrId], |
| 45 | + ) |
| 46 | + const isPreviousDeploymentConfigAvailable = !!previousWfrId |
| 47 | + |
| 48 | + // URL FILTERS |
| 49 | + const { compareWfrId } = useUrlFilters<string, DeploymentHistoryConfigDiffQueryParams>({ |
| 50 | + parseSearchParams: parseDeploymentHistoryDiffSearchParams(previousWfrId), |
| 51 | + }) |
| 52 | + |
| 53 | + // STATES |
| 54 | + const [convertVariables, setConvertVariables] = useState(false) |
| 55 | + |
| 56 | + // ASYNC CALLS |
| 57 | + // Load comparison deployment data |
| 58 | + const [compareDeploymentConfigLoader, compareDeploymentConfig, , reloadCompareDeploymentConfig] = useAsync( |
| 59 | + () => |
| 60 | + Promise.allSettled([ |
| 61 | + getAppEnvDeploymentConfig({ |
| 62 | + params: { |
| 63 | + appName, |
| 64 | + envName, |
| 65 | + configArea: 'DeploymentHistory', |
| 66 | + pipelineId, |
| 67 | + wfrId: currentWfrId, |
| 68 | + }, |
| 69 | + }), |
| 70 | + isPreviousDeploymentConfigAvailable |
| 71 | + ? getAppEnvDeploymentConfig({ |
| 72 | + params: { |
| 73 | + appName, |
| 74 | + envName, |
| 75 | + configArea: 'DeploymentHistory', |
| 76 | + pipelineId, |
| 77 | + wfrId: compareWfrId, |
| 78 | + }, |
| 79 | + }) |
| 80 | + : null, |
| 81 | + ]), |
| 82 | + [currentWfrId, compareWfrId], |
| 83 | + ) |
| 84 | + |
| 85 | + // METHODS |
| 86 | + const getNavItemHref = (resourceType: EnvResourceType, resourceName: string) => |
| 87 | + `${generatePath(path, { ...params })}/${resourceType}${resourceName ? `/${resourceName}` : ''}${search}` |
| 88 | + |
| 89 | + // Generate the deployment history config list |
| 90 | + const deploymentConfigList = useMemo(() => { |
| 91 | + if (!compareDeploymentConfigLoader && compareDeploymentConfig) { |
| 92 | + const compareList = |
| 93 | + isPreviousDeploymentConfigAvailable && compareDeploymentConfig[1].status === 'fulfilled' |
| 94 | + ? compareDeploymentConfig[1].value.result |
| 95 | + : { |
| 96 | + configMapData: null, |
| 97 | + deploymentTemplate: null, |
| 98 | + secretsData: null, |
| 99 | + isAppAdmin: false, |
| 100 | + } |
| 101 | + |
| 102 | + const currentList = |
| 103 | + compareDeploymentConfig[0].status === 'fulfilled' ? compareDeploymentConfig[0].value.result : null |
| 104 | + |
| 105 | + const configData = getAppEnvDeploymentConfigList({ |
| 106 | + currentList, |
| 107 | + compareList, |
| 108 | + getNavItemHref, |
| 109 | + convertVariables, |
| 110 | + }) |
| 111 | + return configData |
| 112 | + } |
| 113 | + |
| 114 | + return null |
| 115 | + }, [isPreviousDeploymentConfigAvailable, compareDeploymentConfigLoader, compareDeploymentConfig, convertVariables]) |
| 116 | + |
| 117 | + const compareDeploymentConfigErr = useMemo( |
| 118 | + () => |
| 119 | + !compareDeploymentConfigLoader && compareDeploymentConfig |
| 120 | + ? getDeploymentHistoryConfigDiffError(compareDeploymentConfig[0]) || |
| 121 | + getDeploymentHistoryConfigDiffError(compareDeploymentConfig[1]) |
| 122 | + : null, |
| 123 | + [compareDeploymentConfigLoader, compareDeploymentConfig], |
| 124 | + ) |
| 125 | + |
| 126 | + const groupedDeploymentConfigList = useMemo( |
| 127 | + () => (deploymentConfigList ? groupArrayByObjectKey(deploymentConfigList.configList, 'groupHeader') : []), |
| 128 | + [deploymentConfigList], |
| 129 | + ) |
| 130 | + |
| 131 | + /** Previous deployment config has 404 error. */ |
| 132 | + const hasPreviousDeploymentConfigNotFoundError = |
| 133 | + compareDeploymentConfig && isDeploymentHistoryConfigDiffNotFoundError(compareDeploymentConfig[1]) |
| 134 | + /** Hide diff state if the previous deployment config is unavailable or returns a 404 error. */ |
| 135 | + const hideDiffState = !isPreviousDeploymentConfigAvailable || hasPreviousDeploymentConfigNotFoundError |
| 136 | + // LOADING |
| 137 | + const isLoading = compareDeploymentConfigLoader || (!compareDeploymentConfigErr && !deploymentConfigList) |
| 138 | + // ERROR CONFIG |
| 139 | + const errorConfig = { |
| 140 | + code: compareDeploymentConfigErr?.code, |
| 141 | + error: compareDeploymentConfigErr && !compareDeploymentConfigLoader, |
| 142 | + reload: reloadCompareDeploymentConfig, |
| 143 | + } |
| 144 | + |
| 145 | + if (compareDeploymentConfig && isDeploymentHistoryConfigDiffNotFoundError(compareDeploymentConfig[0])) { |
| 146 | + return ( |
| 147 | + <div className="flex bcn-0 h-100"> |
| 148 | + <GenericEmptyState |
| 149 | + title="Data not available" |
| 150 | + subTitle="Configurations used for this deployment execution is not available" |
| 151 | + /> |
| 152 | + </div> |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + return ( |
| 157 | + <Switch> |
| 158 | + <Route path={`${path}/:resourceType(${Object.values(EnvResourceType).join('|')})/:resourceName?`}> |
| 159 | + <DeploymentHistoryConfigDiffCompare |
| 160 | + {...deploymentConfigList} |
| 161 | + isLoading={isLoading} |
| 162 | + errorConfig={errorConfig} |
| 163 | + envName={envName} |
| 164 | + wfrId={wfrId} |
| 165 | + previousWfrId={previousWfrId} |
| 166 | + pipelineDeployments={pipelineDeployments} |
| 167 | + setFullScreenView={setFullScreenView} |
| 168 | + convertVariables={convertVariables} |
| 169 | + setConvertVariables={setConvertVariables} |
| 170 | + runSource={runSource} |
| 171 | + resourceId={resourceId} |
| 172 | + renderRunSource={renderRunSource} |
| 173 | + hideDiffState={hideDiffState} |
| 174 | + isCompareDeploymentConfigNotAvailable={hasPreviousDeploymentConfigNotFoundError} |
| 175 | + /> |
| 176 | + </Route> |
| 177 | + <Route> |
| 178 | + {compareDeploymentConfigErr && !compareDeploymentConfigLoader ? ( |
| 179 | + <ErrorScreenManager code={errorConfig.code} reload={errorConfig.reload} /> |
| 180 | + ) : ( |
| 181 | + <div className="p-16 flexbox-col dc__gap-16 bcn-0 h-100"> |
| 182 | + {isLoading ? ( |
| 183 | + <Progressing fullHeight size={48} /> |
| 184 | + ) : ( |
| 185 | + <> |
| 186 | + <h3 className="fs-13 lh-20 fw-6 cn-9 m-0"> |
| 187 | + {hideDiffState |
| 188 | + ? 'Configurations used for this deployment trigger' |
| 189 | + : 'Showing configuration change with respect to previous deployment'} |
| 190 | + </h3> |
| 191 | + <div className="flexbox-col dc__gap-16 dc__mxw-800"> |
| 192 | + <div className="flexbox-col dc__gap-12"> |
| 193 | + {Object.keys(groupedDeploymentConfigList).map((groupHeader) => |
| 194 | + renderDeploymentHistoryConfig( |
| 195 | + groupedDeploymentConfigList[groupHeader], |
| 196 | + groupHeader !== 'UNGROUPED' ? groupHeader : null, |
| 197 | + pathname, |
| 198 | + hideDiffState, |
| 199 | + ), |
| 200 | + )} |
| 201 | + </div> |
| 202 | + {hasPreviousDeploymentConfigNotFoundError && ( |
| 203 | + <InfoColourBar |
| 204 | + classname="error_bar cn-9 fs-13 lh-20" |
| 205 | + Icon={ICError} |
| 206 | + message="Diff unavailable: Configurations for previous deployment not found." |
| 207 | + /> |
| 208 | + )} |
| 209 | + </div> |
| 210 | + </> |
| 211 | + )} |
| 212 | + </div> |
| 213 | + )} |
| 214 | + </Route> |
| 215 | + </Switch> |
| 216 | + ) |
| 217 | +} |
0 commit comments