Skip to content

Commit c0b9705

Browse files
committed
feat: Remove DiffViewer component, use CodeEditor to render collapse unchanged readOnly diffView
1 parent d6268d7 commit c0b9705

File tree

12 files changed

+98
-311
lines changed

12 files changed

+98
-311
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { useParams } from 'react-router-dom'
1919
import Tippy from '@tippyjs/react'
2020

2121
import { CodeEditor } from '@Shared/Components/CodeEditor'
22-
import { renderDiffViewNoDifferenceState } from '@Shared/Components/DeploymentConfigDiff'
23-
import { DiffViewer } from '@Shared/Components/DiffViewer'
2422

2523
import { ReactComponent as Info } from '../../../../Assets/Icon/ic-info-filled.svg'
2624
import { ReactComponent as ViewVariablesIcon } from '../../../../Assets/Icon/ic-view-variable-toggle.svg'
@@ -71,10 +69,14 @@ const DeploymentHistoryDiffView = ({
7169

7270
const renderDeploymentDiffViaCodeEditor = () =>
7371
previousConfigAvailable ? (
74-
<DiffViewer
75-
oldValue={editorValuesLHS}
76-
newValue={editorValuesRHS}
77-
codeFoldMessageRenderer={renderDiffViewNoDifferenceState(editorValuesLHS, editorValuesRHS)}
72+
<CodeEditor
73+
height="100%"
74+
originalValue={editorValuesLHS}
75+
modifiedValue={editorValuesRHS}
76+
mode={MODES.YAML}
77+
noParsing
78+
diffView
79+
collapseUnchangedDiffView
7880
/>
7981
) : (
8082
<CodeEditor

src/Shared/Components/CodeEditor/CodeEditor.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ const CodeEditor = <DiffView extends boolean = false>({
7777
onChange,
7878
onOriginalValueChange,
7979
onModifiedValueChange,
80-
readOnly,
8180
placeholder,
8281
diffView,
8382
loading,
@@ -88,8 +87,13 @@ const CodeEditor = <DiffView extends boolean = false>({
8887
onBlur,
8988
onFocus,
9089
autoFocus,
91-
disableSearch = false,
90+
collapseUnchangedDiffView = false,
91+
...resProps
9292
}: CodeEditorProps<DiffView>) => {
93+
// DERIVED PROPS
94+
const disableSearch = (collapseUnchangedDiffView || resProps.disableSearch) ?? false
95+
const readOnly = (collapseUnchangedDiffView || resProps.readOnly) ?? false
96+
9397
// HOOKS
9498
const { appTheme } = useTheme()
9599

@@ -209,7 +213,7 @@ const CodeEditor = <DiffView extends boolean = false>({
209213
{ key: 'Escape', run: blurOnEscape, stopPropagation: true },
210214
]),
211215
indentationMarkers(),
212-
getLanguageExtension(mode),
216+
getLanguageExtension(mode, collapseUnchangedDiffView),
213217
foldingCompartment.of(foldConfig),
214218
lintGutter(),
215219
search({
@@ -277,6 +281,8 @@ const CodeEditor = <DiffView extends boolean = false>({
277281
modifiedViewExtensions={modifiedViewExtensions}
278282
extensions={extensions}
279283
diffMinimapExtensions={diffMinimapExtensions}
284+
collapseUnchanged={collapseUnchangedDiffView}
285+
disableMinimap={collapseUnchangedDiffView}
280286
/>
281287
</CodeEditorContext.Provider>
282288
)

src/Shared/Components/CodeEditor/CodeEditorRenderer.tsx

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export const CodeEditorRenderer = ({
4646
extensions,
4747
autoFocus,
4848
diffMinimapExtensions,
49+
collapseUnchanged = false,
50+
disableMinimap = false,
4951
}: CodeEditorRendererProps) => {
5052
// CONTEXTS
5153
const { value, lhsValue, diffMode } = useCodeEditorContext()
@@ -160,9 +162,11 @@ export const CodeEditorRenderer = ({
160162
handleLhsOnChange(val, vu)
161163
}
162164

163-
// Using `diffMinimapRef` instead of `diffMinimapInstance` since this extension captures the initial reference in a closure.
164-
// Changes to `diffMinimapInstance` won't be reflected after initialization, so we rely on `diffMinimapRef.current` for updates.
165-
updateDiffMinimapValues(diffMinimapRef.current, vu.transactions, 'a')
165+
if (!disableMinimap) {
166+
// Using `diffMinimapRef` instead of `diffMinimapInstance` since this extension captures the initial reference in a closure.
167+
// Changes to `diffMinimapInstance` won't be reflected after initialization, so we rely on `diffMinimapRef.current` for updates.
168+
updateDiffMinimapValues(diffMinimapRef.current, vu.transactions, 'a')
169+
}
166170
})
167171

168172
const modifiedUpdateListener = EditorView.updateListener.of((vu: ViewUpdate) => {
@@ -172,9 +176,11 @@ export const CodeEditorRenderer = ({
172176
handleOnChange(val, vu)
173177
}
174178

175-
// Using `diffMinimapRef` instead of `diffMinimapInstance` since this extension captures the initial reference in a closure.
176-
// Changes to `diffMinimapInstance` won't be reflected after initialization, so we rely on `diffMinimapRef.current` for updates.
177-
updateDiffMinimapValues(diffMinimapRef.current, vu.transactions, 'b')
179+
if (!disableMinimap) {
180+
// Using `diffMinimapRef` instead of `diffMinimapInstance` since this extension captures the initial reference in a closure.
181+
// Changes to `diffMinimapInstance` won't be reflected after initialization, so we rely on `diffMinimapRef.current` for updates.
182+
updateDiffMinimapValues(diffMinimapRef.current, vu.transactions, 'b')
183+
}
178184
})
179185

180186
useEffect(() => {
@@ -196,11 +202,12 @@ export const CodeEditorRenderer = ({
196202
...(!readOnly ? { revertControls: 'a-to-b', renderRevertControl: getRevertControlButton } : {}),
197203
diffConfig: { scanLimit, timeout: 5000 },
198204
parent: codeMirrorMergeParentRef.current,
205+
collapseUnchanged: collapseUnchanged ? {} : undefined,
199206
})
200207
setCodeMirrorMergeInstance(codeMirrorMergeView)
201208

202209
// MINIMAP INITIALIZATION
203-
if (codeMirrorMergeView && diffMinimapParentRef.current) {
210+
if (!disableMinimap && codeMirrorMergeView && diffMinimapParentRef.current) {
204211
diffMinimapInstance?.destroy()
205212
diffMinimapRef.current?.destroy()
206213

@@ -228,7 +235,7 @@ export const CodeEditorRenderer = ({
228235
setDiffMinimapInstance(null)
229236
diffMinimapRef.current = null
230237
}
231-
}, [loading, codemirrorMergeKey, diffMode])
238+
}, [loading, codemirrorMergeKey, diffMode, collapseUnchanged, disableMinimap])
232239

233240
// Sync external changes of `lhsValue` and `value` state to the diff-editor state.
234241
useEffect(() => {
@@ -251,14 +258,19 @@ export const CodeEditorRenderer = ({
251258

252259
// SCALING FACTOR UPDATER
253260
useEffect(() => {
254-
setTimeout(() => {
255-
setScalingFactor(
256-
codeMirrorMergeInstance
257-
? Math.min(codeMirrorMergeInstance.dom.clientHeight / codeMirrorMergeInstance.dom.scrollHeight, 1)
258-
: 1,
259-
)
260-
}, 100)
261-
}, [lhsValue, value, codeMirrorMergeInstance])
261+
if (!disableMinimap) {
262+
setTimeout(() => {
263+
setScalingFactor(
264+
codeMirrorMergeInstance
265+
? Math.min(
266+
codeMirrorMergeInstance.dom.clientHeight / codeMirrorMergeInstance.dom.scrollHeight,
267+
1,
268+
)
269+
: 1,
270+
)
271+
}, 100)
272+
}
273+
}, [lhsValue, value, codeMirrorMergeInstance, disableMinimap])
262274

263275
const { codeEditorClassName, codeEditorHeight, codeEditorParentClassName } = getCodeEditorHeight(height)
264276

@@ -286,12 +298,14 @@ export const CodeEditorRenderer = ({
286298
ref={codeMirrorMergeParentRef}
287299
className={`cm-merge-theme flex-grow-1 h-100 dc__overflow-hidden ${readOnly ? 'code-editor__read-only' : ''}`}
288300
/>
289-
<DiffMinimap
290-
theme={theme}
291-
view={codeMirrorMergeInstance}
292-
diffMinimapParentRef={diffMinimapParentRef}
293-
scalingFactor={scalingFactor}
294-
/>
301+
{!disableMinimap && (
302+
<DiffMinimap
303+
theme={theme}
304+
view={codeMirrorMergeInstance}
305+
diffMinimapParentRef={diffMinimapParentRef}
306+
scalingFactor={scalingFactor}
307+
/>
308+
)}
295309
</div>
296310
) : (
297311
<div

src/Shared/Components/CodeEditor/codeEditor.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@
244244
content: url("data:image/svg+xml,%3Csvg width='14' height='14' viewBox='0 0 14 14' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M6.24239 2.18666L1.43119 10.4987C1.3542 10.6317 1.3136 10.7827 1.31348 10.9364C1.31335 11.09 1.3537 11.241 1.43046 11.3742C1.50723 11.5073 1.6177 11.6179 1.75077 11.6947C1.88384 11.7716 2.0348 11.8121 2.18848 11.8121H11.8109C11.9646 11.8121 12.1155 11.7716 12.2486 11.6947C12.3817 11.6179 12.4921 11.5073 12.5689 11.3742C12.6457 11.241 12.686 11.09 12.6859 10.9364C12.6858 10.7827 12.6452 10.6317 12.5682 10.4987L7.75697 2.18666C7.68011 2.05387 7.56968 1.94363 7.43676 1.86699C7.30384 1.79034 7.15311 1.75 6.99968 1.75C6.84625 1.75 6.69551 1.79034 6.5626 1.86699C6.42968 1.94363 6.31925 2.05387 6.24239 2.18666Z' fill='%23F4BA63'/%3E%3Cpath d='M7.58333 5.68758C7.58333 5.36542 7.32217 5.10425 7 5.10425C6.67783 5.10425 6.41667 5.36542 6.41667 5.68758V7.87508C6.41667 8.19725 6.67783 8.45841 7 8.45841C7.32217 8.45841 7.58333 8.19725 7.58333 7.87508V5.68758Z' fill='%23000A14'/%3E%3Cpath d='M7.65625 9.84383C7.65625 10.2063 7.36244 10.5001 7 10.5001C6.63756 10.5001 6.34375 10.2063 6.34375 9.84383C6.34375 9.48139 6.63756 9.18758 7 9.18758C7.36244 9.18758 7.65625 9.48139 7.65625 9.84383Z' fill='%23000A14'/%3E%3C/svg%3E%0A");
245245
}
246246

247+
// COLLAPSED
248+
.cm-collapsedLines {
249+
padding: 6px 12px;
250+
background: var(--B50);
251+
font-size: 13px;
252+
line-height: 20px;
253+
color: var(--B500);
254+
255+
&::before,
256+
&::after {
257+
content: none;
258+
}
259+
}
260+
247261
// SEARCH
248262
.cm-searchMatch {
249263
background-color: var(--bg-matching-keyword);

src/Shared/Components/CodeEditor/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type CodeEditorDiffBaseProps = {
5151
originalValue?: ReactCodeMirrorProps['value']
5252
modifiedValue?: ReactCodeMirrorProps['value']
5353
isOriginalModifiable?: boolean
54+
/**
55+
* When true, renders a diff view in readOnly mode with collapsed unchanged diffs.
56+
* This disables the minimap, code-editor search functionality, and language linting.
57+
* @default false
58+
*/
59+
collapseUnchangedDiffView?: boolean
5460
}
5561

5662
type CodeEditorPropsBasedOnDiffView<DiffView extends boolean> = DiffView extends true
@@ -146,6 +152,8 @@ export type CodeEditorRendererProps = Required<
146152
modifiedViewExtensions: ReactCodeMirrorProps['extensions']
147153
extensions: ReactCodeMirrorProps['extensions']
148154
diffMinimapExtensions: ReactCodeMirrorProps['extensions']
155+
collapseUnchanged?: boolean
156+
disableMinimap?: boolean
149157
}
150158

151159
export interface DiffMinimapProps extends Pick<CodeEditorRendererProps, 'theme'> {

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import { ReactComponent as ICCheck } from '@Icons/ic-check.svg'
2020
import { ReactComponent as ICCheckCircleDots } from '@Icons/ic-check-circle-dots.svg'
2121
import { ReactComponent as ICEditFile } from '@Icons/ic-edit-file.svg'
2222
import { ReactComponent as ICFileCode } from '@Icons/ic-file-code.svg'
23-
import { deepEqual, noop, YAMLStringify } from '@Common/Helper'
23+
import { deepEqual, YAMLStringify } from '@Common/Helper'
2424
import {
2525
AppEnvDeploymentConfigListParams,
2626
DeploymentConfigDiffProps,
2727
DeploymentConfigDiffState,
2828
DeploymentHistoryDetail,
2929
DeploymentHistorySingleValue,
3030
DiffHeadingDataType,
31-
GenericSectionErrorState,
3231
prepareHistoryData,
3332
} from '@Shared/Components'
3433
import { DEPLOYMENT_HISTORY_CONFIGURATION_LIST_MAP } from '@Shared/constants'
@@ -47,7 +46,6 @@ import {
4746
TemplateListDTO,
4847
TemplateListType,
4948
} from '../../Services/app.types'
50-
import { DiffViewerProps } from '../DiffViewer/types'
5149

5250
export const getDeploymentTemplateData = (data: DeploymentTemplateDTO) => {
5351
const parsedDraftData = JSON.parse(data?.deploymentDraftData?.configData[0].draftMetadata.data || null)
@@ -911,21 +909,3 @@ export const getDefaultVersionAndPreviousDeploymentOptions = (data: TemplateList
911909
previousDeployments: [],
912910
},
913911
)
914-
915-
export const renderDiffViewNoDifferenceState = (
916-
lhsValue: string,
917-
rhsValue: string,
918-
): DiffViewerProps['codeFoldMessageRenderer'] =>
919-
lhsValue === rhsValue
920-
? () => (
921-
<GenericSectionErrorState
922-
useInfoIcon
923-
title="No diff in configurations"
924-
subTitle=""
925-
description=""
926-
buttonText="View values"
927-
// Click event is handled at the parent level
928-
reload={noop}
929-
/>
930-
)
931-
: null

src/Shared/Components/DeploymentConfigDiff/DeploymentConfigDiffMain.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import { Fragment, useEffect, useState } from 'react'
1818

1919
import { ReactComponent as ICSort } from '@Icons/ic-arrow-up-down.svg'
2020
import { ReactComponent as ICSortArrowDown } from '@Icons/ic-sort-arrow-down.svg'
21-
import { SortingOrder } from '@Common/Constants'
21+
import { MODES, SortingOrder } from '@Common/Constants'
2222
import ErrorScreenManager from '@Common/ErrorScreenManager'
2323
import { Progressing } from '@Common/Progressing'
24-
import { DiffViewer } from '@Shared/Components/DiffViewer'
2524
import { ComponentSizeType } from '@Shared/constants'
2625

2726
import { Button, ButtonStyleType, ButtonVariantType } from '../Button'
2827
import { DeploymentHistoryDiffView } from '../CICDHistory'
28+
import { CodeEditor } from '../CodeEditor'
2929
import { SelectPicker } from '../SelectPicker'
3030
import { ToggleResolveScopedVariables } from '../ToggleResolveScopedVariables'
3131
import {
@@ -34,7 +34,6 @@ import {
3434
DeploymentConfigDiffSelectPickerProps,
3535
DeploymentConfigDiffState,
3636
} from './DeploymentConfigDiff.types'
37-
import { renderDiffViewNoDifferenceState } from './DeploymentConfigDiff.utils'
3837
import { DeploymentConfigDiffAccordion } from './DeploymentConfigDiffAccordion'
3938

4039
export const DeploymentConfigDiffMain = ({
@@ -191,19 +190,25 @@ export const DeploymentConfigDiffMain = ({
191190
hideDiffState={hideDiffState}
192191
>
193192
{singleView ? (
194-
<DiffViewer
195-
oldValue={primaryList.codeEditorValue.value}
196-
newValue={secondaryList.codeEditorValue.value}
197-
// Need to hide the title since the null state is controlled explicitly
198-
{...(primaryList.codeEditorValue.value !== secondaryList.codeEditorValue.value && {
199-
leftTitle: primaryHeading,
200-
rightTitle: secondaryHeading,
201-
})}
202-
codeFoldMessageRenderer={renderDiffViewNoDifferenceState(
203-
primaryList.codeEditorValue.value,
204-
secondaryList.codeEditorValue.value,
205-
)}
206-
/>
193+
<>
194+
<div className="dc__grid-half vertical-divider">
195+
<div className="bcn-1 py-8 px-12">
196+
<p className="m-0 fs-12 lh-20 fw-6">{primaryHeading}</p>
197+
</div>
198+
<div className="bcn-1 py-8 px-12">
199+
<p className="m-0 fs-12 lh-20 fw-6">{secondaryHeading}</p>
200+
</div>
201+
</div>
202+
<CodeEditor
203+
originalValue={primaryList.codeEditorValue.value}
204+
modifiedValue={secondaryList.codeEditorValue.value}
205+
height="100%"
206+
mode={MODES.YAML}
207+
noParsing
208+
diffView
209+
collapseUnchangedDiffView
210+
/>
211+
</>
207212
) : (
208213
<div className="p-16">
209214
{primaryHeading && secondaryHeading && (
@@ -245,7 +250,7 @@ export const DeploymentConfigDiffMain = ({
245250
}
246251

247252
return (
248-
<div className="bg__primary deployment-config-diff__main-top flexbox-col min-h-100">
253+
<div className="bg__primary deployment-config-diff__main-top flexbox-col min-h-100 mw-none">
249254
<div className="dc__border-bottom-n1 flexbox dc__align-items-center dc__position-sticky dc__top-0 bg__primary w-100 dc__zi-11">
250255
<div className="flexbox dc__align-items-center p-12 dc__gap-8 deployment-config-diff__main-top__header">
251256
{!!headerText && <p className="m-0 cn-9 fs-13 lh-20">{headerText}</p>}

0 commit comments

Comments
 (0)