Skip to content

Implement JSON formatting and syntax highlighting#443

Open
AmerMesanovic wants to merge 2 commits intoastarte-platform:masterfrom
AmerMesanovic:json-editor
Open

Implement JSON formatting and syntax highlighting#443
AmerMesanovic wants to merge 2 commits intoastarte-platform:masterfrom
AmerMesanovic:json-editor

Conversation

@AmerMesanovic
Copy link
Copy Markdown
Contributor

@AmerMesanovic AmerMesanovic commented Jul 5, 2024

Enhance UX in Dashboard with JSON text editor using Monaco Editor (@monaco-editor/react) for improved formatting, syntax highlighting, and validation error reporting.

Screenshot from 2024-07-05 09-28-54
Screenshot from 2024-07-05 09-28-34
Screenshot from 2024-07-05 09-28-23
Screenshot from 2024-07-05 09-28-18

@AmerMesanovic AmerMesanovic force-pushed the json-editor branch 2 times, most recently from 2148e57 to e493080 Compare July 11, 2024 13:12
@AmerMesanovic AmerMesanovic requested a review from Pavinati July 11, 2024 13:30
@AmerMesanovic AmerMesanovic requested a review from Pavinati August 8, 2024 09:29
@AmerMesanovic AmerMesanovic force-pushed the json-editor branch 2 times, most recently from 00f7438 to a095ea6 Compare August 9, 2024 06:52
databaseTTL,
} = parseMappingOptions(_.get(iface.mappings, '0'));
const { reliability, explicitTimestamp, retention, expiry, databaseRetention, databaseTTL } =
parseMappingOptions(_.get(iface.mappings, '0'));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are no changes, please avoid to change code format as this will introduce undesired noise to the pr.
Same goes for unnecessary ; at the end of a line

there are multiple occurrences in this PR that only changes how the code is styled but add nothing, it's better to move a restyling in another PR

