Skip to content

Commit cccfa69

Browse files
committed
feat: simplify the console
Simply the Console component by removing the JSONView component. - Remove JSONView component - Remove unused Header component - Remove unused types
1 parent 622837e commit cccfa69

File tree

21 files changed

+70
-357
lines changed

21 files changed

+70
-357
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default [
3838
'react/jsx-uses-react': 'error',
3939
'react/jsx-uses-vars': 'error',
4040
'react/prop-types': 'off',
41+
'@typescript-eslint/consistent-type-definitions': ['error', 'type'],
4142
},
4243
},
4344
];

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"@headlessui/react": "^2.2.2",
4545
"@heroicons/react": "^2.2.0",
4646
"@testing-library/jest-dom": "^6.6.3",
47-
"@uiw/react-json-view": "^2.0.0-alpha.30",
4847
"axios": "^1.7.9",
4948
"date-fns": "^4.1.0",
5049
"eslint-plugin-react": "^7.37.2",

src/components/App/App.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ const App: React.FC = () => {
3131
<Editor />
3232
</div>
3333

34-
<div className="bg-white text-black overflow-auto">
35-
<Console />
36-
</div>
34+
<Console />
3735
</Split>
3836
</div>
3937
);

src/components/CodeEditor/CodeEditor.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ const CodeEditor: React.FC = () => {
1717
value: '',
1818
language: 'typescript',
1919
fontSize: 20,
20-
theme: state.theme,
20+
theme: 'vs-dark',
2121
minimap: {
2222
enabled: false,
2323
},
2424
};
2525

2626
const editorInstance = monaco.editor.create(
2727
editorRef.current!,
28-
editorConfig
28+
editorConfig,
2929
);
3030

3131
monaco.editor.setModelLanguage(editorInstance.getModel()!, 'typescript');
@@ -69,10 +69,6 @@ const CodeEditor: React.FC = () => {
6969
return () => editor?.dispose();
7070
}, [editorRef.current]);
7171

72-
useEffect(() => {
73-
editor?.updateOptions({ theme: state.theme });
74-
}, [state.theme]);
75-
7672
useEffect(() => {
7773
editor?.setValue(state.codeSample);
7874
}, [state.codeSample]);
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('<Console />', () => {
2020
render(
2121
<AppContext.Provider value={{ state, dispatch }}>
2222
<Console />
23-
</AppContext.Provider>
23+
</AppContext.Provider>,
2424
);
2525

2626
const errorDiv = screen.getByTestId('console-error');
@@ -34,12 +34,12 @@ describe('<Console />', () => {
3434
render(
3535
<AppContext.Provider value={{ state, dispatch }}>
3636
<Console />
37-
</AppContext.Provider>
37+
</AppContext.Provider>,
3838
);
3939

4040
expect(screen.queryByTestId('console-error')).not.toBeInTheDocument();
4141
expect(screen.getByTestId('console-result').children.length).toEqual(
42-
result.length
42+
result.length,
4343
);
4444
});
4545

@@ -49,14 +49,33 @@ describe('<Console />', () => {
4949
render(
5050
<AppContext.Provider value={{ state, dispatch }}>
5151
<Console />
52-
</AppContext.Provider>
52+
</AppContext.Provider>,
5353
);
5454

5555
expect(screen.getByTestId('console-result-item-0').textContent).toEqual(
56-
JSON.stringify(result[0])
56+
JSON.stringify(result[0], null, 0),
5757
);
5858
expect(screen.getByTestId('console-result-item-1').textContent).toEqual(
59-
result[1]
59+
JSON.stringify(result[1], null, 0),
60+
);
61+
});
62+
63+
it('add indentation when result is large', () => {
64+
const largeObj = {
65+
a: 'This is a long string to make the object large enough for pretty print.',
66+
b: Array(10).fill('more data'),
67+
c: { nested: true, arr: [1, 2, 3, 4, 5] },
68+
};
69+
state.result = [largeObj];
70+
render(
71+
<AppContext.Provider value={{ state, dispatch }}>
72+
<Console />
73+
</AppContext.Provider>,
74+
);
75+
76+
// The component should pretty-print with 2 spaces for large objects
77+
expect(screen.getByTestId('console-result-item-0').textContent).toEqual(
78+
JSON.stringify(largeObj, null, 2),
6079
);
6180
});
6281
});

src/components/Console/Console.tsx

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
import { useContext } from 'react';
2-
import _ from 'lodash';
32
import { AppContext } from 'context/AppContext';
43

54
const Console: React.FC = () => {
65
const { state } = useContext(AppContext);
7-
const { result, error, theme } = state;
8-
const extraClass = theme === 'vs-light' ? 'console-light' : '';
6+
const { result, error } = state;
97

108
const createKey = (index: number) => {
119
return `key${index}`;
1210
};
1311

14-
if (error) {
12+
const renderError = () => {
1513
return (
16-
<div className="console">
17-
<div data-testid="console-error" className="error">
18-
{error}
19-
</div>
14+
<div data-testid="console-error" className="font-bold text-red-500">
15+
{error}
2016
</div>
2117
);
22-
}
18+
};
19+
20+
const renderResult = () => {
21+
return result.map((item, index) => {
22+
const key = createKey(index);
23+
const isObject = typeof item === 'object' && item !== null;
24+
const str = JSON.stringify(item);
25+
const space = isObject && str.length > 60 ? 2 : 0;
26+
27+
return (
28+
<div key={key} className="py-1.5">
29+
<pre>
30+
<span className="mr-2 text-[#8be9fd]">&#8250;</span>
31+
<span data-testid={`console-result-item-${index}`}>
32+
{JSON.stringify(item, null, space)}
33+
</span>
34+
</pre>
35+
</div>
36+
);
37+
});
38+
};
2339

2440
return (
25-
<div data-testid="console-result" className={`console ${extraClass}`}>
26-
{result.map((item, index) => {
27-
const consoleItem = !_.isString(item) ? JSON.stringify(item) : item;
28-
return (
29-
<div key={createKey(index)}>
30-
<pre>
31-
<span style={{ marginRight: 5 }}>&#8250;</span>
32-
<span data-testid={`console-result-item-${index}`}>
33-
{consoleItem}
34-
</span>
35-
</pre>
36-
</div>
37-
);
38-
})}
41+
<div
42+
data-testid="console-result"
43+
className="bg-[#282a36] text-[#f8f8f2] font-mono overflow-x-auto p-4"
44+
>
45+
{error ? renderError() : renderResult()}
3946
</div>
4047
);
4148
};

src/components/Header/Header.spec.tsx

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)