Skip to content

Commit 7d4bf5b

Browse files
authored
Merge pull request #486 from devtron-labs/fix/rb-compare-sync
feat: add VirtualizedList component
2 parents db49271 + 20cec7a commit 7d4bf5b

File tree

16 files changed

+230
-32
lines changed

16 files changed

+230
-32
lines changed

package-lock.json

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.4.3",
3+
"version": "1.4.4",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",
@@ -106,6 +106,7 @@
106106
"react-dates": "^21.8.0",
107107
"react-diff-viewer-continued": "^3.4.0",
108108
"react-monaco-editor": "^0.54.0",
109+
"react-virtualized-sticky-tree": "^3.0.0-beta18",
109110
"sass": "^1.69.7",
110111
"tslib": "2.7.0"
111112
},
@@ -120,6 +121,10 @@
120121
},
121122
"vite-plugin-svgr": {
122123
"vite": "5.4.11"
124+
},
125+
"react-virtualized-sticky-tree": {
126+
"react": "^17.0.2",
127+
"react-dom": "^17.0.2"
123128
}
124129
}
125130
}

src/Common/CodeEditor/CodeEditor.reducer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const CodeEditorReducer = (state: CodeEditorState, action: Action) => {
2929
return { ...state, theme: action.value }
3030
case 'setCode':
3131
return { ...state, code: action.value }
32+
case 'setDefaultCode':
33+
return { ...state, defaultCode: action.value }
3234
case 'setHeight':
3335
return { ...state, height: action.value.toString() }
3436
default:
@@ -71,13 +73,15 @@ export const initialState = ({
7173
mode,
7274
theme,
7375
value,
76+
defaultValue,
7477
diffView,
7578
noParsing,
7679
tabSize,
7780
}: CodeEditorInitialValueType): CodeEditorState => ({
7881
mode: mode as MODES,
7982
theme: (theme || CodeEditorThemesKeys.vs) as CodeEditorThemesKeys,
8083
code: noParsing ? value : parseValueToCode(value, mode, tabSize),
84+
defaultCode: noParsing ? defaultValue : parseValueToCode(defaultValue, mode, tabSize),
8185
diffMode: diffView,
8286
noParsing: [MODES.JSON, MODES.YAML].includes(mode as MODES) ? noParsing : true,
8387
})

src/Common/CodeEditor/CodeEditor.tsx

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import React, { useEffect, useMemo, useReducer, useRef, useState } from 'react'
18-
import MonacoEditor, { MonacoDiffEditor } from 'react-monaco-editor'
18+
import MonacoEditor, { ChangeHandler, DiffEditorDidMount, EditorDidMount, MonacoDiffEditor } from 'react-monaco-editor'
1919
import ReactGA from 'react-ga4'
2020
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
2121
import { configureMonacoYaml } from 'monaco-yaml'
@@ -94,10 +94,10 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
9494
const memoisedReducer = React.useCallback(CodeEditorReducer, [])
9595
const [state, dispatch] = useReducer(
9696
memoisedReducer,
97-
initialState({ mode, theme, value, diffView, noParsing, tabSize }),
97+
initialState({ mode, theme, value, defaultValue, diffView, noParsing, tabSize }),
9898
)
9999
const [, json, yamlCode, error] = useJsonYaml(state.code, tabSize, state.mode, !state.noParsing)
100-
const [, originalJson, originlaYaml] = useJsonYaml(defaultValue, tabSize, state.mode, !state.noParsing)
100+
const [, originalJson, originalYaml] = useJsonYaml(state.defaultCode, tabSize, state.mode, !state.noParsing)
101101
const [contentHeight, setContentHeight] = useState(
102102
adjustEditorHeightToContent ? INITIAL_HEIGHT_WHEN_DYNAMIC_HEIGHT : height,
103103
)
@@ -160,7 +160,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
160160
}
161161
}, [disableSearch])
162162

163-
const editorDidMount = (editor, monaco) => {
163+
const editorDidMount: EditorDidMount = (editor) => {
164164
if (
165165
mode === MODES.YAML &&
166166
editor &&
@@ -181,15 +181,21 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
181181
}
182182

183183
if (adjustEditorHeightToContent && editor) {
184-
if (!('getModifiedEditor' in editor)) {
185-
editor.onDidContentSizeChange(() => {
186-
setContentHeight(editor.getContentHeight())
187-
})
184+
editor.onDidContentSizeChange(() => {
188185
setContentHeight(editor.getContentHeight())
189-
return
190-
}
191-
const modifiedEditor = editor.getModifiedEditor()
192-
const originalEditor = editor.getOriginalEditor()
186+
})
187+
setContentHeight(editor.getContentHeight())
188+
}
189+
190+
editorRef.current = editor
191+
monacoRef.current = monaco
192+
}
193+
194+
const diffEditorDidMount: DiffEditorDidMount = (editor, monaco) => {
195+
const originalEditor = editor.getOriginalEditor()
196+
const modifiedEditor = editor.getModifiedEditor()
197+
198+
if (adjustEditorHeightToContent) {
193199
originalEditor.onDidContentSizeChange(() => {
194200
setContentHeight(
195201
Math.max(
@@ -198,6 +204,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
198204
),
199205
)
200206
})
207+
201208
modifiedEditor.onDidContentSizeChange(() => {
202209
setContentHeight(
203210
Math.max(
@@ -206,9 +213,14 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
206213
),
207214
)
208215
})
209-
setContentHeight(Math.max(modifiedEditor.getContentHeight(), modifiedEditor.getContentHeight()))
216+
217+
setContentHeight(Math.max(originalEditor.getContentHeight(), modifiedEditor.getContentHeight()))
210218
}
211219

220+
originalEditor.onDidChangeModelContent(() => {
221+
codeEditorOnChange(modifiedEditor.getValue(), originalEditor.getValue())
222+
})
223+
212224
editorRef.current = editor
213225
monacoRef.current = monaco
214226
}
@@ -254,14 +266,15 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
254266
editorRef.current.layout()
255267
}, [width, windowHeight])
256268

257-
const setCode = (value: string) => {
269+
const setCode = (value: string, originalValue: string) => {
258270
dispatch({ type: 'setCode', value })
259-
onChangeRef.current?.(value)
271+
dispatch({ type: 'setDefaultCode', value: originalValue })
272+
onChangeRef.current?.(value, originalValue)
260273
}
261274

262275
useEffectAfterMount(() => {
263276
if (noParsing) {
264-
setCode(value)
277+
setCode(value, defaultValue)
265278

266279
return
267280
}
@@ -270,8 +283,8 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
270283
return
271284
}
272285

273-
setCode(parseValueToCode(value, state.mode, tabSize))
274-
}, [value, noParsing])
286+
setCode(parseValueToCode(value, state.mode, tabSize), parseValueToCode(defaultValue, state.mode, tabSize))
287+
}, [value, defaultValue, noParsing])
275288

276289
useEffect(() => {
277290
dispatch({ type: 'setDiff', value: diffView })
@@ -283,13 +296,17 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
283296
}
284297
}, [focus])
285298

