Commit 0f62d6fa authored by tom goriunov's avatar tom goriunov Committed by GitHub

hotfix: display error inside snippet if code editor crashes (#1364)

parent 12967209
import type { SystemStyleObject } from '@chakra-ui/react'; import type { SystemStyleObject } from '@chakra-ui/react';
import { Box, useColorMode, Flex, useToken } from '@chakra-ui/react'; import { Box, useColorMode, Flex, useToken, Center } from '@chakra-ui/react';
import type { EditorProps } from '@monaco-editor/react'; import type { EditorProps } from '@monaco-editor/react';
import MonacoEditor from '@monaco-editor/react'; import MonacoEditor from '@monaco-editor/react';
import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
...@@ -11,6 +11,7 @@ import type { SmartContractExternalLibrary } from 'types/api/contract'; ...@@ -11,6 +11,7 @@ import type { SmartContractExternalLibrary } from 'types/api/contract';
import useClientRect from 'lib/hooks/useClientRect'; import useClientRect from 'lib/hooks/useClientRect';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import isMetaKey from 'lib/isMetaKey'; import isMetaKey from 'lib/isMetaKey';
import ErrorBoundary from 'ui/shared/ErrorBoundary';
import CodeEditorBreadcrumbs from './CodeEditorBreadcrumbs'; import CodeEditorBreadcrumbs from './CodeEditorBreadcrumbs';
import CodeEditorLoading from './CodeEditorLoading'; import CodeEditorLoading from './CodeEditorLoading';
...@@ -207,6 +208,10 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -207,6 +208,10 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
}, },
}), [ editorWidth, themeColors, borderRadius ]); }), [ editorWidth, themeColors, borderRadius ]);
const renderErrorScreen = React.useCallback(() => {
return <Center bgColor={ themeColors['editor.background'] } w="100%" borderRadius="md">Oops! Something went wrong!</Center>;
}, [ themeColors ]);
if (data.length === 1) { if (data.length === 1) {
const sx = { const sx = {
...containerSx, ...containerSx,
...@@ -220,6 +225,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -220,6 +225,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
return ( return (
<Box height={ `${ EDITOR_HEIGHT }px` } width="100%" sx={ sx } ref={ containerNodeRef }> <Box height={ `${ EDITOR_HEIGHT }px` } width="100%" sx={ sx } ref={ containerNodeRef }>
<ErrorBoundary renderErrorScreen={ renderErrorScreen }>
<MonacoEditor <MonacoEditor
className="editor-container" className="editor-container"
language={ editorLanguage } language={ editorLanguage }
...@@ -229,6 +235,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -229,6 +235,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
onMount={ handleEditorDidMount } onMount={ handleEditorDidMount }
loading={ <CodeEditorLoading borderRadius="md"/> } loading={ <CodeEditorLoading borderRadius="md"/> }
/> />
</ErrorBoundary>
</Box> </Box>
); );
} }
...@@ -247,6 +254,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -247,6 +254,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
onKeyDown={ handleKeyDown } onKeyDown={ handleKeyDown }
onKeyUp={ handleKeyUp } onKeyUp={ handleKeyUp }
> >
<ErrorBoundary renderErrorScreen={ renderErrorScreen }>
<Box flexGrow={ 1 }> <Box flexGrow={ 1 }>
<CodeEditorTabs <CodeEditorTabs
tabs={ tabs } tabs={ tabs }
...@@ -275,6 +283,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -275,6 +283,7 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
selectedFile={ data[index].file_path } selectedFile={ data[index].file_path }
mainFile={ mainFile } mainFile={ mainFile }
/> />
</ErrorBoundary>
</Flex> </Flex>
); );
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment