import { apiInterceptors, getDbList, getDbSupportType, postDbDelete } from '@/client/api'; import GPTCard from '@/components/common/gpt-card'; import MuiLoading from '@/components/common/loading'; import FormDialog from '@/components/database/form-dialog'; import ConstructLayout from '@/new-components/layout/Construct'; import { DBOption, DBType, DbListResponse, DbSupportTypeResponse } from '@/types/db'; import { dbMapper } from '@/utils'; import { DeleteFilled, EditFilled, PlusOutlined } from '@ant-design/icons'; import { useAsyncEffect } from 'ahooks'; import { Badge, Button, Card, Drawer, Empty, Modal, message } from 'antd'; import { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; type DBItem = DbListResponse[0]; export function isFileDb(dbTypeList: DBOption[], dbType: DBType) { return dbTypeList.find(item => item.value === dbType)?.isFileDb; } function Database() { // const { setCurrentDialogInfo } = useContext(ChatContext); // unused // const router = useRouter(); // unused const { t } = useTranslation(); const [dbList, setDbList] = useState([]); const [dbSupportList, setDbSupportList] = useState([]); const [loading, setLoading] = useState(false); const [modal, setModal] = useState<{ open: boolean; info?: DBItem; dbType?: DBType; }>({ open: false }); const [draw, setDraw] = useState<{ open: boolean; dbList?: DbListResponse; name?: string; type?: DBType; }>({ open: false }); const getDbSupportList = async () => { const [, data] = await apiInterceptors(getDbSupportType()); setDbSupportList(data ?? []); }; const refreshDbList = async () => { setLoading(true); const [, data] = await apiInterceptors(getDbList()); setDbList(data ?? []); setLoading(false); }; const dbTypeList = useMemo(() => { const supportDbList = dbSupportList.map(item => { const { db_type, is_file_db } = item; return { ...dbMapper[db_type], value: db_type, isFileDb: is_file_db }; }) as DBOption[]; const unSupportDbList = Object.keys(dbMapper) .filter(item => !supportDbList.some(db => db.value === item)) .map(item => ({ ...dbMapper[item], value: dbMapper[item].label, disabled: true, })) as DBOption[]; return [...supportDbList, ...unSupportDbList]; }, [dbSupportList]); const onModify = (item: DBItem) => { setModal({ open: true, info: item }); }; const onDelete = (item: DBItem) => { Modal.confirm({ title: 'Tips', content: `Do you Want to delete the ${item.db_name}?`, onOk() { return new Promise((resolve, reject) => { handleDelete(item.db_name, resolve, reject); }); }, }); }; const handleDelete = async (dbName: string, resolve: () => void, reject: () => void) => { try { const [err] = await apiInterceptors(postDbDelete(dbName)); if (err) { message.error(err.message); reject(); return; } message.success('success'); refreshDbList(); resolve(); } catch { reject(); } }; const dbListByType = useMemo(() => { const mapper = dbTypeList.reduce( (acc, item) => { acc[item.value] = dbList.filter(dbConn => dbConn.db_type === item.value); return acc; }, {} as Record, ); return mapper; }, [dbList, dbTypeList]); useAsyncEffect(async () => { await refreshDbList(); await getDbSupportList(); }, []); const handleDbTypeClick = (info: DBOption) => { const dbItems = dbList.filter(item => item.db_type === info.value); setDraw({ open: true, dbList: dbItems, name: info.label, type: info.value, }); }; // TODO: unused function call // const handleChat = async (item: IChatDbSchema) => { // const [, data] = await apiInterceptors( // newDialogue({ // chat_mode: 'chat_with_db_execute', // }), // ); // // 知识库对话都默认私有知识库应用下 // if (data?.conv_uid) { // setCurrentDialogInfo?.({ // chat_scene: data.chat_mode, // app_code: data.chat_mode, // }); // localStorage.setItem( // 'cur_dialog_info', // JSON.stringify({ // chat_scene: data.chat_mode, // app_code: data.chat_mode, // }), // ); // router.push(`/chat?scene=chat_with_db_execute&id=${data?.conv_uid}&db_name=${item.db_name}`); // } // }; return (
{/* } placeholder={t('please_enter_the_keywords')} // onChange={onSearch} // onPressEnter={onSearch} allowClear className="w-[230px] h-[40px] border-1 border-white backdrop-filter backdrop-blur-lg bg-white bg-opacity-30 dark:border-[#6f7f95] dark:bg-[#6f7f95] dark:bg-opacity-60" /> */}
{dbTypeList.map(item => { return ( { if (item.disabled) return; handleDbTypeClick(item); }} /> // { // onDelete(item); // }} // > // {t('Delete_Btn')} // // ), // }, // ], // }} // /> // } // rightTopHover={false} // Tags={ //
// {item.db_type} //
// } // RightBottom={ // { // handleChat(item); // }} // /> // } // onClick={() => { // // if (targetDBType?.disabled) return; // // handleDbTypeClick(targetDBType); // onModify(item); // }} // /> ); })}
item.db_name)} onSuccess={() => { setModal({ open: false }); refreshDbList(); }} onClose={() => { setModal({ open: false }); }} /> { setDraw({ open: false }); }} open={draw.open} > {draw.type && dbListByType[draw.type] && dbListByType[draw.type].length ? ( <> {dbListByType[draw.type].map(item => ( { onModify(item); }} /> { onDelete(item); }} /> } className='mb-4' > {item.db_path ? (

path: {item.db_path}

) : ( <>

host: {item.db_host}

username: {item.db_user}

port: {item.db_port}

)}

remark: {item.comment}

))} ) : ( )}
); } export default Database;