286-
function handleOnChange(newValue, e) {
287-
setCode(newValue)
299+
const codeEditorOnChange = (newValue: string, newOriginalValue: string) => {
300+
setCode(newValue, newOriginalValue)
301+
}
302+
303+
const handleOnChange: ChangeHandler = (newValue) => {
304+
codeEditorOnChange(newValue, editorRef.current?.getOriginalEditor?.().getValue?.() ?? '')
288305
}
289306

290307
function handleLanguageChange(mode: 'json' | 'yaml') {
291308
dispatch({ type: 'changeLanguage', value: mode })
292-
setCode(mode === 'json' ? json : yamlCode)
309+
setCode(mode === 'json' ? json : yamlCode, mode === 'json' ? originalJson : originalYaml)
293310
}
294311

295312
const options: monaco.editor.IEditorConstructionOptions = {
@@ -325,6 +342,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
325342

326343
const diffViewOptions: monaco.editor.IDiffEditorConstructionOptions = {
327344
...options,
345+
originalEditable: !readOnly,
328346
useInlineViewWhenSpaceIsLimited: false,
329347
}
330348

@@ -340,15 +358,13 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
340358
{shebang && <div className="code-editor__shebang">{shebang}</div>}
341359
{state.diffMode ? (
342360
<MonacoDiffEditor
343-
original={
344-
noParsing ? defaultValue : state.mode === 'json' ? originalJson : originlaYaml
345-
}
361+
original={state.defaultCode}
346362
value={state.code}
347363
language={state.mode}
348364
onChange={handleOnChange}
349365
options={diffViewOptions}
350366
theme={state.theme.toLowerCase().split(' ').join('-')}
351-
editorDidMount={editorDidMount}
367+
editorDidMount={diffEditorDidMount}
352368
height={editorHeight}
353369
width="100%"
354370
/>

src/Common/CodeEditor/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface CodeEditorBaseInterface {
2626
value?: string
2727
lineDecorationsWidth?: number
2828
responseType?: string
29-
onChange?: (string) => void
29+
onChange?: (value: string, defaultValue: string) => void
3030
onBlur?: () => void
3131
onFocus?: () => void
3232
children?: any
@@ -88,7 +88,7 @@ export interface CodeEditorHeaderComposition {
8888
Clipboard?: React.FC<any>
8989
}
9090

91-
export type ActionTypes = 'changeLanguage' | 'setDiff' | 'setTheme' | 'setCode' | 'setHeight'
91+
export type ActionTypes = 'changeLanguage' | 'setDiff' | 'setTheme' | 'setCode' | 'setDefaultCode' | 'setHeight'
9292

9393
export interface Action {
9494
type: ActionTypes
@@ -108,6 +108,7 @@ export interface CodeEditorInitialValueType {
108108
diffView: boolean
109109
theme?: string
110110
value: string
111+
defaultValue: string
111112
noParsing?: boolean
112113
tabSize: number
113114
}
@@ -117,6 +118,7 @@ export interface CodeEditorState {
117118
diffMode: boolean
118119
theme: CodeEditorThemesKeys
119120
code: string
121+
defaultCode: string
120122
noParsing: boolean
121123
}
122124

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const ROUTES = {
117117
ATTRIBUTES_UPDATE: 'attributes/update',
118118
APP_LIST_MIN: 'app/min',
119119
CLUSTER_LIST_MIN: 'cluster/autocomplete',
120+
CLUSTER_LIST_RAW: 'k8s/capacity/cluster/list/raw',
120121
PLUGIN_GLOBAL_LIST_DETAIL_V2: 'plugin/global/list/detail/v2',
121122
PLUGIN_GLOBAL_LIST_V2: 'plugin/global/list/v2',
122123
PLUGIN_GLOBAL_LIST_TAGS: 'plugin/global/list/tags',

src/Pages/ResourceBrowser/service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { post } from '@Common/Api'
1+
import { get, post } from '@Common/Api'
22
import { ROUTES } from '@Common/Constants'
33
import { ResponseType } from '@Common/Types'
44
import {
@@ -7,6 +7,7 @@ import {
77
K8sResourceDetailType,
88
K8sResourceListPayloadType,
99
} from './ResourceBrowser.Types'
10+
import { ClusterDetail } from './types'
1011

1112
export const getK8sResourceList = (
1213
resourceListPayload: K8sResourceListPayloadType,
@@ -19,3 +20,5 @@ export const getK8sResourceList = (
1920
export const createNewResource = (
2021
resourceListPayload: CreateResourcePayload,
2122
): Promise<ResponseType<CreateResourceDTO[]>> => post(ROUTES.K8S_RESOURCE_CREATE, resourceListPayload)
23+
24+
export const getClusterListRaw = () => get<ClusterDetail[]>(ROUTES.CLUSTER_LIST_RAW)

0 commit comments

Comments
 (0)