import { React, useContext, 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, IconDeleteStroked, IconCheckboxTick, IconPlus, IconSearch, } from "@douyinfe/semi-icons"; import { IllustrationNoContent, IllustrationNoContentDark, } from "@douyinfe/semi-illustrations"; import { TableContext } from "../pages/editor"; export default function TableOverview(props) { const [indexActiveKey, setIndexActiveKey] = useState(""); const [value, setValue] = useState(""); const { tables, setTables } = useContext(TableContext); const [filteredResult, setFilteredResult] = useState( tables.map((t) => { return t.name; }) ); const handleStringSearch = (value) => { setFilteredResult( tables .map((t) => { return t.name; }) .filter((i) => i.includes(value)) ); }; const updatedField = (tid, fid, updatedValues) => { 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) => { 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 } = tables.find((t) => t.name === v); props.setSelectedTable(`${id}`); document .getElementById(`scroll_table_${id}`) .scrollIntoView({ behavior: "smooth" }); }} className="w-full" /> } block onClick={() => { const id = tables.length === 0 ? 0 : tables[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, }; setTables((prev) => [...prev, newTable]); }} > Add table props.setSelectedTable(k)} accordion > {tables.length <= 0 ? ( } darkModeImage={ } title="No tables" description="Start building your diagram!" /> ) : ( tables.map((t, i) => ( {t.name}} itemKey={`${t.id}`}> {t.fields.map((f, j) => ( updatedField(i, j, value.values)} > { return { label: value, value: value, }; })} filter initValue={f.type} placeholder="Type" > updatedField(i, j, { notNull: !f.notNull }) } > ? updatedField(i, j, { primary: !f.primary }) } icon={} > updatedField(i, j, value.values) } > Unique updatedField(i, j, { [checkedValues.target.value]: checkedValues.target.checked, }) } > Autoincrement updatedField(i, j, { [checkedValues.target.value]: checkedValues.target.checked, }) } > } type="danger" block onClick={(ev) => { 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)} accordion > {t.indices.map((idx, k) => ( ({ value: e.name, label: e.name, }))} className="w-full" defaultValue={idx.fields} onChange={(value) => { const updatedTables = [...tables]; const updatedIndices = [...t.indices]; updatedIndices[k] = { name: `${value.join("_")}_index`, fields: [...value], }; updatedTables[i] = { ...t, indices: [...updatedIndices], }; setTables(updatedTables); }} /> } type="danger" block onClick={() => { const updatedTables = [...tables]; const updatedIndices = [...t.indices]; updatedIndices.splice(k, 1); updatedTables[i] = { ...t, indices: [...updatedIndices], }; setTables(updatedTables); }} > Delete } trigger="click" position="rightTop" showArrow > } type="tertiary" style={{ marginLeft: "12px" }} > ))} )} updateTable(i, value.values)}> updateTable(i, { comment: "" })} initValue={t.comment} autosize placeholder="Add comment" rows={1} /> Theme updateTable(i, { color: defaultTableTheme }) } > Clear {tableThemes .slice(0, Math.ceil(tableThemes.length / 2)) .map((c) => ( updateTable(i, { color: c })} > {t.color === c ? ( ) : ( )} ))} {tableThemes .slice(Math.ceil(tableThemes.length / 2)) .map((c) => ( updateTable(i, { color: c })} > ))} } trigger="click" position="bottomLeft" showArrow > { setIndexActiveKey("1"); const updatedTables = [...tables]; updatedTables[i] = { ...t, indices: [ ...t.indices, { name: `index_${t.indices.length}`, fields: [] }, ], }; setTables(updatedTables); }} > Add index { const updatedTables = [...tables]; updatedTables[i].fields = [ ...updatedTables[i].fields, { name: "", type: "", default: "", primary: false, unique: false, notNull: false, increment: false, comment: "", }, ]; setTables(updatedTables); }} block > Add field } type="danger" onClick={() => { Toast.success(`Table deleted!`); setTables((prev) => prev .filter((e) => e.id !== i) .map((e, idx) => ({ ...e, id: idx })) ); props.setSelectedTable(""); }} > )) )} > ); }