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,15 +225,17 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -220,15 +225,17 @@ 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 }>
<MonacoEditor <ErrorBoundary renderErrorScreen={ renderErrorScreen }>
className="editor-container" <MonacoEditor
language={ editorLanguage } className="editor-container"
path={ data[index].file_path } language={ editorLanguage }
defaultValue={ data[index].source_code } path={ data[index].file_path }
options={ EDITOR_OPTIONS } defaultValue={ data[index].source_code }
onMount={ handleEditorDidMount } options={ EDITOR_OPTIONS }
loading={ <CodeEditorLoading borderRadius="md"/> } onMount={ handleEditorDidMount }
/> loading={ <CodeEditorLoading borderRadius="md"/> }
/>
</ErrorBoundary>
</Box> </Box>
); );
} }
...@@ -247,34 +254,36 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props) ...@@ -247,34 +254,36 @@ const CodeEditor = ({ data, remappings, libraries, language, mainFile }: Props)
onKeyDown={ handleKeyDown } onKeyDown={ handleKeyDown }
onKeyUp={ handleKeyUp } onKeyUp={ handleKeyUp }
> >
<Box flexGrow={ 1 }> <ErrorBoundary renderErrorScreen={ renderErrorScreen }>
<CodeEditorTabs <Box flexGrow={ 1 }>
tabs={ tabs } <CodeEditorTabs
activeTab={ data[index].file_path } tabs={ tabs }
activeTab={ data[index].file_path }
mainFile={ mainFile }
onTabSelect={ handleTabSelect }
onTabClose={ handleTabClose }
/>
<CodeEditorBreadcrumbs path={ data[index].file_path }/>
<MonacoEditor
className="editor-container"
height={ `${ EDITOR_HEIGHT }px` }
language={ editorLanguage }
path={ data[index].file_path }
defaultValue={ data[index].source_code }
options={ EDITOR_OPTIONS }
onMount={ handleEditorDidMount }
loading={ <CodeEditorLoading borderBottomLeftRadius="md"/> }
/>
</Box>
<CodeEditorSideBar
data={ data }
onFileSelect={ handleSelectFile }
monaco={ instance }
editor={ editor }
selectedFile={ data[index].file_path }
mainFile={ mainFile } mainFile={ mainFile }
onTabSelect={ handleTabSelect }
onTabClose={ handleTabClose }
/> />
<CodeEditorBreadcrumbs path={ data[index].file_path }/> </ErrorBoundary>
<MonacoEditor
className="editor-container"
height={ `${ EDITOR_HEIGHT }px` }
language={ editorLanguage }
path={ data[index].file_path }
defaultValue={ data[index].source_code }
options={ EDITOR_OPTIONS }
onMount={ handleEditorDidMount }
loading={ <CodeEditorLoading borderBottomLeftRadius="md"/> }
/>
</Box>
<CodeEditorSideBar
data={ data }
onFileSelect={ handleSelectFile }
monaco={ instance }
editor={ editor }
selectedFile={ data[index].file_path }
mainFile={ mainFile }
/>
</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