From fa16f8fd484a11ded91c3195582952bef78f3e2a Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Fri, 5 May 2023 16:46:29 +0800 Subject: [PATCH] feat: add a table select to choose a table as prompt (#74) --- src/components/ConnectionSidebar.tsx | 81 ++++++++++++++++++++++- src/components/ConversationView/index.tsx | 15 ++++- src/hooks/useLoading.ts | 36 ++++++++++ src/locales/en.json | 2 + src/locales/zh-Hant.json | 2 + src/locales/zh.json | 2 + src/store/conversation.ts | 11 +++ src/types/conversation.ts | 1 + 8 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 src/hooks/useLoading.ts diff --git a/src/components/ConnectionSidebar.tsx b/src/components/ConnectionSidebar.tsx index feadf7e..a37f634 100644 --- a/src/components/ConnectionSidebar.tsx +++ b/src/components/ConnectionSidebar.tsx @@ -1,7 +1,14 @@ import { Drawer } from "@mui/material"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; -import { useConnectionStore, useLayoutStore, ResponsiveWidth } from "@/store"; +import { + useConnectionStore, + useConversationStore, + useLayoutStore, + ResponsiveWidth, +} from "@/store"; +import { Table } from "@/types"; +import useLoading from "@/hooks/useLoading"; import Link from "next/link"; import Select from "./kit/Select"; import Tooltip from "./kit/Tooltip"; @@ -16,12 +23,15 @@ const ConnectionSidebar = () => { const { t } = useTranslation(); const layoutStore = useLayoutStore(); const connectionStore = useConnectionStore(); + const conversationStore = useConversationStore(); const [isRequestingDatabase, setIsRequestingDatabase] = useState(false); const currentConnectionCtx = connectionStore.currentConnectionCtx; const databaseList = connectionStore.databaseList.filter( (database) => database.connectionId === currentConnectionCtx?.connection.id ); + const [tableList, updateTableList] = useState([]); + const tableSchemaLoadingState = useLoading(); useEffect(() => { const handleWindowResize = () => { @@ -49,12 +59,40 @@ const ConnectionSidebar = () => { .getOrFetchDatabaseList(currentConnectionCtx.connection) .finally(() => { setIsRequestingDatabase(false); + const database = databaseList.find( + (database) => + database.name === + useConnectionStore.getState().currentConnectionCtx?.database?.name + ); + if (database) { + tableSchemaLoadingState.setLoading(); + connectionStore.getOrFetchDatabaseSchema(database).then(() => { + tableSchemaLoadingState.setFinish(); + }); + } }); } else { setIsRequestingDatabase(false); } }, [currentConnectionCtx?.connection]); + useEffect(() => { + const tableList = + connectionStore.databaseList.find( + (database) => + database.connectionId === currentConnectionCtx?.connection.id && + database.name === currentConnectionCtx?.database?.name + )?.tableList || []; + + updateTableList([ + { + name: "", + structure: "", + } as Table, + ...tableList, + ]); + }, [connectionStore, currentConnectionCtx]); + const handleDatabaseNameSelect = async (databaseName: string) => { if (!currentConnectionCtx?.connection) { return; @@ -70,8 +108,17 @@ const ConnectionSidebar = () => { connection: currentConnectionCtx.connection, database: database, }); + if (database) { + tableSchemaLoadingState.setLoading(); + connectionStore.getOrFetchDatabaseSchema(database).then(() => { + tableSchemaLoadingState.setFinish(); + }); + } }; + const handleTableNameSelect = async (tableName: string) => { + conversationStore.updateTableName(tableName); + }; return ( <> { /> )} + {tableSchemaLoadingState.isLoading ? ( +
+ {" "} + {t("common.loading")} +
+ ) : ( + tableList.length > 0 && ( +
+