import { head } from "lodash-es"; import { useEffect, useState } from "react"; import { createPortal } from "react-dom"; import { useChatStore, useConnectionStore, useLayoutStore } from "@/store"; import { Chat, Connection } from "@/types"; import Icon from "./Icon"; import EngineIcon from "./EngineIcon"; import CreateConnectionModal from "./CreateConnectionModal"; import SettingModal from "./SettingModal"; import EditChatTitleModal from "./EditChatTitleModal"; interface State { showCreateConnectionModal: boolean; showSettingModal: boolean; showEditChatTitleModal: boolean; } const ConnectionSidebar = () => { const layoutStore = useLayoutStore(); const connectionStore = useConnectionStore(); const chatStore = useChatStore(); const [state, setState] = useState({ showCreateConnectionModal: false, showSettingModal: false, showEditChatTitleModal: false, }); const [editConnectionModalContext, setEditConnectionModalContext] = useState(); const [editChatTitleModalContext, setEditChatTitleModalContext] = useState(); const [isRequestingDatabase, setIsRequestingDatabase] = useState(false); const connectionList = connectionStore.connectionList; const currentConnectionCtx = connectionStore.currentConnectionCtx; const databaseList = connectionStore.databaseList.filter((database) => database.connectionId === currentConnectionCtx?.connection.id); const chatList = chatStore.chatList.filter( (chat) => chat.connectionId === currentConnectionCtx?.connection.id && chat.databaseName === currentConnectionCtx?.database?.name ); useEffect(() => { if (currentConnectionCtx?.connection) { setIsRequestingDatabase(true); connectionStore.getOrFetchDatabaseList(currentConnectionCtx.connection).finally(() => { setIsRequestingDatabase(false); }); } else { setIsRequestingDatabase(false); } }, [currentConnectionCtx?.connection]); const toggleCreateConnectionModal = (show = true) => { setState({ ...state, showCreateConnectionModal: show, }); setEditConnectionModalContext(undefined); }; const toggleSettingModal = (show = true) => { setState({ ...state, showSettingModal: show, }); }; const toggleEditChatTitleModal = (show = true) => { setState({ ...state, showEditChatTitleModal: show, }); }; const handleConnectionSelect = async (connection: Connection) => { const databaseList = await connectionStore.getOrFetchDatabaseList(connection); connectionStore.setCurrentConnectionCtx({ connection, database: head(databaseList), }); }; const handleEditConnection = (connection: Connection) => { setState({ ...state, showCreateConnectionModal: true, }); setEditConnectionModalContext(connection); }; const handleDatabaseNameSelect = async (databaseName: string) => { if (!currentConnectionCtx?.connection) { return; } const databaseList = await connectionStore.getOrFetchDatabaseList(currentConnectionCtx.connection); const database = databaseList.find((database) => database.name === databaseName); connectionStore.setCurrentConnectionCtx({ connection: currentConnectionCtx.connection, database: database, }); }; const handleCreateChat = () => { if (!currentConnectionCtx) { chatStore.createChat(); } else { chatStore.createChat(currentConnectionCtx.connection.id, currentConnectionCtx.database?.name); } }; const handleChatSelect = (chat: Chat) => { chatStore.setCurrentChat(chat); layoutStore.toggleSidebar(false); }; const handleEditChatTitle = (chat: Chat) => { setEditChatTitleModalContext(chat); setState({ ...state, showEditChatTitleModal: true, }); }; const handleDeleteChat = (chat: Chat) => { chatStore.clearChat((item) => item.id !== chat.id); if (chatStore.currentChat?.id === chat.id) { chatStore.setCurrentChat(undefined); } }; return ( <> {createPortal( toggleCreateConnectionModal(false)} />, document.body )} {createPortal( toggleSettingModal(false)} />, document.body)} {state.showEditChatTitleModal && editChatTitleModalContext && createPortal( toggleEditChatTitleModal(false)} chat={editChatTitleModalContext} />, document.body)} ); }; export default ConnectionSidebar;