From 2edadd5045e0ab78713628b1e33b3474421daa14 Mon Sep 17 00:00:00 2001 From: "yassine.belkaid" Date: Thu, 10 Oct 2024 15:49:47 +0300 Subject: [PATCH 1/3] feat: add databases switcher in control panel header --- src/components/EditorHeader/ControlPanel.jsx | 6 ++ .../EditorHeader/DatabasesSwitcher.jsx | 76 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/components/EditorHeader/DatabasesSwitcher.jsx diff --git a/src/components/EditorHeader/ControlPanel.jsx b/src/components/EditorHeader/ControlPanel.jsx index 593e69d6..f9a4937f 100644 --- a/src/components/EditorHeader/ControlPanel.jsx +++ b/src/components/EditorHeader/ControlPanel.jsx @@ -73,6 +73,7 @@ import { jsonToMermaid } from "../../utils/exportAs/mermaid"; import { isRtl } from "../../i18n/utils/rtl"; import { jsonToDocumentation } from "../../utils/exportAs/documentation"; import { IdContext } from "../Workspace"; +import DatabasesSwitcher from "./DatabasesSwitcher"; export default function ControlPanel({ diagramId, @@ -80,6 +81,7 @@ export default function ControlPanel({ title, setTitle, lastSaved, + setLastSaved, }) { const [modal, setModal] = useState(MODAL.NONE); const [sidesheet, setSidesheet] = useState(SIDESHEET.NONE); @@ -1640,6 +1642,10 @@ export default function ControlPanel({ title={databases[database].name + " diagram"} /> )} +
e.isPrimary && setShowEditName(true)} diff --git a/src/components/EditorHeader/DatabasesSwitcher.jsx b/src/components/EditorHeader/DatabasesSwitcher.jsx new file mode 100644 index 00000000..618e2df4 --- /dev/null +++ b/src/components/EditorHeader/DatabasesSwitcher.jsx @@ -0,0 +1,76 @@ +import { Select } from "@douyinfe/semi-ui"; +import { databases } from "../../data/databases"; +import { DB, State } from "../../data/constants"; +import { useDiagram, useSaveState } from "../../hooks"; +import { db } from "../../data/db"; + +const databasesWithoutGeneric = Object.keys(databases).filter(db => databases[db].label !== DB.GENERIC); + +export default function DatabasesSwitcher({ setLastSaved, diagramId }) { + const { database, setDatabase } = useDiagram(); + const { setSaveState } = useSaveState(); + + if (!databases[database] || database === DB.GENERIC) return null; + + const renderOptionItem = renderProps => { + const { + disabled, + selected, + label, + value, + focused, + className, + style, + onMouseEnter, + onClick, + } = renderProps; + const optionCls = [ + 'flex justify-start items-center pl-2 pt-3 cursor-pointer custom-option-render', + focused && 'custom-option-render-focused', + disabled && 'custom-option-render-disabled', + selected && 'custom-option-render-selected', + className, + ].filter(cls => cls).join(' '); + + return ( +
onClick()} onMouseEnter={e => onMouseEnter()}> + {databases[value].image && ( + {databases[value].name + )} +
{label}
+
+ ); + }; + const onChangeHandler = async (selectedDb) => { + await db.diagrams + .update(diagramId, { + database: selectedDb, + }).then(() => { + setSaveState(State.SAVED); + setLastSaved(new Date().toLocaleString()); + setDatabase(selectedDb); + }); + }; + + return