import { React, useState } from "react";
import { defaultTableTheme, sqlDataTypes, tableThemes } from "../data/data";
import {
Collapse,
Row,
Col,
Form,
Button,
Card,
Popover,
Checkbox,
Select,
AutoComplete,
Toast,
Empty,
} from "@douyinfe/semi-ui";
import {
IconMore,
IconKeyStroked,
IconColorPalette,
IconDeleteStroked,
IconCheckboxTick,
IconPlus,
IconSearch,
} from "@douyinfe/semi-icons";
import {
IllustrationNoContent,
IllustrationNoContentDark,
} from "@douyinfe/semi-illustrations";
export default function TableOverview(props) {
const [indexActiveKey, setIndexActiveKey] = useState("");
const [tableActiveKey, setTableActiveKey] = useState("");
const [value, setValue] = useState("");
const [filteredResult, setFilteredResult] = useState(
props.tables.map((t) => {
return t.name;
})
);
const handleStringSearch = (value) => {
setFilteredResult(
props.tables
.map((t) => {
return t.name;
})
.filter((i) => i.includes(value))
);
};
const updatedField = (tid, fid, updatedValues) => {
props.setTables((prev) =>
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) => {
props.setTables((prev) =>
prev.map((table, i) => {
if (tid === i) {
return {
...table,
...updatedValues,
};
}
return table;
})
);
};
return (
<>
}
placeholder="Search..."
emptyContent={No tables found
}
onSearch={(v) => handleStringSearch(v)}
onChange={(v) => setValue(v)}
onSelect={(v) => {
const { id, name } = props.tables.find((t) => t.name === v);
setTableActiveKey(`${id}`);
document
.getElementById(`${name}_scroll_id`)
.scrollIntoView({ behavior: "smooth" });
}}
className="w-full"
/>
}
block
onClick={() => {
const id =
props.tables.length === 0
? 0
: props.tables[props.tables.length - 1].id + 1;
const newTable = {
id: id,
name: `table_${id}`,
x: 0,
y: 0,
fields: [
{
name: "id",
type: "UUID",
default: "",
check: "",
primary: true,
unique: true,
notNull: true,
increment: true,
comment: "",
},
],
comment: "",
indices: [],
color: defaultTableTheme,
};
props.setTables((prev) => [...prev, newTable]);
}}
>
Add table
setTableActiveKey(k)}
accordion
>
{props.tables.length <= 0 ? (
}
darkModeImage={
}
title="No tables"
description="Start building your diagram!"
/>
) : (
props.tables.map((t, i) => (
{t.name}
} itemKey={`${t.id}`}>
{t.fields.map((f, j) => (
}
type="danger"
block
onClick={(ev) => {
props.setTables((prev) => {
const updatedTables = [...prev];
const updatedFields = [
...updatedTables[t.id].fields,
];
updatedFields.splice(j, 1);
updatedTables[t.id].fields = [
...updatedFields,
];
return updatedTables;
});
}}
>
Delete
}
trigger="click"
position="rightTop"
showArrow
>
}>
))}
{t.indices.length > 0 && (
setIndexActiveKey(itemKey)}
>
{t.indices.map((idx, k) => (
}
trigger="click"
position="rightTop"
showArrow
>
}
type="tertiary"
style={{ marginLeft: "12px" }}
>
))}
)}
updateTable(i, { comment: "" })}
initValue={t.comment}
autosize
placeholder="Add comment"
rows={1}
/>
Theme
{tableThemes
.slice(0, Math.ceil(tableThemes.length / 2))
.map((c) => (
))}
{tableThemes
.slice(Math.ceil(tableThemes.length / 2))
.map((c) => (
))}
}
trigger="click"
position="bottomLeft"
showArrow
>
}
>
}
type="danger"
onClick={() => {
Toast.success(`Table deleted!`);
props.setTables((prev) =>
prev
.filter((e) => e.id !== i)
.map((e, idx) => ({ ...e, id: idx }))
);
setTableActiveKey("");
}}
>
))
)}
>
);
}