()
| 17 | interface State {} |
| 18 | |
| 19 | const ConnectionSidebar = () => { |
| 20 | const { t } = useTranslation(); |
| 21 | const settingStore = useSettingStore(); |
| 22 | const layoutStore = useLayoutStore(); |
| 23 | const connectionStore = useConnectionStore(); |
| 24 | const conversationStore = useConversationStore(); |
| 25 | const [isRequestingDatabase, setIsRequestingDatabase] = useState<boolean>(false); |
| 26 | const currentConnectionCtx = connectionStore.currentConnectionCtx; |
| 27 | const databaseList = connectionStore.databaseList.filter((database) => database.connectionId === currentConnectionCtx?.connection.id); |
| 28 | const [tableList, updateTableList] = useState<Table[]>([]); |
| 29 | const [schemaList, updateSchemaList] = useState<Schema[]>([]); |
| 30 | const selectedTableNameList: string[] = |
| 31 | conversationStore.getConversationById(conversationStore.currentConversationId)?.selectedTableNameList || []; |
| 32 | const selectedSchemaName: string = |
| 33 | conversationStore.getConversationById(conversationStore.currentConversationId)?.selectedSchemaName || ""; |
| 34 | const tableSchemaLoadingState = useLoading(); |
| 35 | const currentConversation = conversationStore.getConversationById(conversationStore.currentConversationId); |
| 36 | const maxToken = getModel(settingStore.setting.openAIApiConfig?.model || "").max_token; |
| 37 | const [totalToken, setTotalToken] = useState<number>(0); |
| 38 | const hasSchemaProperty: boolean = |
| 39 | currentConnectionCtx?.connection.engineType === Engine.PostgreSQL || currentConnectionCtx?.connection.engineType === Engine.MSSQL; |
| 40 | |
| 41 | useEffect(() => { |
| 42 | const handleWindowResize = () => { |
| 43 | if (window.innerWidth < ResponsiveWidth.sm) { |
| 44 | layoutStore.toggleSidebar(false); |
| 45 | layoutStore.setIsMobileView(true); |
| 46 | } else { |
| 47 | layoutStore.toggleSidebar(true); |
| 48 | layoutStore.setIsMobileView(false); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | handleWindowResize(); |
| 53 | window.addEventListener("resize", handleWindowResize); |
| 54 | |
| 55 | return () => { |
| 56 | window.removeEventListener("resize", handleWindowResize); |
| 57 | }; |
| 58 | }, []); |
| 59 | |
| 60 | useEffect(() => { |
| 61 | // update total token |
| 62 | const totalToken = selectedTableNameList.reduce((totalToken, tableName) => { |
| 63 | const table = tableList.find((table) => table.name === tableName); |
| 64 | // because old cache didn't have token, So the value may is undefined. |
| 65 | return totalToken + (table?.token || countTextTokens(table?.structure || "")); |
| 66 | }, 0); |
| 67 | setTotalToken(totalToken); |
| 68 | }, [selectedTableNameList, tableList]); |
| 69 | |
| 70 | useEffect(() => { |
| 71 | if (currentConnectionCtx?.connection) { |
| 72 | setIsRequestingDatabase(true); |
| 73 | connectionStore.getOrFetchDatabaseList(currentConnectionCtx.connection).finally(() => { |
| 74 | setIsRequestingDatabase(false); |
| 75 | const database = databaseList.find( |
| 76 | (database) => database.name === useConnectionStore.getState().currentConnectionCtx?.database?.name |
nothing calls this directly
no test coverage detected