database.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { apiInterceptors, getDbList, getDbSupportType, postDbDelete } from '@/client/api';
  2. import GPTCard from '@/components/common/gpt-card';
  3. import MuiLoading from '@/components/common/loading';
  4. import FormDialog from '@/components/database/form-dialog';
  5. import ConstructLayout from '@/new-components/layout/Construct';
  6. import { DBOption, DBType, DbListResponse, DbSupportTypeResponse } from '@/types/db';
  7. import { dbMapper } from '@/utils';
  8. import { DeleteFilled, EditFilled, PlusOutlined, SearchOutlined } from '@ant-design/icons';
  9. import { useAsyncEffect } from 'ahooks';
  10. import { Avatar, Badge, Button, Card, Drawer, Empty, Input, Modal, message } from 'antd';
  11. import { useMemo, useState } from 'react';
  12. import { useTranslation } from 'react-i18next';
  13. type DBItem = DbListResponse[0];
  14. export function isFileDb(dbTypeList: DBOption[], dbType: DBType) {
  15. return dbTypeList.find(item => item.value === dbType)?.isFileDb;
  16. }
  17. function Database() {
  18. // const { setCurrentDialogInfo } = useContext(ChatContext); // unused
  19. // const router = useRouter(); // unused
  20. const { t } = useTranslation();
  21. const [dbList, setDbList] = useState<DbListResponse>([]);
  22. const [dbSupportList, setDbSupportList] = useState<DbSupportTypeResponse>([]);
  23. const [loading, setLoading] = useState(false);
  24. const [modal, setModal] = useState<{
  25. open: boolean;
  26. info?: DBItem;
  27. dbType?: DBType;
  28. }>({ open: false });
  29. const [draw, setDraw] = useState<{
  30. open: boolean;
  31. dbList?: DbListResponse;
  32. name?: string;
  33. type?: DBType;
  34. }>({ open: false });
  35. const getDbSupportList = async () => {
  36. const [, data] = await apiInterceptors(getDbSupportType());
  37. setDbSupportList(data ?? []);
  38. };
  39. const refreshDbList = async () => {
  40. setLoading(true);
  41. const [, data] = await apiInterceptors(getDbList());
  42. setDbList(data ?? []);
  43. setLoading(false);
  44. };
  45. const dbTypeList = useMemo(() => {
  46. const supportDbList = dbSupportList.map(item => {
  47. const { db_type, is_file_db } = item;
  48. return { ...dbMapper[db_type], value: db_type, isFileDb: is_file_db };
  49. }) as DBOption[];
  50. const unSupportDbList = Object.keys(dbMapper)
  51. .filter(item => !supportDbList.some(db => db.value === item))
  52. .map(item => ({
  53. ...dbMapper[item],
  54. value: dbMapper[item].label,
  55. disabled: true,
  56. })) as DBOption[];
  57. return [...supportDbList, ...unSupportDbList];
  58. }, [dbSupportList]);
  59. const onModify = (item: DBItem) => {
  60. setModal({ open: true, info: item });
  61. };
  62. const onDelete = (item: DBItem) => {
  63. Modal.confirm({
  64. title: 'Tips',
  65. content: `Do you Want to delete the ${item.db_name}?`,
  66. onOk() {
  67. return new Promise<void>((resolve, reject) => {
  68. handleDelete(item.db_name, resolve, reject);
  69. });
  70. },
  71. });
  72. };
  73. const handleDelete = async (dbName: string, resolve: () => void, reject: () => void) => {
  74. try {
  75. const [err] = await apiInterceptors(postDbDelete(dbName));
  76. if (err) {
  77. message.error(err.message);
  78. reject();
  79. return;
  80. }
  81. message.success('success');
  82. refreshDbList();
  83. resolve();
  84. } catch {
  85. reject();
  86. }
  87. };
  88. const dbListByType = useMemo(() => {
  89. const mapper = dbTypeList.reduce(
  90. (acc, item) => {
  91. acc[item.value] = dbList.filter(dbConn => dbConn.db_type === item.value);
  92. return acc;
  93. },
  94. {} as Record<DBType, DbListResponse>,
  95. );
  96. return mapper;
  97. }, [dbList, dbTypeList]);
  98. useAsyncEffect(async () => {
  99. await refreshDbList();
  100. await getDbSupportList();
  101. }, []);
  102. const handleDbTypeClick = (info: DBOption) => {
  103. const dbItems = dbList.filter(item => item.db_type === info.value);
  104. setDraw({
  105. open: true,
  106. dbList: dbItems,
  107. name: info.label,
  108. type: info.value,
  109. });
  110. };
  111. // TODO: unused function call
  112. // const handleChat = async (item: IChatDbSchema) => {
  113. // const [, data] = await apiInterceptors(
  114. // newDialogue({
  115. // chat_mode: 'chat_with_db_execute',
  116. // }),
  117. // );
  118. // // 知识库对话都默认私有知识库应用下
  119. // if (data?.conv_uid) {
  120. // setCurrentDialogInfo?.({
  121. // chat_scene: data.chat_mode,
  122. // app_code: data.chat_mode,
  123. // });
  124. // localStorage.setItem(
  125. // 'cur_dialog_info',
  126. // JSON.stringify({
  127. // chat_scene: data.chat_mode,
  128. // app_code: data.chat_mode,
  129. // }),
  130. // );
  131. // router.push(`/chat?scene=chat_with_db_execute&id=${data?.conv_uid}&db_name=${item.db_name}`);
  132. // }
  133. // };
  134. return (
  135. <ConstructLayout>
  136. <div className='relative min-h-full overflow-y-auto px-6 max-h-[90vh] bg-[#fff]'>
  137. <MuiLoading visible={loading} />
  138. <div className='mt-2 rounded-[10px] flex h-16 justify-between items-center'>
  139. <Input
  140. variant='filled'
  141. prefix={<SearchOutlined />}
  142. placeholder={t('please_enter_the_keywords')}
  143. allowClear
  144. className='w-[400px] h-[40px]
  145. border-1 border-[#d1d1d1]
  146. backdrop-filter
  147. backdrop-blur-lg
  148. dark:border-[#6f7f95]
  149. dark:bg-[#6f7f95]
  150. dark:bg-opacity-60'
  151. />
  152. <span className='flex gap-2 items-center'>
  153. <Avatar className='bg-gradient-to-tr from-[#31afff] to-[#1677ff] cursor-pointer'>
  154. </Avatar>
  155. <span
  156. >
  157. admin
  158. </span>
  159. </span>
  160. </div>
  161. <div className='flex justify-between items-center mb-6'>
  162. <div className='flex items-center gap-4'>
  163. {/* <Input
  164. variant="filled"
  165. prefix={<SearchOutlined />}
  166. placeholder={t('please_enter_the_keywords')}
  167. // onChange={onSearch}
  168. // onPressEnter={onSearch}
  169. allowClear
  170. 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"
  171. /> */}
  172. </div>
  173. <div className='flex items-center gap-4'>
  174. <Button
  175. className='border-none text-white bg-button-gradient'
  176. onClick={() => {
  177. setModal({ open: true });
  178. }}
  179. >
  180. 添加数据源
  181. </Button>
  182. </div>
  183. </div>
  184. <div className='flex flex-wrap mx-[-8px] gap-2 md:gap-4 rounded-[10px] w-full h-full pb-12 bg-slate-200 p-4 border-1 mb-2 '>
  185. {dbTypeList.map(item => {
  186. return (
  187. <Badge key={item.value} count={dbListByType[item.value]?.length} className='min-h-fit'>
  188. <GPTCard
  189. className='h-full'
  190. title={item.label}
  191. desc={item.desc ?? ''}
  192. disabled={item.disabled}
  193. icon={item.icon}
  194. onClick={() => {
  195. if (item.disabled) return;
  196. handleDbTypeClick(item);
  197. }}
  198. />
  199. </Badge>
  200. // <BlurredCard
  201. // description={item.db_path ?? ''}
  202. // name={item.db_name}
  203. // key={item.db_name}
  204. // logo={targetDBType?.icon}
  205. // RightTop={
  206. // <InnerDropdown
  207. // menu={{
  208. // items: [
  209. // {
  210. // key: 'del',
  211. // label: (
  212. // <span
  213. // className="text-red-400"
  214. // onClick={() => {
  215. // onDelete(item);
  216. // }}
  217. // >
  218. // {t('Delete_Btn')}
  219. // </span>
  220. // ),
  221. // },
  222. // ],
  223. // }}
  224. // />
  225. // }
  226. // rightTopHover={false}
  227. // Tags={
  228. // <div>
  229. // <Tag>{item.db_type}</Tag>
  230. // </div>
  231. // }
  232. // RightBottom={
  233. // <ChatButton
  234. // text={t('start_chat')}
  235. // onClick={() => {
  236. // handleChat(item);
  237. // }}
  238. // />
  239. // }
  240. // onClick={() => {
  241. // // if (targetDBType?.disabled) return;
  242. // // handleDbTypeClick(targetDBType);
  243. // onModify(item);
  244. // }}
  245. // />
  246. );
  247. })}
  248. </div>
  249. <FormDialog
  250. open={modal.open}
  251. dbTypeList={dbTypeList}
  252. choiceDBType={modal.dbType}
  253. editValue={modal.info}
  254. dbNames={dbList.map(item => item.db_name)}
  255. onSuccess={() => {
  256. setModal({ open: false });
  257. refreshDbList();
  258. }}
  259. onClose={() => {
  260. setModal({ open: false });
  261. }}
  262. />
  263. <Drawer
  264. title={draw.name}
  265. placement='right'
  266. onClose={() => {
  267. setDraw({ open: false });
  268. }}
  269. open={draw.open}
  270. >
  271. {draw.type && dbListByType[draw.type] && dbListByType[draw.type].length ? (
  272. <>
  273. <Button
  274. type='primary'
  275. className='mb-4 flex items-center'
  276. icon={<PlusOutlined />}
  277. onClick={() => {
  278. setModal({ open: true, dbType: draw.type });
  279. }}
  280. >
  281. Create
  282. </Button>
  283. {dbListByType[draw.type].map(item => (
  284. <Card
  285. key={item.db_name}
  286. title={item.db_name}
  287. extra={
  288. <>
  289. <EditFilled
  290. className='mr-2'
  291. style={{ color: '#1b7eff' }}
  292. onClick={() => {
  293. onModify(item);
  294. }}
  295. />
  296. <DeleteFilled
  297. style={{ color: '#ff1b2e' }}
  298. onClick={() => {
  299. onDelete(item);
  300. }}
  301. />
  302. </>
  303. }
  304. className='mb-4'
  305. >
  306. {item.db_path ? (
  307. <p>path: {item.db_path}</p>
  308. ) : (
  309. <>
  310. <p>host: {item.db_host}</p>
  311. <p>username: {item.db_user}</p>
  312. <p>port: {item.db_port}</p>
  313. </>
  314. )}
  315. <p>remark: {item.comment}</p>
  316. </Card>
  317. ))}
  318. </>
  319. ) : (
  320. <Empty image={Empty.PRESENTED_IMAGE_DEFAULT}>
  321. <Button
  322. type='primary'
  323. className='flex items-center mx-auto'
  324. icon={<PlusOutlined />}
  325. onClick={() => {
  326. setModal({ open: true, dbType: draw.type });
  327. }}
  328. >
  329. Create Now
  330. </Button>
  331. </Empty>
  332. )}
  333. </Drawer>
  334. </div>
  335. </ConstructLayout>
  336. );
  337. }
  338. export default Database;