Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve db table module & test script support output logs #202

Merged
merged 2 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 51 additions & 47 deletions src/components/IDE/DBTable/DBTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { observer } from 'mobx-react-lite';
import { useStore } from '@/store/index';
import { Box, Button, Flex, Stack, Image } from '@chakra-ui/react';
import { Box, Button, Flex, Stack, Image, Spinner, Text } from '@chakra-ui/react';
import JSONTable from '@/components/JSONTable';
import { useEffect, useRef, useState } from 'react';
import { useEffect, useRef } from 'react';
import { defaultButtonStyle, defaultOutlineButtonStyle } from '@/lib/theme';
import { MdRefresh } from 'react-icons/md';
import { AddIcon, DeleteIcon, EditIcon } from '@chakra-ui/icons';
Expand Down Expand Up @@ -190,6 +190,38 @@ const ViewData = observer(() => {
return (
<>
<Flex alignItems="center">
<Button
mr="20px"
h="32px"
size="sm"
leftIcon={<AddIcon />}
{...defaultButtonStyle}
onClick={async (e) => {
const form = creatColumnDataForm(dbTable.currentColumns);
const formData = await hooks.getFormData({
title: `Insert data to '${dbTable.currentTable.tableName}'`,
size: 'md',
formList: [
{
form
}
]
});
try {
const keys = Object.keys(formData);
const values = Object.values(formData);
const errorMsg = await dbTable.createTableData(keys, values);
if (!errorMsg) {
const data = await dbTable.getCurrentTableData.call();
dbTable.table.set({
dataSource: data
});
}
} catch (error) { }
}}
>
Insert
</Button>
<label>
<CSVReader
label=""
Expand All @@ -207,7 +239,7 @@ const ViewData = observer(() => {
if (errorMsg) {
toast.error(errorMsg);
} else {
const data = await dbTable.getCurrentTableData();
const data = await dbTable.getCurrentTableData.call();
dbTable.table.set({
dataSource: data
});
Expand All @@ -231,51 +263,19 @@ const ViewData = observer(() => {
inputName="csv-input"
inputStyle={{ display: 'none' }}
/>
<Flex alignItems="center" px="20px" h="32px" borderRadius="4px" cursor="pointer" {...defaultButtonStyle}>
<Flex alignItems="center" px="10px" h="32px" borderRadius="6px" cursor="pointer" {...defaultButtonStyle}>
<FiUpload />
<Box ml="10px" fontSize={"14px"}>Upload CSV</Box>
<Text ml="10px" fontSize="12px" fontWeight={600}>Upload CSV</Text>
</Flex>
</label>
<Button
ml="20px"
h="32px"
size="sm"
leftIcon={<AddIcon />}
{...defaultButtonStyle}
onClick={async (e) => {
const form = creatColumnDataForm(dbTable.currentColumns);
const formData = await hooks.getFormData({
title: `Insert data to '${dbTable.currentTable.tableName}'`,
size: 'md',
formList: [
{
form
}
]
});
try {
const keys = Object.keys(formData);
const values = Object.values(formData);
const errorMsg = await dbTable.createTableData(keys, values);
if (!errorMsg) {
const data = await dbTable.getCurrentTableData();
dbTable.table.set({
dataSource: data
});
}
} catch (error) { }
}}
>
Insert
</Button>
<Button
ml="20px"
h="32px"
size="sm"
leftIcon={<MdRefresh />}
{...defaultOutlineButtonStyle}
onClick={async (e) => {
dbTable.init();
dbTable.init.call();
}}
>
Refresh
Expand All @@ -297,8 +297,6 @@ const QuerySQL = observer(() => {
}, 800)
);

const [queryResult, setQueryResult] = useState('');

return (
<Box bg="#000">
<Box p="1" fontSize="sm" fontWeight={700} color="#fff">
Expand All @@ -321,15 +319,21 @@ const QuerySQL = observer(() => {
/>
<Box
pos="absolute"
bottom={4}
bottom={1}
right={4}
cursor="pointer"
onClick={async () => {
const result = await dbTable.querySQL();
setQueryResult(JSON.stringify(result, null, 2));
onClick={() => {
if (dbTable.querySQL.loading.value) {
return;
}
dbTable.querySQL.call();
}}
>
<Image p={1} h={6} w={6} borderRadius="4px" bg="#946FFF" _hover={{ background: 'gray.200' }} src="/images/icons/execute.svg" />
{
dbTable.querySQL.loading.value
? <Spinner size="sm" color='#fff' />
: <Image p={1} h={6} w={6} borderRadius="4px" bg="#946FFF" _hover={{ background: 'gray.200' }} src="/images/icons/execute.svg" />
}
</Box>
</Box>
<Box p="1" fontSize="sm" fontWeight={700} color="#fff">
Expand All @@ -345,7 +349,7 @@ const QuerySQL = observer(() => {
height="calc(100vh - 480px)"
theme="vs-dark"
language="json"
value={queryResult}
value={dbTable.querySQL.value}
/>
</Box>
);
Expand All @@ -357,7 +361,7 @@ const DBTable = observer(() => {
} = useStore();

useEffect(() => {
dbTable.init();
dbTable.init.call();
}, [dbTable.currentTable.tableSchema, dbTable.currentTable.tableName]);

useEffect(() => {
Expand Down
45 changes: 6 additions & 39 deletions src/components/IDE/DBTable/SideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react';
import { Flex, Box, Text, Tooltip, Button, useDisclosure, Collapse, Spinner } from '@chakra-ui/react';
import { Icon } from '@chakra-ui/react';
import { ChevronDownIcon, ChevronRightIcon, DeleteIcon, EditIcon, ViewIcon } from '@chakra-ui/icons';
import { ChevronDownIcon, ChevronRightIcon, DeleteIcon, EditIcon } from '@chakra-ui/icons';
import { MdAddBox } from 'react-icons/md';
import { observer } from 'mobx-react-lite';
import { useStore } from '@/store/index';
Expand All @@ -19,14 +19,6 @@ export const DBTableSideBar = observer(() => {
allTables.call();
}, []);

if (allTables.loading.value) {
return (
<Flex w="100%" h="100%" justify="center">
<Spinner mt="100px" color="#946FFF" />
</Flex>
);
}

if (!allTables.value) {
return null;
}
Expand Down Expand Up @@ -64,10 +56,11 @@ const TableNames = observer(({ tableSchema, tables }: { tableSchema: string; tab
<Flex alignItems="center" overflowX="auto">
<Icon as={collaspeState.isOpen ? ChevronDownIcon : ChevronRightIcon} boxSize={6} cursor="pointer" />
<Box w="220px" fontSize="16x" fontWeight={700}>
{/* {tableSchema} */} Default
{tableSchema === 'public' ? "Default" : tableSchema}
</Box>
</Flex>
<Flex alignItems="center">
{dbTable.allTables.loading.value && <Spinner size="sm" color="#946FFF" />}
<Tooltip hasArrow label="Create a new table" placement="bottom">
<Button
p={0}
Expand Down Expand Up @@ -109,30 +102,20 @@ const TableNames = observer(({ tableSchema, tables }: { tableSchema: string; tab
borderBottom="1px solid rgba(0, 0, 0, 0.06)"
sx={getSelectedStyles(dbTable.currentTable.tableId === item.tableId)}
cursor="pointer"
onClick={() => {
onClick={(e) => {
e.stopPropagation();
dbTable.setCurrentTable({
tableSchema,
tableId: item.tableId,
tableName: item.tableName
});
if (dbTable.mode === 'QUERY_SQL') {
dbTable.setDefaultSQL();
}
dbTable.setMode('VIEW_DATA');
}}
>
<Text
fontSize="14px"
fontWeight={600}
overflowX="auto"
onClick={(e) => {
e.stopPropagation();
dbTable.setCurrentTable({
tableSchema,
tableId: item.tableId,
tableName: item.tableName
});
dbTable.setMode('VIEW_DATA');
}}
>
{item.tableName}
</Text>
Expand Down Expand Up @@ -172,22 +155,6 @@ const TableNames = observer(({ tableSchema, tables }: { tableSchema: string; tab
}}
/>
</Tooltip>
<Tooltip hasArrow label="View Data" placement="bottom">
<ViewIcon
ml="12px"
boxSize={4}
cursor="pointer"
onClick={(e) => {
e.stopPropagation();
dbTable.setCurrentTable({
tableSchema,
tableId: item.tableId,
tableName: item.tableName
});
dbTable.setMode('VIEW_DATA');
}}
/>
</Tooltip>
</Flex>
</Flex>
);
Expand Down
10 changes: 7 additions & 3 deletions src/components/IDE/DBTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const DeveloperDBTable = () => {
<DBTable />
</Box>
{w3s.showContent === 'DB_TABLE' && (
<Box pos="absolute" right="0px" top="0px">
<Box pos="absolute" right={2} top="0px">
<Tooltip label="Query SQL" placement="bottom">
<Box
position="relative"
cursor="pointer"
onClick={() => {
w3s.dbTable.setDefaultSQL();
w3s.dbTable.setMode('QUERY_SQL');
if (w3s.dbTable.mode === 'QUERY_SQL') {
w3s.dbTable.setMode('VIEW_DATA');
} else {
w3s.dbTable.setDefaultSQL();
w3s.dbTable.setMode('QUERY_SQL');
}
}}
>
<Image h={10} w={10} src="/images/icons/execute_sql.svg" />
Expand Down
31 changes: 24 additions & 7 deletions src/components/IDE/Editor/EditorBottomPanels/ConsolePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import dayjs from '@/lib/dayjs';
import { StdIOType } from '@/server/wasmvm';
import { useStore } from '@/store/index';
import { Box, Flex } from '@chakra-ui/react';
import { observer } from 'mobx-react-lite';
import { useEffect, useRef } from 'react';
import { VscClearAll, VscDebugStart } from 'react-icons/vsc';
import { VscClearAll } from 'react-icons/vsc';

const stringify = (val: Partial<StdIOType>) => {
try {
return JSON.stringify(val);
} catch (error) {
return JSON.stringify({
'@lv': val['@lv'],
'@ts': val['@ts'],
msg: error.message
});
}
}

export const ConsolePanel = observer(() => {
const {
w3s,
w3s: {
projectManager: { curFilesListSchema },
lab
}
} = useStore();
Expand Down Expand Up @@ -59,12 +70,18 @@ export const ConsolePanel = observer(() => {
{lab.stdout?.map((i) => {
return (
<Flex color={i?.['@lv'] == 'error' ? 'red' : ''}>
<Flex maxW="200px" mr={2} whiteSpace="nowrap">
[<Box color="#d892ff">{i.prefix} </Box>
<Flex mr={2} whiteSpace="nowrap">
[<Box color="#d892ff">{i.prefix}</Box>
<Box color="#ffd300">{dayjs(i?.['@ts']).format('hh:mm:ss')}</Box>]
</Flex>
<Flex w="90%" overflowWrap={'anywhere'}>
{JSON.stringify(i)}
<Flex>
{stringify(
{
'@lv': i?.['@lv'],
'@ts': i?.['@ts'],
msg: i?.msg
}
)}
</Flex>
</Flex>
);
Expand Down
Loading