Skip to content

Commit cb4a62a

Browse files
committed
feat: DeploymentConfigDiff - Scope Variables Support & code refactor
1 parent 97aab9d commit cb4a62a

File tree

7 files changed

+194
-40
lines changed

7 files changed

+194
-40
lines changed

src/Common/Helper.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,19 @@ export function asyncWrap(promise): any[] {
10471047
}
10481048

10491049
export const prefixZeroIfSingleDigit = (value: number = 0) => (value > 0 && value < 10 ? `0${value}` : value)
1050+
1051+
export const throttle = <T extends (...args: unknown[]) => unknown>(
1052+
func: T,
1053+
delay: number = 300,
1054+
): ((...args: Parameters<T>) => void) => {
1055+
let lastCall = 0
1056+
1057+
return (...args: Parameters<T>) => {
1058+
const now = Date.now()
1059+
1060+
if (now - lastCall >= delay) {
1061+
lastCall = now
1062+
func(...args)
1063+
}
1064+
}
1065+
}

src/Common/Toggle/Toggle.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React from 'react'
18-
import { useEffectAfterMount } from '../Helper'
17+
import React, { useCallback } from 'react'
18+
import { throttle, useEffectAfterMount } from '../Helper'
1919
import './Toggle.scss'
2020

2121
const Toggle = ({
@@ -27,6 +27,7 @@ const Toggle = ({
2727
dataTestId = 'handle-toggle-button',
2828
Icon = null,
2929
iconClass = '',
30+
throttleOnChange = false,
3031
...props
3132
}) => {
3233
const [active, setActive] = React.useState(selected)
@@ -49,13 +50,20 @@ const Toggle = ({
4950
}
5051
}
5152

53+
const throttledHandleClick = useCallback(throttle(handleClick, 500), [])
54+
5255
return (
5356
<label
5457
{...props}
5558
className={`${rootClassName} toggle__switch ${disabled ? 'disabled' : ''}`}
5659
style={{ ['--color' as any]: color }}
5760
>
58-
<input type="checkbox" checked={!!active} onChange={handleClick} className="toggle__input" />
61+
<input
62+
type="checkbox"
63+
checked={!!active}
64+
onChange={throttleOnChange ? throttledHandleClick : handleClick}
65+
className="toggle__input"
66+
/>
5967
<span className={`toggle__slider ${Icon ? 'with-icon br-4 dc__border' : 'round'}`} data-testid={dataTestId}>
6068
{Icon && <Icon className={`icon-dim-20 br-4 p-2 ${iconClass}`} />}
6169
</span>

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiff.types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export interface DeploymentConfigDiffProps {
8080
activeTab: string
8181
onClick: (tab: string) => void
8282
}
83+
scopeVariablesConfig?: {
84+
convertVariables: boolean
85+
onConvertVariablesClick: () => void
86+
}
8387
}
8488

8589
export interface DeploymentConfigDiffNavigationProps
@@ -98,6 +102,7 @@ export interface DeploymentConfigDiffMainProps
98102
| 'scrollIntoViewId'
99103
| 'selectorsConfig'
100104
| 'sortingConfig'
105+
| 'scopeVariablesConfig'
101106
> {}
102107

103108
export interface DeploymentConfigDiffAccordionProps extends Pick<CollapseProps, 'onTransitionEnd'> {
@@ -126,4 +131,7 @@ export type AppEnvDeploymentConfigListParams<IsManifestView> = (IsManifestView e
126131
}) & {
127132
getNavItemHref: (resourceType: EnvResourceType, resourceName: string) => string
128133
isManifestView?: IsManifestView
134+
convertVariables?: boolean
135+
currentDeploymentTemplateResolvedData?: AppEnvDeploymentConfigDTO
136+
compareDeploymentTemplateResolvedData?: AppEnvDeploymentConfigDTO
129137
}

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiff.utils.tsx

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,46 @@ const getDiffHeading = <DeploymentTemplate extends boolean>(
367367
)
368368
}
369369

370+
const getConfigMapSecretResolvedValues = (configMapSecretData: ConfigMapSecretDataDTO, convertVariables: boolean) => {
371+
const resolvedData: ConfigMapSecretDataConfigDatumDTO[] =
372+
((convertVariables && JSON.parse(configMapSecretData?.resolvedValue || null)) || configMapSecretData?.data)
373+
?.configData || []
374+
375+
const data =
376+
(convertVariables &&
377+
resolvedData.map((item, index) => {
378+
if (configMapSecretData.data.configData[index].draftMetadata) {
379+
const resolvedDraftData =
380+
configMapSecretData.data.configData[index].draftMetadata.draftResolvedValue ||
381+
configMapSecretData.data.configData[index].draftMetadata.data
382+
383+
return {
384+
...configMapSecretData.data.configData[index],
385+
draftMetadata: {
386+
...configMapSecretData.data.configData[index].draftMetadata,
387+
data: resolvedDraftData,
388+
},
389+
}
390+
}
391+
392+
return item
393+
})) ||
394+
resolvedData
395+
396+
return data
397+
}
398+
370399
const getConfigMapSecretData = (
371400
compareToList: ConfigMapSecretDataDTO,
372401
compareWithList: ConfigMapSecretDataDTO,
373402
resourceType: ConfigResourceType,
374403
compareToIsAdmin: boolean,
375404
compareWithIsAdmin: boolean,
405+
convertVariables: boolean,
376406
) => {
377407
const combinedList = mergeConfigDataArraysByName(
378-
compareToList?.data.configData || [],
379-
compareWithList?.data.configData || [],
408+
getConfigMapSecretResolvedValues(compareToList, convertVariables),
409+
getConfigMapSecretResolvedValues(compareWithList, convertVariables),
380410
)
381411

382412
const deploymentConfig = combinedList.map(([currentItem, compareItem]) => {
@@ -407,6 +437,21 @@ const getConfigMapSecretData = (
407437
return deploymentConfig
408438
}
409439

440+
const getConfigDataWithResolvedDeploymentTemplate = (
441+
data: AppEnvDeploymentConfigListParams<false>['currentList'],
442+
resolvedData: AppEnvDeploymentConfigListParams<false>['currentDeploymentTemplateResolvedData'],
443+
) =>
444+
data && resolvedData?.deploymentTemplate
445+
? {
446+
...data,
447+
deploymentTemplate: {
448+
...data.deploymentTemplate,
449+
deploymentDraftData: null,
450+
data: resolvedData.deploymentTemplate.resolvedValue,
451+
},
452+
}
453+
: data
454+
410455
/**
411456
* Generates a list of deployment configurations for application environments and identifies changes between the current and compare lists.
412457
*
@@ -425,14 +470,23 @@ export const getAppEnvDeploymentConfigList = <ManifestView extends boolean = fal
425470
getNavItemHref,
426471
isManifestView,
427472
sortOrder,
473+
convertVariables = false,
474+
currentDeploymentTemplateResolvedData,
475+
compareDeploymentTemplateResolvedData,
428476
}: AppEnvDeploymentConfigListParams<ManifestView>): {
429477
configList: DeploymentConfigDiffProps['configList']
430478
navList: DeploymentConfigDiffProps['navList']
431479
collapsibleNavList: DeploymentConfigDiffProps['collapsibleNavList']
432480
} => {
433481
if (!isManifestView) {
434-
const _currentList = currentList as AppEnvDeploymentConfigListParams<false>['currentList']
435-
const _compareList = compareList as AppEnvDeploymentConfigListParams<false>['compareList']
482+
const _currentList = getConfigDataWithResolvedDeploymentTemplate(
483+
currentList as AppEnvDeploymentConfigListParams<false>['currentList'],
484+
currentDeploymentTemplateResolvedData,
485+
)
486+
const _compareList = getConfigDataWithResolvedDeploymentTemplate(
487+
compareList as AppEnvDeploymentConfigListParams<false>['compareList'],
488+
compareDeploymentTemplateResolvedData,
489+
)
436490
const currentDeploymentData = getDeploymentTemplateDiffViewData(_currentList.deploymentTemplate, sortOrder)
437491
const compareDeploymentData = getDeploymentTemplateDiffViewData(_compareList.deploymentTemplate, sortOrder)
438492

@@ -457,6 +511,7 @@ export const getAppEnvDeploymentConfigList = <ManifestView extends boolean = fal
457511
ConfigResourceType.ConfigMap,
458512
_currentList.isAppAdmin,
459513
_compareList.isAppAdmin,
514+
convertVariables,
460515
)
461516

462517
const secretData = getConfigMapSecretData(
@@ -465,6 +520,7 @@ export const getAppEnvDeploymentConfigList = <ManifestView extends boolean = fal
465520
ConfigResourceType.Secret,
466521
_currentList.isAppAdmin,
467522
_compareList.isAppAdmin,
523+
convertVariables,
468524
)
469525

470526
const configList: DeploymentConfigDiffProps['configList'] = [deploymentTemplateData, ...cmData, ...secretData]

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiffMain.tsx

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { Fragment, TransitionEvent, useEffect, useState } from 'react'
2+
import Tippy from '@tippyjs/react'
23

34
import { ReactComponent as ICSortArrowDown } from '@Icons/ic-sort-arrow-down.svg'
45
import { ReactComponent as ICSort } from '@Icons/ic-arrow-up-down.svg'
6+
import { ReactComponent as ICViewVariableToggle } from '@Icons/ic-view-variable-toggle.svg'
57
import { Progressing } from '@Common/Progressing'
68
import { CodeEditor } from '@Common/CodeEditor'
79
import { MODES, SortingOrder } from '@Common/Constants'
810
import ErrorScreenManager from '@Common/ErrorScreenManager'
11+
import Toggle from '@Common/Toggle/Toggle'
12+
import { ComponentSizeType } from '@Shared/constants'
913

14+
import { Button, ButtonStyleType, ButtonVariantType } from '../Button'
1015
import { SelectPicker } from '../SelectPicker'
1116
import { DeploymentHistoryDiffView } from '../CICDHistory'
1217
import { DeploymentConfigDiffAccordion } from './DeploymentConfigDiffAccordion'
@@ -20,6 +25,7 @@ export const DeploymentConfigDiffMain = ({
2025
selectorsConfig,
2126
sortingConfig,
2227
scrollIntoViewId,
28+
scopeVariablesConfig,
2329
}: DeploymentConfigDiffMainProps) => {
2430
// STATES
2531
const [expandedView, setExpandedView] = useState<Record<string | number, boolean>>({})
@@ -94,26 +100,55 @@ export const DeploymentConfigDiffMain = ({
94100
const { handleSorting, sortBy, sortOrder } = sortingConfig
95101

96102
return (
97-
<div className="dc__border-left p-12 h-100">
98-
<button
99-
type="button"
100-
className={`dc__unset-button-styles flexbox dc__align-items-center dc__gap-6 ${isLoading ? 'dc__disabled' : ''}`}
101-
onClick={handleSorting}
102-
disabled={isLoading}
103-
>
104-
{sortBy ? (
103+
<Button
104+
dataTestId="config-diff-sort-button"
105+
text="Sort keys"
106+
variant={ButtonVariantType.borderLess}
107+
style={ButtonStyleType.neutral}
108+
size={ComponentSizeType.small}
109+
startIcon={
110+
sortBy ? (
105111
<ICSortArrowDown
106-
className="fcn-7 rotate"
112+
className="rotate"
107113
style={{
108114
['--rotateBy' as string]: sortOrder === SortingOrder.ASC ? '0deg' : '180deg',
109115
}}
110116
/>
111117
) : (
112-
<ICSort className="icon-dim-12 mw-12 scn-7" />
113-
)}
114-
<span className="cn-7 fs-13 lh-20 fw-6">Sort keys</span>
115-
</button>
116-
</div>
118+
<ICSort />
119+
)
120+
}
121+
onClick={handleSorting}
122+
disabled={isLoading}
123+
/>
124+
)
125+
}
126+
127+
return null
128+
}
129+
130+
const renderScopeVariablesButton = () => {
131+
if (scopeVariablesConfig) {
132+
const { convertVariables } = scopeVariablesConfig
133+
134+
return (
135+
<Tippy
136+
content={convertVariables ? 'Hide variables values' : 'Show variables values'}
137+
placement="bottom-start"
138+
animation="shift-away"
139+
className="default-tt"
140+
arrow={false}
141+
>
142+
<div className="w-40 h-20">
143+
<Toggle
144+
selected={scopeVariablesConfig.convertVariables}
145+
color="var(--V500)"
146+
onSelect={scopeVariablesConfig.onConvertVariablesClick}
147+
Icon={ICViewVariableToggle}
148+
throttleOnChange
149+
/>
150+
</div>
151+
</Tippy>
117152
)
118153
}
119154

@@ -142,7 +177,7 @@ export const DeploymentConfigDiffMain = ({
142177
<div className="px-12 py-6">{secondaryHeading}</div>
143178
</div>
144179
<CodeEditor
145-
key={sortingConfig?.sortOrder}
180+
key={`${sortingConfig?.sortOrder}-${scopeVariablesConfig?.convertVariables}`}
146181
diffView
147182
defaultValue={primaryList.codeEditorValue.value}
148183
value={secondaryList.codeEditorValue.value}
@@ -184,7 +219,12 @@ export const DeploymentConfigDiffMain = ({
184219
<div className="flex-grow-1 flexbox dc__align-items-center dc__gap-8 p-12">
185220
{renderHeaderSelectors(selectorsConfig.secondaryConfig)}
186221
</div>
187-
{renderSortButton()}
222+
{(sortingConfig || scopeVariablesConfig) && (
223+
<div className="dc__border-left flex dc__gap-8 pr-12 pl-8 py-8">
224+
{renderSortButton()}
225+
{renderScopeVariablesButton()}
226+
</div>
227+
)}
188228
</div>
189229
</div>
190230
<div className="deployment-config-diff__main-content dc__overflow-y-auto">

src/Shared/Services/app.service.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ROUTES, ResponseType, get, getUrlWithSearchParams, showError } from '../../Common'
17+
import { ROUTES, ResponseType, get, getUrlWithSearchParams, post, showError } from '../../Common'
1818
import {
1919
CIMaterialInfoDTO,
2020
CIMaterialInfoType,
@@ -55,8 +55,13 @@ export const getArtifactInfo = async (
5555
}
5656
}
5757

58-
export const getAppEnvDeploymentConfig = (
59-
params: AppEnvDeploymentConfigPayloadType,
60-
signal?: AbortSignal,
61-
): Promise<ResponseType<AppEnvDeploymentConfigDTO>> =>
62-
get(getUrlWithSearchParams(ROUTES.CONFIG_DATA, params), { signal })
58+
export const getAppEnvDeploymentConfig = ({
59+
params,
60+
payload,
61+
signal,
62+
}: {
63+
params: AppEnvDeploymentConfigPayloadType
64+
payload?: { values: string }
65+
signal?: AbortSignal
66+
}): Promise<ResponseType<AppEnvDeploymentConfigDTO>> =>
67+
post(getUrlWithSearchParams(ROUTES.CONFIG_DATA, params), payload, { signal })

0 commit comments

Comments
 (0)