drawDB/src/components/TableOverview.jsx

1131 lines
50 KiB
React
Raw Normal View History

2024-03-13 07:27:42 +08:00
import { useState } from "react";
2023-09-19 20:50:02 +08:00
import {
Action,
ObjectType,
2024-03-15 22:37:22 +08:00
defaultBlue,
2023-09-19 20:50:02 +08:00
sqlDataTypes,
tableThemes,
2024-03-15 22:37:22 +08:00
} from "../data/constants";
2023-09-19 20:47:51 +08:00
import {
Collapse,
Row,
Col,
2023-09-19 20:50:02 +08:00
Input,
2023-09-19 20:50:04 +08:00
TextArea,
2023-09-19 20:47:51 +08:00
Button,
Card,
2023-09-19 20:50:43 +08:00
TagInput,
2023-09-19 20:51:11 +08:00
InputNumber,
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";
2024-03-11 08:45:44 +08:00
import { getSize, hasCheck, hasPrecision, isSized } from "../utils/toSQL";
import useTables from "../hooks/useTables";
import useUndoRedo from "../hooks/useUndoRedo";
import useSelect from "../hooks/useSelect";
2024-03-13 07:27:42 +08:00
import useTypes from "../hooks/useTypes";
2023-09-19 20:47:51 +08:00
2023-12-16 11:39:13 +08:00
export default function TableOverview() {
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:50:49 +08:00
const {
tables,
addTable,
deleteTable,
updateField,
updateTable,
setRelationships,
} = useTables();
2024-03-13 07:27:42 +08:00
const { types } = useTypes();
const { setUndoStack, setRedoStack } = useUndoRedo();
const { selectedElement, setSelectedElement } = useSelect();
2023-09-19 20:50:02 +08:00
const [editField, setEditField] = useState({});
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: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..."
onSearch={(v) => handleStringSearch(v)}
2023-09-19 20:51:08 +08:00
emptyContent={
<div className="p-3 popover-theme">No tables found</div>
}
2023-09-19 20:48:32 +08:00
onChange={(v) => setValue(v)}
onSelect={(v) => {
2023-09-19 20:48:48 +08:00
const { id } = tables.find((t) => t.name === v);
2024-03-14 02:39:16 +08:00
setSelectedElement((prev) => ({
...prev,
2023-09-19 20:50:28 +08:00
id: id,
2024-03-14 02:39:16 +08:00
open: true,
}));
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
2024-03-14 02:39:16 +08:00
activeKey={selectedElement.open ? `${selectedElement.id}` : ""}
2023-09-19 20:50:28 +08:00
onChange={(k) =>
2024-03-14 02:39:16 +08:00
setSelectedElement((prev) => ({
...prev,
2023-09-19 20:50:28 +08:00
id: parseInt(k),
2024-03-14 02:39:16 +08:00
open: true,
}))
2023-09-19 20:50:28 +08:00
}
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:51:08 +08:00
<div className="select-none mt-2">
2023-09-19 20:48:30 +08:00
<Empty
image={
2023-09-19 20:51:08 +08:00
<IllustrationNoContent style={{ width: 154, height: 154 }} />
2023-09-19 20:48:30 +08:00
}
darkModeImage={
<IllustrationNoContentDark
2023-09-19 20:51:08 +08:00
style={{ width: 154, height: 154 }}
2023-09-19 20:48:30 +08:00
/>
}
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}`}>
2023-09-19 20:51:17 +08:00
<div className="flex items-center mb-2.5">
2023-09-19 20:51:13 +08:00
<div className="text-md font-semibold">Name: </div>
<Input
value={t.name}
2023-09-19 20:51:17 +08:00
validateStatus={t.name === "" ? "error" : "default"}
2023-09-19 20:51:13 +08:00
placeholder="Name"
className="ms-2"
onChange={(value) => updateTable(t.id, { name: value })}
onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: t.id,
undo: editField,
redo: { name: e.target.value },
message: `Edit table name to ${e.target.value}`,
},
]);
setRedoStack([]);
}}
/>
</div>
2023-09-19 20:48:28 +08:00
{t.fields.map((f, j) => (
2023-09-19 20:51:13 +08:00
<Row gutter={6} key={j} className="hover-1 my-2">
2023-09-19 20:50:04 +08:00
<Col span={7}>
<Input
value={f.name}
2023-09-19 20:51:17 +08:00
validateStatus={f.name === "" ? "error" : "default"}
2023-09-19 20:50:04 +08:00
placeholder="Name"
onChange={(value) => updateField(i, j, { name: value })}
2023-09-19 20:50:15 +08:00
onFocus={(e) => setEditField({ name: e.target.value })}
2023-09-19 20:50:04 +08:00
onBlur={(e) => {
if (e.target.value === editField.name) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: editField,
redo: { name: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table field name to ${e.target.value}`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
}}
/>
</Col>
<Col span={8}>
<Select
className="w-full"
2023-09-19 20:51:34 +08:00
optionList={[
...sqlDataTypes.map((value) => ({
2023-09-19 20:50:04 +08:00
label: value,
value: value,
2023-09-19 20:51:34 +08:00
})),
...types.map((type) => ({
label: type.name.toUpperCase(),
value: type.name.toUpperCase(),
})),
]}
2023-09-19 20:50:04 +08:00
filter
value={f.type}
2023-09-19 20:51:17 +08:00
validateStatus={f.type === "" ? "error" : "default"}
2023-09-19 20:50:04 +08:00
placeholder="Type"
onChange={(value) => {
if (value === f.type) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: { type: f.type },
redo: { type: value },
2023-09-19 20:50:52 +08:00
message: `Edit table field type to ${value}`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:43 +08:00
const incr =
f.increment &&
(value === "INT" ||
value === "BIGINT" ||
value === "SMALLINT");
if (value === "ENUM" || value === "SET") {
updateField(i, j, {
type: value,
default: "",
2023-09-19 20:51:26 +08:00
values: f.values ? [...f.values] : [],
increment: incr,
});
} else if (isSized(value) || hasPrecision(value)) {
updateField(i, j, {
type: value,
size: getSize(value),
increment: incr,
});
2023-09-19 20:51:21 +08:00
} else if (
value === "BLOB" ||
value === "JSON" ||
value === "UUID" ||
value === "TEXT" ||
2023-09-19 20:51:21 +08:00
incr
) {
updateField(t.id, j, {
2023-09-19 20:51:21 +08:00
type: value,
increment: incr,
default: "",
size: "",
2023-09-19 20:51:21 +08:00
values: [],
});
2023-09-19 20:51:25 +08:00
} else if (hasCheck(value)) {
updateField(i, j, {
type: value,
check: "",
increment: incr,
});
} else {
updateField(i, j, {
type: value,
increment: incr,
size: "",
values: [],
});
}
2023-09-19 20:50:04 +08:00
}}
></Select>
</Col>
<Col span={3}>
<Button
type={f.notNull ? "primary" : "tertiary"}
title="Nullable"
theme={f.notNull ? "solid" : "light"}
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: { notNull: f.notNull },
redo: { notNull: !f.notNull },
2023-09-19 20:50:52 +08:00
message: `Edit table field to${
f.notNull ? "" : " not"
} null`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
updateField(i, j, { notNull: !f.notNull });
}}
>
?
</Button>
</Col>
<Col span={3}>
<Button
type={f.primary ? "primary" : "tertiary"}
title="Primary"
theme={f.primary ? "solid" : "light"}
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: { primary: f.primary },
redo: { primary: !f.primary },
2023-09-19 20:50:52 +08:00
message: `Edit table field to${
f.primary ? " not" : ""
} primary`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
updateField(i, j, { primary: !f.primary });
}}
icon={<IconKeyStroked />}
></Button>
</Col>
<Col span={3}>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="px-1 w-[240px] popover-theme">
2023-09-19 20:50:04 +08:00
<div className="font-semibold">Default value</div>
<Input
className="my-2"
placeholder="Set default"
value={f.default}
disabled={
f.type === "BLOB" ||
f.type === "JSON" ||
f.type === "TEXT" ||
f.type === "UUID" ||
f.increment
}
2023-09-19 20:50:04 +08:00
onChange={(value) =>
updateField(i, j, { default: value })
}
onFocus={(e) =>
2023-09-19 20:50:15 +08:00
setEditField({ default: e.target.value })
2023-09-19 20:50:04 +08:00
}
onBlur={(e) => {
if (e.target.value === editField.default)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: editField,
redo: { default: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table field default to ${e.target.value}`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
}}
/>
2023-09-19 20:50:44 +08:00
{(f.type === "ENUM" || f.type === "SET") && (
2023-09-19 20:50:43 +08:00
<>
<div className="font-semibold mb-1">
2023-09-19 20:50:44 +08:00
{f.type} values
2023-09-19 20:50:43 +08:00
</div>
<TagInput
separator={[",", ", ", " ,"]}
2023-09-19 20:50:44 +08:00
value={f.values}
2023-09-19 20:51:17 +08:00
validateStatus={
!f.values || f.values.length === 0
? "error"
: "default"
}
2023-09-19 20:50:43 +08:00
className="my-2"
placeholder="Use ',' for batch input"
onChange={(v) =>
2023-09-19 20:50:44 +08:00
updateField(i, j, { values: v })
2023-09-19 20:50:43 +08:00
}
2023-12-16 11:39:13 +08:00
onFocus={() =>
2023-09-19 20:50:44 +08:00
setEditField({ values: f.values })
2023-09-19 20:50:43 +08:00
}
2023-12-16 11:39:13 +08:00
onBlur={() => {
2023-09-19 20:50:43 +08:00
if (
2023-09-19 20:50:44 +08:00
JSON.stringify(editField.values) ===
JSON.stringify(f.values)
2023-09-19 20:50:43 +08:00
)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: i,
fid: j,
undo: editField,
2023-09-19 20:50:44 +08:00
redo: { values: f.values },
2023-09-19 20:50:52 +08:00
message: `Edit table field values to "${JSON.stringify(
f.values
)}"`,
2023-09-19 20:50:43 +08:00
},
]);
setRedoStack([]);
}}
/>
</>
)}
{isSized(f.type) && (
2023-09-19 20:50:43 +08:00
<>
<div className="font-semibold">Size</div>
2023-09-19 20:51:11 +08:00
<InputNumber
className="my-2 w-full"
2023-09-19 20:50:43 +08:00
placeholder="Set length"
value={f.size}
onChange={(value) =>
updateField(i, j, { size: value })
}
onFocus={(e) =>
setEditField({ size: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.size)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: i,
fid: j,
undo: editField,
redo: { size: e.target.value },
message: `Edit table field size to ${e.target.value}`,
},
]);
setRedoStack([]);
}}
/>
</>
)}
{hasPrecision(f.type) && (
<>
<div className="font-semibold">Precision</div>
<Input
className="my-2 w-full"
placeholder="Set precision: (size, d)"
2023-09-19 20:51:17 +08:00
validateStatus={
/^\(\d+,\s*\d+\)$|^$/.test(f.size)
? "default"
: "error"
2023-09-19 20:51:17 +08:00
}
value={f.size}
2023-09-19 20:50:43 +08:00
onChange={(value) =>
updateField(i, j, { size: value })
2023-09-19 20:50:43 +08:00
}
onFocus={(e) =>
setEditField({ size: e.target.value })
2023-09-19 20:50:43 +08:00
}
onBlur={(e) => {
if (e.target.value === editField.size)
2023-09-19 20:50:43 +08:00
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: i,
fid: j,
undo: editField,
redo: { size: e.target.value },
message: `Edit table field precision to ${e.target.value}`,
2023-09-19 20:50:43 +08:00
},
]);
setRedoStack([]);
}}
/>
</>
)}
2023-09-19 20:51:25 +08:00
{hasCheck(f.type) && (
<>
<div className="font-semibold">
Check Expression
</div>
<Input
className="mt-2"
placeholder="Set constraint"
value={f.check}
disabled={f.increment}
onChange={(value) =>
updateField(i, j, { check: value })
}
onFocus={(e) =>
setEditField({ check: e.target.value })
}
onBlur={(e) => {
if (e.target.value === editField.check)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
tid: i,
fid: j,
undo: editField,
redo: { check: e.target.value },
message: `Edit table field check expression to ${e.target.value}`,
},
]);
setRedoStack([]);
}}
/>
<div className="text-xs mt-1">
*This will be in the script as is.
</div>
</>
)}
2023-09-19 20:50:04 +08:00
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
value="unique"
2023-09-19 20:50:45 +08:00
checked={f.unique}
2023-09-19 20:50:04 +08:00
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
[checkedValues.target.value]:
checkedValues.target.checked,
2023-09-19 20:50:04 +08:00
},
},
]);
setRedoStack([]);
updateField(i, j, {
[checkedValues.target.value]:
checkedValues.target.checked,
});
}}
></Checkbox>
</div>
<div className="flex justify-between items-center my-3">
<div className="font-medium">Autoincrement</div>
<Checkbox
value="increment"
2023-09-19 20:50:45 +08:00
checked={f.increment}
disabled={
!(
f.type === "INT" ||
f.type === "BIGINT" ||
f.type === "SMALLINT"
)
}
2023-09-19 20:50:04 +08:00
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
[checkedValues.target.value]:
checkedValues.target.checked,
2023-09-19 20:50:04 +08:00
},
2023-09-19 20:50:52 +08:00
message: `Edit table field to${
f.increment ? " not" : ""
} auto increment`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
updateField(i, j, {
2023-09-19 20:51:25 +08:00
increment: !f.increment,
check: f.increment ? f.check : "",
2023-09-19 20:48:28 +08:00
});
}}
2023-09-19 20:50:04 +08:00
></Checkbox>
2023-09-19 20:48:28 +08:00
</div>
2023-09-19 20:50:04 +08:00
<div className="font-semibold">Comment</div>
<TextArea
className="my-2"
label="Comment"
placeholder="Add comment"
value={f.comment}
autosize
rows={2}
onChange={(value) =>
updateField(i, j, { comment: value })
}
onFocus={(e) =>
2023-09-19 20:50:15 +08:00
setEditField({ comment: e.target.value })
2023-09-19 20:50:04 +08:00
}
onBlur={(e) => {
if (e.target.value === editField.comment)
return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field",
2023-09-19 20:50:15 +08:00
tid: i,
fid: j,
undo: editField,
redo: { comment: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit field comment to "${e.target.value}"`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
}}
/>
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_delete",
tid: t.id,
data: f,
2023-09-19 20:50:52 +08:00
message: `Delete field`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:49 +08:00
setRelationships((prev) =>
prev
.filter(
(e) =>
!(
(e.startTableId === t.id &&
2023-09-19 20:50:49 +08:00
e.startFieldId === j) ||
(e.endTableId === t.id &&
2023-09-19 20:50:49 +08:00
e.endFieldId === j)
)
)
.map((e, i) => ({ ...e, id: i }))
);
2023-12-27 10:45:23 +08:00
setRelationships((prev) => {
return prev.map((e) => {
if (
e.startTableId === t.id &&
e.startFieldId > f.id
) {
return {
...e,
startFieldId: e.startFieldId - 1,
startX: t.x + 15,
startY:
t.y +
(e.startFieldId - 1) * 36 +
50 +
19,
};
}
if (
e.endTableId === t.id &&
e.endFieldId > f.id
) {
return {
...e,
endFieldId: e.endFieldId - 1,
endX: t.x + 15,
endY:
t.y +
(e.endFieldId - 1) * 36 +
50 +
19,
};
}
return e;
});
});
2023-09-19 20:50:15 +08:00
updateTable(i, {
fields: t.fields
.filter((field) => field.id !== j)
.map((e, i) => ({ ...e, id: i })),
});
2023-09-19 20:50:04 +08:00
}}
>
Delete field
</Button>
</div>
}
trigger="click"
position="right"
2023-09-19 20:50:04 +08:00
showArrow
>
<Button type="tertiary" icon={<IconMore />}></Button>
</Popover>
</Col>
</Row>
2023-09-19 20:48:28 +08:00
))}
{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
2023-09-19 20:51:17 +08:00
validateStatus={
idx.fields.length === 0 ? "error" : "default"
}
2023-09-19 20:48:28 +08:00
optionList={t.fields.map((e) => ({
value: e.name,
label: e.name,
}))}
className="w-full"
2023-09-19 20:50:04 +08:00
value={idx.fields}
2023-09-19 20:48:28 +08:00
onChange={(value) => {
2023-09-19 20:50:04 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index",
tid: i,
iid: k,
undo: {
2023-09-19 20:50:15 +08:00
fields: [...idx.fields],
name: `${idx.fields.join("_")}_index`,
2023-09-19 20:50:04 +08:00
},
redo: {
2023-09-19 20:50:15 +08:00
fields: [...value],
name: `${value.join("_")}_index`,
2023-09-19 20:50:04 +08:00
},
2023-09-19 20:50:52 +08:00
message: `Edit index fields to "${JSON.stringify(
value
)}"`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:15 +08:00
updateTable(i, {
indices: t.indices.map((index) =>
index.id === k
? {
...index,
fields: [...value],
name: `${value.join("_")}_index`,
}
: index
),
});
2023-09-19 20:47:55 +08:00
}}
2023-09-19 20:48:28 +08:00
/>
<Popover
content={
2023-09-19 20:51:08 +08:00
<div className="px-1 popover-theme">
2023-09-19 20:50:45 +08:00
<div className="font-semibold mb-1">
Index name:{" "}
</div>
2023-09-19 20:50:04 +08:00
<Input
value={idx.name}
placeholder="Index name"
disabled
/>
2023-09-19 20:50:45 +08:00
<div className="flex justify-between items-center my-3">
<div className="font-medium">Unique</div>
<Checkbox
value="unique"
checked={idx.unique}
onChange={(checkedValues) => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index",
tid: i,
iid: k,
undo: {
[checkedValues.target.value]:
!checkedValues.target.checked,
},
redo: {
[checkedValues.target.value]:
checkedValues.target.checked,
},
2023-09-19 20:50:52 +08:00
message: `Edit table field to${
idx.unique ? " not" : ""
} unique`,
2023-09-19 20:50:45 +08:00
},
]);
setRedoStack([]);
updateTable(i, {
indices: t.indices.map((index) =>
index.id === k
? {
...index,
[checkedValues.target.value]:
checkedValues.target
.checked,
}
: index
),
});
}}
></Checkbox>
</div>
2023-09-19 20:48:28 +08:00
<Button
icon={<IconDeleteStroked />}
type="danger"
block
onClick={() => {
2023-09-19 20:50:04 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index_delete",
tid: t.id,
data: idx,
2023-09-19 20:50:52 +08:00
message: `Delete index`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:15 +08:00
updateTable(i, {
indices: t.indices
.filter((e) => e.id !== k)
.map((e, j) => ({
...e,
id: j,
})),
});
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">
2023-09-19 20:50:04 +08:00
<TextArea
field="comment"
value={t.comment}
autosize
placeholder="Add comment"
rows={1}
2023-09-19 20:50:15 +08:00
onChange={(value) =>
updateTable(i, { comment: value }, false)
}
2023-09-19 20:50:04 +08:00
onFocus={(e) =>
2023-09-19 20:50:15 +08:00
setEditField({ comment: e.target.value })
2023-09-19 20:50:04 +08:00
}
onBlur={(e) => {
if (e.target.value === editField.comment) return;
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
2023-09-19 20:50:52 +08:00
component: "self",
2023-09-19 20:50:15 +08:00
tid: i,
undo: editField,
redo: { comment: e.target.value },
2023-09-19 20:50:52 +08:00
message: `Edit table comment to ${e.target.value}`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
}}
/>
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={
2023-09-19 20:51:08 +08:00
<div className="popover-theme">
2023-09-19 20:48:28 +08:00
<div className="flex justify-between items-center p-2">
<div className="font-medium">Theme</div>
<Button
type="tertiary"
size="small"
2023-09-19 20:50:12 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: i,
undo: { color: t.color },
2024-03-15 22:37:22 +08:00
redo: { color: defaultBlue },
2023-09-19 20:50:52 +08:00
message: `Edit table color to default`,
2023-09-19 20:50:12 +08:00
},
]);
setRedoStack([]);
2024-03-15 22:37:22 +08:00
updateTable(i, { color: defaultBlue });
2023-09-19 20:50:12 +08:00
}}
2023-09-19 20:48:28 +08:00
>
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"
2023-09-19 20:50:12 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: i,
undo: { color: t.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit table color to ${c}`,
2023-09-19 20:50:12 +08:00
},
]);
setRedoStack([]);
updateTable(i, { color: c });
}}
2023-09-19 20:48:28 +08:00
>
{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"
2023-09-19 20:50:12 +08:00
onClick={() => {
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "self",
tid: i,
undo: { color: t.color },
redo: { color: c },
2023-09-19 20:50:52 +08:00
message: `Edit table color to ${c}`,
2023-09-19 20:50:12 +08:00
},
]);
setRedoStack([]);
updateTable(i, { color: c });
}}
2023-09-19 20:48:28 +08:00
>
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:50:04 +08:00
setUndoStack((prev) => [
...prev,
{
action: Action.EDIT,
element: ObjectType.TABLE,
component: "index_add",
tid: t.id,
2023-09-19 20:50:52 +08:00
message: `Add index`,
2023-09-19 20:50:04 +08:00
},
]);
setRedoStack([]);
2023-09-19 20:50:15 +08:00
updateTable(i, {
indices: [
...t.indices,
{
id: t.indices.length,
name: `index_${t.indices.length}`,
2023-09-19 20:50:45 +08:00
unique: false,
2023-09-19 20:50:15 +08:00
fields: [],
},
],
});
2023-09-19 20:48:28 +08:00
}}
>
Add index
</Button>
</Col>
<Col span={6}>
<Button
onClick={() => {
2023-09-19 20:50:04 +08:00
setUndoStack((prev) => [
...prev,
2023-09-19 20:48:28 +08:00
{
2023-09-19 20:50:04 +08:00
action: Action.EDIT,
element: ObjectType.TABLE,
component: "field_add",
tid: t.id,
2023-09-19 20:50:52 +08:00
message: `Add field`,
2023-09-19 20:48:28 +08:00
},
2023-09-19 20:50:04 +08:00
]);
setRedoStack([]);
2023-09-19 20:50:15 +08:00
updateTable(i, {
fields: [
...t.fields,
{
name: "",
type: "",
default: "",
check: "",
primary: false,
unique: false,
notNull: false,
increment: false,
comment: "",
id: t.fields.length,
},
],
});
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: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
}