Skip to content

Commit

Permalink
feat: enable error diags
Browse files Browse the repository at this point in the history
  • Loading branch information
alswl committed May 12, 2024
1 parent d210b94 commit a6b45cc
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Parser } from '@dbml/core';
import { Col, Row } from 'antd';
import { CompilerDiagnostic, Parser } from '@dbml/core';
import { Col, Row, message } from 'antd';
import { debounce } from 'lodash-es';
import { useState } from 'react';
import MonacoEditor from 'react-monaco-editor';

import { InitCode } from '@/components/editor';
import Viewer from '@/components/viewer/viewer';
import { PageContainer } from '@ant-design/pro-components';
import { CompilerError } from '@dbml/core/types/parse/error';
import './index.less';

export default () => {
const [messageApi, contextHolder] = message.useMessage();
const parser = new Parser();

const [code] = useState(InitCode);
Expand All @@ -28,40 +30,56 @@ export default () => {
console.log(newDB);
setDatabase(newDB);
} catch (e) {
console.error(e);
// TODO hl to editor
if (e as CompilerError) {
const diags = (e as CompilerError).diags
.map((d: CompilerDiagnostic) => {
return `${d.location.start.line}:${d.location.start.column} ${d.message}`;
})
.join('\n');

messageApi.error(diags);
console.error(e);
// TODO hl to editor
} else if (e instanceof Error) {
messageApi.error(`${e.message}`);
} else {
throw e;
}
}
};
const debouncedOnChange = debounce(onChange, 500);

return (
<PageContainer ghost header={{ title: '' }}>
<Row gutter={[8, 8]}>
<Col xxl={12} xl={12} lg={12} md={24} sm={24} xs={24}>
<div className="editor">
<MonacoEditor
width={'100%'}
language="dbml"
theme="vs-dark"
value={code}
options={{
selectOnLineNumbers: true,
minimap: {
enabled: false,
},
automaticLayout: true,
scrollBeyondLastLine: false,
}}
onChange={debouncedOnChange}
editorDidMount={editorDidMount}
// handle resize TODO
/>
</div>
</Col>
<Col xxl={12} xl={12} lg={12} md={24} sm={24} xs={24}>
<Viewer code={code} database={database} />
</Col>
</Row>
</PageContainer>
<>
{contextHolder}
<PageContainer ghost header={{ title: '' }}>
<Row gutter={[8, 8]}>
<Col xxl={12} xl={12} lg={12} md={24} sm={24} xs={24}>
<div className="editor">
<MonacoEditor
width={'100%'}
language="dbml"
theme="vs-dark"
value={code}
options={{
selectOnLineNumbers: true,
minimap: {
enabled: false,
},
automaticLayout: true,
scrollBeyondLastLine: false,
}}
onChange={debouncedOnChange}
editorDidMount={editorDidMount}
// handle resize TODO
/>
</div>
</Col>
<Col xxl={12} xl={12} lg={12} md={24} sm={24} xs={24}>
<Viewer code={code} database={database} />
</Col>
</Row>
</PageContainer>
</>
);
};

0 comments on commit a6b45cc

Please sign in to comment.