Skip to content

Commit 46e17ff

Browse files
authored
Merge pull request #393 from devtron-labs/feat/rjsf-field-selector
fix: use useEffectAfterMount to setCode instead of useEffect
2 parents e04a58e + fa4f2f5 commit 46e17ff

File tree

14 files changed

+86
-27
lines changed

14 files changed

+86
-27
lines changed

package-lock.json

Lines changed: 2 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: 1 addition & 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.0.3-beta-7",
3+
"version": "1.0.3-beta-8",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/CodeEditor/CodeEditor.reducer.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import YAML from 'yaml'
18+
import { noop, YAMLStringify } from '@Common/Helper'
1719
import { MODES } from '../Constants'
1820
import { Action, CodeEditorInitialValueType, CodeEditorState, CodeEditorThemesKeys } from './types'
1921

@@ -34,16 +36,39 @@ export const CodeEditorReducer = (state: CodeEditorState, action: Action) => {
3436
}
3537
}
3638

39+
export const parseValueToCode = (value: string, mode: string, tabSize: number) => {
40+
let obj = null
41+
42+
try {
43+
obj = JSON.parse(value)
44+
} catch {
45+
try {
46+
obj = YAML.parse(value)
47+
} catch {
48+
noop()
49+
}
50+
}
51+
52+
let final = value
53+
54+
if (obj) {
55+
final = mode === MODES.JSON ? JSON.stringify(obj, null, tabSize) : YAMLStringify(obj)
56+
}
57+
58+
return final
59+
}
60+
3761
export const initialState = ({
3862
mode,
3963
theme,
4064
value,
4165
diffView,
4266
noParsing,
67+
tabSize,
4368
}: CodeEditorInitialValueType): CodeEditorState => ({
4469
mode: mode as MODES,
4570
theme: (theme || CodeEditorThemesKeys.vs) as CodeEditorThemesKeys,
46-
code: value,
71+
code: parseValueToCode(value, mode, tabSize),
4772
diffMode: diffView,
4873
noParsing: [MODES.JSON, MODES.YAML].includes(mode as MODES) ? noParsing : true,
4974
})

src/Common/CodeEditor/CodeEditor.tsx

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import React, { useEffect, useMemo, useReducer, useRef, useState } from 'react'
1818
import MonacoEditor, { MonacoDiffEditor } from 'react-monaco-editor'
19-
import YAML from 'yaml'
2019
import ReactGA from 'react-ga4'
2120
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
2221
import { configureMonacoYaml } from 'monaco-yaml'
@@ -27,7 +26,7 @@ import { ReactComponent as ErrorIcon } from '../../Assets/Icon/ic-error-exclamat
2726
import './codeEditor.scss'
2827
import 'monaco-editor'
2928

30-
import { YAMLStringify, cleanKubeManifest, useJsonYaml } from '../Helper'
29+
import { cleanKubeManifest, useEffectAfterMount, useJsonYaml } from '../Helper'
3130
import { useWindowSize } from '../Hooks'
3231
import Select from '../Select/Select'
3332
import RadioGroup from '../RadioGroup/RadioGroup'
@@ -41,7 +40,7 @@ import {
4140
CodeEditorThemesKeys,
4241
InformationBarProps,
4342
} from './types'
44-
import { CodeEditorReducer, initialState } from './CodeEditor.reducer'
43+
import { CodeEditorReducer, initialState, parseValueToCode } from './CodeEditor.reducer'
4544
import { DEFAULT_JSON_SCHEMA_URI, MODES } from '../Constants'
4645

4746
const CodeEditorContext = React.createContext(null)
@@ -93,16 +92,13 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
9392
const monacoRef = useRef(null)
9493
const { width, height: windowHeight } = useWindowSize()
9594
const memoisedReducer = React.useCallback(CodeEditorReducer, [])
96-
const [state, dispatch] = useReducer(memoisedReducer, initialState({ mode, theme, value, diffView, noParsing }))
95+
const [state, dispatch] = useReducer(memoisedReducer, initialState({ mode, theme, value, diffView, noParsing, tabSize }))
9796
const [, json, yamlCode, error] = useJsonYaml(state.code, tabSize, state.mode, !state.noParsing)
9897
const [, originalJson, originlaYaml] = useJsonYaml(defaultValue, tabSize, state.mode, !state.noParsing)
9998
const [contentHeight, setContentHeight] = useState(
10099
adjustEditorHeightToContent ? INITIAL_HEIGHT_WHEN_DYNAMIC_HEIGHT : height,
101100
)
102-
/**
103-
* TODO: can be removed with this new merge into react-monaco-editor :)
104-
* see: https://github.com/react-monaco-editor/react-monaco-editor/pull/955
105-
* */
101+
// TODO: upgrade to 0.56.2 to remove this
106102
const onChangeRef = useRef(onChange)
107103
onChangeRef.current = onChange
108104
monaco.editor.defineTheme(CodeEditorThemesKeys.vsDarkDT, {
@@ -260,28 +256,18 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
260256
onChangeRef.current?.(value)
261257
}
262258

263-
useEffect(() => {
259+
useEffectAfterMount(() => {
264260
if (noParsing) {
265261
setCode(value)
266262

267263
return
268264
}
269-
let obj
265+
270266
if (value === state.code) {
271267
return
272268
}
273-
try {
274-
obj = JSON.parse(value)
275-
} catch (err) {
276-
try {
277-
obj = YAML.parse(value)
278-
} catch (err) {}
279-
}
280-
let final = value
281-
if (obj) {
282-
final = state.mode === 'json' ? JSON.stringify(obj, null, tabSize) : YAMLStringify(obj)
283-
}
284-
setCode(final)
269+
270+
setCode(parseValueToCode(value, state.mode, tabSize))
285271
}, [value, noParsing])
286272

287273
useEffect(() => {

src/Common/CodeEditor/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export interface CodeEditorInitialValueType {
109109
theme?: string
110110
value: string
111111
noParsing?: boolean
112+
tabSize: number
112113
}
113114

114115
export interface CodeEditorState {

src/Common/RJSF/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
export { RJSFForm } from './Form'
18-
export * from './types'
18+
export type * from './types'
1919
export { getInferredTypeFromValueType, getRedirectionProps } from './utils'
2020
export { HIDE_SUBMIT_BUTTON_UI_SCHEMA } from './constants'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ViewError as GUIViewError } from './utils'
2+
export type { ViewErrorType as GUIViewErrorType } from './types'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export interface ViewErrorType extends Record<'title' | 'subTitle', string> {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ViewErrorType } from './types'
2+
3+
export class ViewError implements ViewErrorType {
4+
title: string = ''
5+
6+
subTitle: string = ''
7+
8+
constructor(title: string, subTitle: string) {
9+
this.title = title
10+
this.subTitle = subTitle
11+
}
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './GUIView'

0 commit comments

Comments
 (0)