.within(() => {
cy.contains(mapping.endpoint);
cy.get('button').contains('Edit...').click();
cy.get('button').contains('Edit...').should('have.length', 1).click({ force: true });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could possible have more that one edit button? There is a necessity to check that?

cy.fixture(interfaceFixture).then(({ data: iface }) => {
setupInterfaceEditorFromUI(iface);
checkInterfaceEditorUIValues(iface);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above, avoid newlines if not necessary

Comment on lines +86 to +94
if (win.monaco && win.monaco.editor) {
const editor = win.monaco.editor.getModels()[0];
const editorContent = editor.getValue();
const editorJson = JSON.parse(editorContent);

expect(editorJson.simple_triggers[0].interface_name).to.equal(iface);
} else {
throw new Error('Monaco Editor instance is not available');
}
});
cy.window().then((win) => {
if (win.monaco && win.monaco.editor) {
const editor = win.monaco.editor.getModels()[0];
const editorContent = editor.getValue();
const editorJson = JSON.parse(editorContent);

expect(editorJson.simple_triggers[0].interface_major).to.equal(simpleTrigger.interface_major);
} else {
throw new Error('Monaco Editor instance is not available');
}
});
cy.window().then((win) => {
if (win.monaco && win.monaco.editor) {
const editor = win.monaco.editor.getModels()[0];
const editorContent = editor.getValue();
const editorJson = JSON.parse(editorContent);

expect(editorJson.simple_triggers[0].match_path).to.equal(simpleTrigger.match_path);
} else {
throw new Error('Monaco Editor instance is not available');
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here there is a repetition of

      cy.window().then((win) => {
        if (win.monaco && win.monaco.editor) {
          const editor = win.monaco.editor.getModels()[0];
          const editorContent = editor.getValue();
          const editorJson = JSON.parse(editorContent);

all of that can be merged in a single if clause

.scrollIntoView()
.should('be.visible')
.contains(simpleTrigger.interface_major)
.contains(triggerOperatorToLabel[simpleTrigger.value_match_operator])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to check for more than one property, but

.contains(simpleTrigger.interface_major)

got removed

.scrollIntoView()
.should('be.visible')
.contains(simpleTrigger.interface_major)
.contains(triggerOperatorToLabel[simpleTrigger.value_match_operator])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to check for more than one property, but

.contains(simpleTrigger.interface_major)

got removed entirely

Comment on lines +338 to +342
cy.get('#triggerSource', { timeout: 1000 }).scrollIntoView().should('be.visible');
cy.get('button').contains('Hide source', { timeout: 1000 }).scrollIntoView().click();
cy.get('#triggerSource', { timeout: 1000 }).should('not.exist');
cy.get('button').contains('Show source', { timeout: 1000 }).scrollIntoView().click();
cy.get('#triggerSource', { timeout: 1000 }).scrollIntoView().should('be.visible');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not required, all of those timeouts would only prolong the duration of this already long tests

This apply for the entire PR, check and be sure the timeouts/cy.wait functions are really needed

Enhance UX in Dashboard with JSON text editor using Monaco Editor
(@monaco-editor/react) for improved formatting, syntax highlighting,
and validation error reporting.

Signed-off-by: AmerMesanovic <amer.mesanovic@secomind.com>
@AmerMesanovic AmerMesanovic requested a review from eddbbt February 4, 2025 12:44
@AmerMesanovic AmerMesanovic force-pushed the json-editor branch 2 times, most recently from 0c6eadb to 1563051 Compare February 5, 2025 12:17
@AmerMesanovic AmerMesanovic force-pushed the json-editor branch 9 times, most recently from 82336bc to 2e397f2 Compare February 7, 2025 13:48
utilizing MonacoEditor.

- Updated test selectors to target JSONEditor
component.
- Used force option for clicks to handle overlays
in the editor.
- Increased timeouts where necessary to account
for MonacoEditor's loading times

Signed-off-by: AmerMesanovic <amer.mesanovic@secomind.com>
Copy link
Copy Markdown
Contributor

@eddbbt eddbbt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Annopaolo can we relaunch the CI? imho it's just cypress flakiness

Comment on lines +1 to +30
import React, { createContext, useContext, useEffect, useState } from 'react';
import monaco from '@monaco-editor/react';

interface MonacoContextType {
monaco: typeof monaco | null;
}

const MonacoContext = createContext<MonacoContextType | undefined>(undefined);

export const MonacoProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [monacoInstance, setMonacoInstance] = useState<typeof monaco | null>(null);

useEffect(() => {
if (!monacoInstance) {
setMonacoInstance(monaco);
}
}, [monacoInstance]);

return (
<MonacoContext.Provider value={{ monaco: monacoInstance }}>{children}</MonacoContext.Provider>
);
};

export const useMonacoContext = (): MonacoContextType => {
const context = useContext(MonacoContext);
if (!context) {
throw new Error('useMonacoContext must be used within a MonacoProvider');
}
return context;
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file defines a MonacoContext and a useMonacoContext, but it seems they are not really used anywhere (besides wrapping the App with MonacoProvider).
What is their purpose? Can we get rid of them?
From the documentation of the editor it should be enough to import its React component and render it where needed, as you already did.

Comment on lines +16 to +23
cy.get('.monaco-editor')
.should('be.visible')
.then(() => {
cy.window().then((win) => {
const editor = win.monaco.editor.getModels()[0];
editor.setValue(JSON.stringify(iface, null, 4));
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be ideal to stick to the basic Cypress commands, thus simulating an actual user interacting and writing (or pasting) into the editor.
The main idea when doing e2e tests is to avoid thinking about the implementation and just test the expected user interaction.
Do you think it's feasible, or did you encounter some issues?

interfaceFixtures.forEach((interfaceFixture) => {
cy.fixture(interfaceFixture).then(({ data: iface }) => {
setupInterfaceEditorFromSource(iface);
setupInterfaceEditorFromUI(iface);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like cheating :)

This test correctly loads interface from its source should validate that when the user writes into the editor (setupInterfaceEditorFromSource(iface)) then the UI form fields are automatically updated and correspond to the provided source.

If we call setupInterfaceEditorFromUI(iface) to setup the UI form fields, of course they will correspond: it defies the purpose of the test.

If we want to maintain the same behavior, we can update the implementation instead of the test, making sure that when the user changes the editor's content then the UI fields are updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants