Skip to content

Commit 5e5e993

Browse files
committed
refactor: DeploymentHistoryDiffView - review fixes - add memoization
1 parent 1229fd5 commit 5e5e993

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/Shared/Components/CICDHistory/DeploymentHistoryDiff/DeploymentHistoryDiffView.tsx

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

17-
import { Fragment, useEffect, useRef, useState } from 'react'
17+
import { Fragment, useEffect, useMemo, useRef, useState } from 'react'
1818
import { useParams } from 'react-router'
1919
import Tippy from '@tippyjs/react'
2020
import { yamlComparatorBySortOrder } from '@Shared/Helpers'
@@ -66,30 +66,38 @@ const DeploymentHistoryDiffView = ({
6666
Object.keys(baseTemplateConfiguration?.codeEditorValue?.variableSnapshot || {}).length !== 0 ||
6767
Object.keys(currentConfiguration?.codeEditorValue?.variableSnapshot || {}).length !== 0
6868

69-
const editorValuesRHS = convertVariables
70-
? baseTemplateConfiguration?.codeEditorValue?.resolvedValue
71-
: baseTemplateConfiguration?.codeEditorValue?.value
69+
const editorValuesRHS = useMemo(() => {
70+
if (!baseTemplateConfiguration?.codeEditorValue?.value || isDeleteDraft) {
71+
return ''
72+
}
73+
74+
const editorValue = convertVariables
75+
? baseTemplateConfiguration?.codeEditorValue?.resolvedValue
76+
: baseTemplateConfiguration?.codeEditorValue?.value
77+
78+
return YAMLStringify(JSON.parse(editorValue), {
79+
sortMapEntries: (a, b) => yamlComparatorBySortOrder(a, b, sortOrder),
80+
})
81+
}, [convertVariables, baseTemplateConfiguration, sortOrder, isDeleteDraft])
82+
83+
const editorValuesLHS = useMemo(() => {
84+
if (!currentConfiguration?.codeEditorValue?.value || isUnpublished) {
85+
return ''
86+
}
87+
88+
const editorValue = convertVariables
89+
? currentConfiguration?.codeEditorValue?.resolvedValue
90+
: currentConfiguration?.codeEditorValue?.value
7291

73-
const editorValuesLHS = convertVariables
74-
? currentConfiguration?.codeEditorValue?.resolvedValue
75-
: currentConfiguration?.codeEditorValue?.value
92+
return YAMLStringify(JSON.parse(editorValue), {
93+
sortMapEntries: (a, b) => yamlComparatorBySortOrder(a, b, sortOrder),
94+
})
95+
}, [convertVariables, currentConfiguration, sortOrder, isUnpublished])
7696

7797
const renderDeploymentDiffViaCodeEditor = () => (
7898
<CodeEditor
79-
value={
80-
!baseTemplateConfiguration?.codeEditorValue?.value || isDeleteDraft
81-
? ''
82-
: YAMLStringify(JSON.parse(editorValuesRHS), {
83-
sortMapEntries: (a, b) => yamlComparatorBySortOrder(a, b, sortOrder),
84-
})
85-
}
86-
defaultValue={
87-
!currentConfiguration?.codeEditorValue?.value || isUnpublished
88-
? ''
89-
: YAMLStringify(JSON.parse(editorValuesLHS), {
90-
sortMapEntries: (a, b) => yamlComparatorBySortOrder(a, b, sortOrder),
91-
})
92-
}
99+
value={editorValuesRHS}
100+
defaultValue={editorValuesLHS}
93101
height={codeEditorHeight}
94102
diffView={previousConfigAvailable && true}
95103
readOnly

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiffMain.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ export const DeploymentConfigDiffMain = ({
140140
<div className="px-12 py-6">{secondaryHeading}</div>
141141
</div>
142142
<DeploymentHistoryDiffView
143-
key={`${id}-${sortingConfig?.sortOrder || ''}`}
144143
baseTemplateConfiguration={secondaryList}
145144
currentConfiguration={primaryList}
146145
previousConfigAvailable

0 commit comments

Comments
 (0)