drawDB/src/components/table_overview.jsx

537 lines
21 KiB
React
Raw Normal View History

2023-09-19 20:48:48 +08:00
import { React, useContext, useState } from "react";
2023-09-19 20:49:57 +08:00
import { defaultTableTheme, sqlDataTypes, tableThemes } from "../data/data";
2023-09-19 20:47:51 +08:00
import {
Collapse,
Row,
Col,
Form,
Button,
Card,
2023-09-19 20:47:53 +08:00
Popover,
Checkbox,
2023-09-19 20:47:55 +08:00
Select,
2023-09-19 20:48:09 +08:00
AutoComplete,
2023-09-19 20:48:13 +08:00
Toast,
2023-09-19 20:48:28 +08:00
Empty,
2023-09-19 20:47:51 +08:00
} from "@douyinfe/semi-ui";
import {
IconMore,
IconKeyStroked,
2023-09-19 20:47:53 +08:00
IconDeleteStroked,
2023-09-19 20:47:54 +08:00
IconCheckboxTick,
2023-09-19 20:48:09 +08:00
IconPlus,
IconSearch,
2023-09-19 20:47:51 +08:00
} from "@douyinfe/semi-icons";
2023-09-19 20:48:28 +08:00
import {
IllustrationNoContent,
IllustrationNoContentDark,
} from "@douyinfe/semi-illustrations";
2023-09-19 20:49:57 +08:00
import { TableContext } from "../pages/editor";
2023-09-19 20:47:51 +08:00
2023-09-19 20:48:14 +08:00
export default function TableOverview(props) {
2023-09-19 20:47:55 +08:00
const [indexActiveKey, setIndexActiveKey] = useState("");
2023-09-19 20:48:09 +08:00
const [value, setValue] = useState("");
2023-09-19 20:49:57 +08:00
const { tables, setTables, addTable, deleteTable } = useContext(TableContext);
2023-09-19 20:48:09 +08:00
const [filteredResult, setFilteredResult] = useState(
2023-09-19 20:48:48 +08:00
tables.map((t) => {
2023-09-19 20:48:09 +08:00
return t.name;
})
);
const handleStringSearch = (value) => {
setFilteredResult(
2023-09-19 20:48:48 +08:00
tables
2023-09-19 20:48:09 +08:00
.map((t) => {
return t.name;
})
.filter((i) => i.includes(value))
);
};
2023-09-19 20:48:14 +08:00
const updatedField = (tid, fid, updatedValues) => {
2023-09-19 20:48:48 +08:00
setTables((prev) =>
2023-09-19 20:48:14 +08:00
prev.map((table, i) => {
if (tid === i) {
return {
...table,
fields: table.fields.map((field, j) =>
fid === j ? { ...field, ...updatedValues } : field
),
};
}
return table;
})
);
};
const updateTable = (tid, updatedValues) => {
2023-09-19 20:48:48 +08:00
setTables((prev) =>
2023-09-19 20:48:14 +08:00
prev.map((table, i) => {
if (tid === i) {
return {
...table,
...updatedValues,
};
}
return table;
})
);
2023-09-19 20:48:09 +08:00
};
2023-09-19 20:47:51 +08:00
return (
2023-09-19 20:48:09 +08:00
<>
2023-09-19 20:48:32 +08:00
<Row gutter={6}>
<Col span={16}>
<AutoComplete
data={filteredResult}
value={value}
showClear
prefix={<IconSearch />}
placeholder="Search..."
emptyContent={<div className="p-3">No tables found</div>}
onSearch={(v) => handleStringSearch(v)}
onChange={(v) => setValue(v)}
onSelect={(v) => {
2023-09-19 20:48:48 +08:00
const { id } = tables.find((t) => t.name === v);
2023-09-19 20:48:53 +08:00
props.setSelectedTable(`${id}`);
2023-09-19 20:48:32 +08:00
document
.getElementById(`scroll_table_${id}`)
.scrollIntoView({ behavior: "smooth" });
}}
className="w-full"
/>
</Col>
<Col span={8}>
2023-09-19 20:50:00 +08:00
<Button icon={<IconPlus />} block onClick={() => addTable(true)}>
2023-09-19 20:48:32 +08:00
Add table
</Button>
</Col>
</Row>
2023-09-19 20:48:09 +08:00
<Collapse
2023-09-19 20:48:53 +08:00
activeKey={props.selectedTable}
onChange={(k) => props.setSelectedTable(k)}
2023-09-19 20:48:13 +08:00
accordion
2023-09-19 20:48:09 +08:00
>
2023-09-19 20:48:48 +08:00
{tables.length <= 0 ? (
2023-09-19 20:48:30 +08:00
<div className="select-none">
<Empty
image={
<IllustrationNoContent style={{ width: 160, height: 160 }} />
}
darkModeImage={
<IllustrationNoContentDark
style={{ width: 160, height: 160 }}
/>
}
title="No tables"
description="Start building your diagram!"
/>
</div>
2023-09-19 20:48:28 +08:00
) : (
2023-09-19 20:48:48 +08:00
tables.map((t, i) => (
2023-09-19 20:48:32 +08:00
<div id={`scroll_table_${t.id}`} key={t.id}>
2023-09-19 20:48:28 +08:00
<Collapse.Panel header={<div>{t.name}</div>} itemKey={`${t.id}`}>
{t.fields.map((f, j) => (
<Form
key={j}
onChange={(value) => updatedField(i, j, value.values)}
2023-09-19 20:48:09 +08:00
>
2023-09-19 20:48:28 +08:00
<Row
type="flex"
justify="start"
align="middle"
gutter={6}
className="hover:bg-slate-100"
>
<Col span={7}>
<Form.Input
field="name"
noLabel={true}
initValue={f.name}
className="m-0"
placeholder="Name"
/>
</Col>
<Col span={8}>
<Form.Select
className="w-full"
field="type"
noLabel={true}
optionList={sqlDataTypes.map((value, index) => {
return {
label: value,
value: value,
};
})}
filter
initValue={f.type}
placeholder="Type"
></Form.Select>
</Col>
<Col span={3}>
<Button
type={f.notNull ? "primary" : "tertiary"}
title="Nullable"
theme={f.notNull ? "solid" : "light"}
onClick={() =>
updatedField(i, j, { notNull: !f.notNull })
}
>
?
</Button>
</Col>
<Col span={3}>
<Button
type={f.primary ? "primary" : "tertiary"}
title="Primary"
theme={f.primary ? "solid" : "light"}
onClick={() =>
updatedField(i, j, { primary: !f.primary })
}
icon={<IconKeyStroked />}
></Button>
</Col>
<Col span={3}>
<Popover
content={
<div className="px-1">
<Form
onChange={(value) =>
updatedField(i, j, value.values)
}
>
<Form.Input
field="default"
label="Default"
initValue={f.default}
trigger="blur"
placeholder="Set default"
/>
<Form.Input
field="check"
label="Check Expression"
trigger="blur"
placeholder="Set constraint"
initValue={f.check}
/>
<div className="flex justify-between items-center my-3">
<label
htmlFor="unique"
className="font-medium"
>
Unique
</label>
<Checkbox
value="unique"
defaultChecked={f.unique}
onChange={(checkedValues) =>
updatedField(i, j, {
[checkedValues.target.value]:
checkedValues.target.checked,
})
}
></Checkbox>
</div>
<div className="flex justify-between items-center my-3">
<label
htmlFor="increment"
className="font-medium"
>
Autoincrement
</label>
<Checkbox
value="increment"
defaultChecked={f.increment}
onChange={(checkedValues) =>
updatedField(i, j, {
[checkedValues.target.value]:
checkedValues.target.checked,
})
}
></Checkbox>
</div>
<Form.TextArea
field="comment"
label="Comment"
placeholder="Add comment"
initValue={f.comment}
autosize
rows={2}
/>
</Form>
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={(ev) => {
2023-09-19 20:48:48 +08:00
setTables((prev) => {
2023-09-19 20:48:28 +08:00
const updatedTables = [...prev];
const updatedFields = [
...updatedTables[t.id].fields,
];
updatedFields.splice(j, 1);
updatedTables[t.id].fields = [
...updatedFields,
];
return updatedTables;
});
}}
>
Delete
</Button>
</div>
}
trigger="click"
position="rightTop"
showArrow
>
<Button type="tertiary" icon={<IconMore />}></Button>
</Popover>
</Col>
</Row>
</Form>
))}
{t.indices.length > 0 && (
<Card
bodyStyle={{ padding: "4px" }}
style={{ marginTop: "12px", marginBottom: "12px" }}
headerLine={false}
>
<Collapse
activeKey={indexActiveKey}
onChange={(itemKey) => setIndexActiveKey(itemKey)}
2023-09-19 20:48:53 +08:00
accordion
2023-09-19 20:48:28 +08:00
>
2023-09-19 20:48:53 +08:00
<Collapse.Panel header="Indices" itemKey="1">
2023-09-19 20:48:28 +08:00
{t.indices.map((idx, k) => (
<div
className="flex justify-between items-center mb-2"
key={k}
>
<Select
placeholder="Select fields"
multiple
optionList={t.fields.map((e) => ({
value: e.name,
label: e.name,
}))}
className="w-full"
defaultValue={idx.fields}
onChange={(value) => {
2023-09-19 20:48:48 +08:00
const updatedTables = [...tables];
2023-09-19 20:48:28 +08:00
const updatedIndices = [...t.indices];
updatedIndices[k] = {
name: `${value.join("_")}_index`,
fields: [...value],
};
updatedTables[i] = {
...t,
indices: [...updatedIndices],
};
2023-09-19 20:48:48 +08:00
setTables(updatedTables);
2023-09-19 20:47:55 +08:00
}}
2023-09-19 20:48:28 +08:00
/>
<Popover
content={
<div className="px-1">
<Form>
<Form.Input
field="name"
label="Name"
initValue={idx.name}
trigger="blur"
placeholder="Index name"
/>
</Form>
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
2023-09-19 20:48:48 +08:00
const updatedTables = [...tables];
2023-09-19 20:48:28 +08:00
const updatedIndices = [...t.indices];
updatedIndices.splice(k, 1);
updatedTables[i] = {
...t,
indices: [...updatedIndices],
};
2023-09-19 20:48:48 +08:00
setTables(updatedTables);
2023-09-19 20:48:28 +08:00
}}
>
Delete
</Button>
</div>
}
trigger="click"
position="rightTop"
showArrow
2023-09-19 20:47:55 +08:00
>
2023-09-19 20:48:28 +08:00
<Button
icon={<IconMore />}
type="tertiary"
style={{ marginLeft: "12px" }}
></Button>
</Popover>
2023-09-19 20:47:55 +08:00
</div>
2023-09-19 20:48:28 +08:00
))}
</Collapse.Panel>
</Collapse>
</Card>
)}
2023-09-19 20:48:09 +08:00
<Card
bodyStyle={{ padding: "4px" }}
style={{ marginTop: "12px", marginBottom: "12px" }}
headerLine={false}
2023-09-19 20:47:57 +08:00
>
2023-09-19 20:48:28 +08:00
<Collapse>
<Collapse.Panel header="Comment" itemKey="1">
<Form onChange={(value) => updateTable(i, value.values)}>
<Form.TextArea
field="comment"
noLabel={true}
showClear
onClear={() => updateTable(i, { comment: "" })}
initValue={t.comment}
autosize
placeholder="Add comment"
rows={1}
/>
</Form>
2023-09-19 20:48:09 +08:00
</Collapse.Panel>
</Collapse>
</Card>
2023-09-19 20:48:28 +08:00
<Row gutter={6} className="mt-2">
<Col span={8}>
<Popover
content={
<div>
<div className="flex justify-between items-center p-2">
<div className="font-medium">Theme</div>
<Button
type="tertiary"
size="small"
onClick={() =>
updateTable(i, { color: defaultTableTheme })
}
>
Clear
</Button>
</div>
<hr />
<div className="py-3">
<div>
{tableThemes
.slice(0, Math.ceil(tableThemes.length / 2))
.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
onClick={() => updateTable(i, { color: c })}
>
{t.color === c ? (
<IconCheckboxTick
style={{ color: "white" }}
/>
) : (
<IconCheckboxTick style={{ color: c }} />
)}
</button>
))}
</div>
<div className="mt-3">
{tableThemes
.slice(Math.ceil(tableThemes.length / 2))
.map((c) => (
<button
key={c}
style={{ backgroundColor: c }}
className="p-3 rounded-full mx-1"
onClick={() => updateTable(i, { color: c })}
>
2023-09-19 20:48:09 +08:00
<IconCheckboxTick
2023-09-19 20:48:28 +08:00
style={{
color: t.color === c ? "white" : c,
}}
2023-09-19 20:48:09 +08:00
/>
2023-09-19 20:48:28 +08:00
</button>
))}
</div>
2023-09-19 20:48:09 +08:00
</div>
</div>
2023-09-19 20:48:28 +08:00
}
trigger="click"
position="bottomLeft"
showArrow
>
2023-09-19 20:49:11 +08:00
<div
className="h-[32px] w-[32px] rounded mb-2"
style={{ backgroundColor: t.color }}
/>
2023-09-19 20:48:28 +08:00
</Popover>
</Col>
<Col span={7}>
2023-09-19 20:48:09 +08:00
<Button
2023-09-19 20:48:28 +08:00
block
onClick={() => {
setIndexActiveKey("1");
2023-09-19 20:48:48 +08:00
const updatedTables = [...tables];
2023-09-19 20:48:28 +08:00
updatedTables[i] = {
...t,
indices: [
...t.indices,
{ name: `index_${t.indices.length}`, fields: [] },
],
};
2023-09-19 20:48:48 +08:00
setTables(updatedTables);
2023-09-19 20:48:28 +08:00
}}
>
Add index
</Button>
</Col>
<Col span={6}>
<Button
onClick={() => {
2023-09-19 20:48:48 +08:00
const updatedTables = [...tables];
2023-09-19 20:48:28 +08:00
updatedTables[i].fields = [
...updatedTables[i].fields,
{
name: "",
type: "",
default: "",
2023-09-19 20:49:25 +08:00
check: "",
2023-09-19 20:48:28 +08:00
primary: false,
unique: false,
notNull: false,
increment: false,
comment: "",
},
];
2023-09-19 20:48:48 +08:00
setTables(updatedTables);
2023-09-19 20:48:28 +08:00
}}
block
>
Add field
</Button>
</Col>
<Col span={3}>
<Button
icon={<IconDeleteStroked />}
type="danger"
onClick={() => {
Toast.success(`Table deleted!`);
2023-09-19 20:49:57 +08:00
deleteTable(i);
2023-09-19 20:48:53 +08:00
props.setSelectedTable("");
2023-09-19 20:48:28 +08:00
}}
2023-09-19 20:48:09 +08:00
></Button>
2023-09-19 20:48:28 +08:00
</Col>
</Row>
</Collapse.Panel>
</div>
))
)}
2023-09-19 20:48:09 +08:00
</Collapse>
</>
2023-09-19 20:47:51 +08:00
);
2023-09-19 20:48:14 +08:00
}