index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { SimpleTreeView, TreeItem } from '@mui/x-tree-view';
  4. import icons from '@/components/icons/Icons';
  5. import { Tooltip, Typography, Grow, Popover } from '@mui/material';
  6. import { useNavigate } from 'react-router-dom';
  7. import { CollectionObject } from '@server/types';
  8. import clcx from 'clsx';
  9. import { formatNumber } from '@/utils';
  10. import { useStyles } from './style';
  11. import {
  12. DatabaseTreeItem,
  13. TreeNodeType,
  14. DatabaseToolProps,
  15. ContextMenu,
  16. TreeNodeObject,
  17. } from './types';
  18. import { TreeContextMenu } from './TreeContextMenu';
  19. // get expanded nodes from data
  20. const getExpanded = (nodes: DatabaseTreeItem[]) => {
  21. const expanded: string[] = [];
  22. nodes.forEach(node => {
  23. if (node.expanded) {
  24. expanded.push(node.id);
  25. }
  26. if (node.children && node.children.length > 0) {
  27. expanded.push(...getExpanded(node.children));
  28. }
  29. });
  30. return expanded;
  31. };
  32. const CollectionNode: React.FC<{ data: CollectionObject }> = ({ data }) => {
  33. // i18n collectionTrans
  34. const { t: commonTrans } = useTranslation();
  35. // styles
  36. const classes = useStyles();
  37. // class
  38. const loadClass = clcx(classes.dot, {
  39. [classes.loaded]: data.loaded,
  40. [classes.unloaded]: !data.loaded,
  41. [classes.loading]: data.status === 'loading',
  42. [classes.noIndex]: !data.schema || !data.schema.hasVectorIndex,
  43. });
  44. // status tooltip
  45. const hasIndex = data.schema && data.schema.hasVectorIndex;
  46. const loadStatus = hasIndex
  47. ? data.loaded
  48. ? commonTrans('status.loaded')
  49. : commonTrans('status.unloaded')
  50. : commonTrans('status.noVectorIndex');
  51. return (
  52. <div className={classes.collectionNode}>
  53. <div className={classes.collectionName} title={data.collection_name}>
  54. <Typography noWrap className="collectionName">
  55. {data.collection_name}
  56. </Typography>
  57. <span className={classes.count}>
  58. ({formatNumber(data.rowCount || 0)})
  59. </span>
  60. </div>
  61. <Tooltip title={loadStatus}>
  62. <div className={loadClass}></div>
  63. </Tooltip>
  64. </div>
  65. );
  66. };
  67. const DatabaseTree: React.FC<DatabaseToolProps> = props => {
  68. // state
  69. const [contextMenu, setContextMenu] = useState<ContextMenu | null>(null);
  70. // props
  71. const { database, collections, params } = props;
  72. // format tree data
  73. const children = collections.map(c => {
  74. return {
  75. id: `c_${c.collection_name}`,
  76. name: c.collection_name,
  77. type: 'collection' as TreeNodeType,
  78. data: c,
  79. };
  80. });
  81. // tree data
  82. const tree: DatabaseTreeItem = {
  83. id: database,
  84. name: database,
  85. expanded: children.length > 0,
  86. type: 'db',
  87. children: children,
  88. };
  89. // Icons
  90. const DatabaseIcon = icons.database;
  91. const CollectionIcon = icons.navCollection;
  92. // hooks
  93. const navigate = useNavigate();
  94. const classes = useStyles();
  95. // on node click
  96. const onNodeClick = (node: DatabaseTreeItem) => {
  97. navigate(
  98. node.type === 'db'
  99. ? `/databases/${database}/${params.databasePage || 'collections'}`
  100. : `/databases/${database}/${node.name}/${
  101. params.collectionPage || 'schema'
  102. }`
  103. );
  104. // close context menu
  105. setContextMenu(null);
  106. };
  107. const handleContextMenu = (
  108. event: any,
  109. nodeId: string,
  110. nodeType: string,
  111. object: TreeNodeObject
  112. ) => {
  113. // prevent default
  114. event.preventDefault();
  115. event.stopPropagation();
  116. setContextMenu({
  117. mouseX: event.clientX - 2,
  118. mouseY: event.clientY - 4,
  119. nodeId,
  120. nodeType: nodeType as TreeNodeType,
  121. object: object,
  122. });
  123. };
  124. const handleClose = () => {
  125. setContextMenu(null);
  126. };
  127. // render children
  128. const renderTree = (nodes: DatabaseTreeItem[]) => {
  129. return nodes.map(node => {
  130. if (node.children && node.children.length > 0) {
  131. return (
  132. <TreeItem
  133. key={node.id}
  134. itemId={node.id}
  135. slots={{
  136. icon: CollectionIcon,
  137. }}
  138. label={node.name}
  139. className={clcx(classes.treeItem, {
  140. ['right-selected-on']: contextMenu?.nodeId === node.id,
  141. })}
  142. onClick={(event: any) => {
  143. event.stopPropagation();
  144. if (onNodeClick) {
  145. onNodeClick(node);
  146. }
  147. }}
  148. >
  149. {renderTree(node.children)}
  150. </TreeItem>
  151. );
  152. }
  153. return (
  154. <TreeItem
  155. key={node.id}
  156. itemId={node.id}
  157. slots={{
  158. icon: CollectionIcon,
  159. }}
  160. label={<CollectionNode data={node.data!} />}
  161. onContextMenu={event =>
  162. handleContextMenu(event, node.id, node.type, node.data!)
  163. }
  164. className={clcx(classes.treeItem, {
  165. ['right-selected-on']: contextMenu?.nodeId === node.id,
  166. })}
  167. onClick={(event: any) => {
  168. event.stopPropagation();
  169. if (onNodeClick) {
  170. onNodeClick(node);
  171. }
  172. }}
  173. />
  174. );
  175. });
  176. };
  177. // useEffect
  178. useEffect(() => {
  179. // register click event on document, close context menu if click outside
  180. document.addEventListener('click', handleClose);
  181. return () => {
  182. document.removeEventListener('click', handleClose);
  183. };
  184. }, []);
  185. return (
  186. <>
  187. <SimpleTreeView
  188. expandedItems={[database]}
  189. multiSelect={false}
  190. disableSelection={false}
  191. selectedItems={
  192. params.collectionName
  193. ? `c_${params.collectionName}`
  194. : params.databaseName
  195. }
  196. className={classes.root}
  197. >
  198. {
  199. <TreeItem
  200. key={tree.id}
  201. itemId={tree.id}
  202. title={tree.name}
  203. label={
  204. <Typography noWrap className={classes.dbName}>
  205. {tree.name}
  206. </Typography>
  207. }
  208. className={classes.treeItem}
  209. slots={{
  210. icon: DatabaseIcon,
  211. }}
  212. onClick={(event: any) => {
  213. event.stopPropagation();
  214. if (onNodeClick) {
  215. onNodeClick(tree);
  216. }
  217. }}
  218. onContextMenu={event =>
  219. handleContextMenu(event, tree.id, tree.type, null)
  220. }
  221. >
  222. {tree.children && tree.children.length > 0
  223. ? renderTree(tree.children)
  224. : [<div key="stub" />]}
  225. </TreeItem>
  226. }
  227. </SimpleTreeView>
  228. <Popover
  229. open={Boolean(contextMenu)}
  230. onClose={handleClose}
  231. anchorReference="anchorPosition"
  232. anchorPosition={
  233. contextMenu !== null
  234. ? { top: contextMenu.mouseY, left: contextMenu.mouseX }
  235. : undefined
  236. }
  237. TransitionComponent={Grow}
  238. transitionDuration={0}
  239. sx={{ pointerEvents: 'none' }}
  240. PaperProps={{
  241. sx: { pointerEvents: 'auto', boxShadow: 4, borderRadius: 2 },
  242. }}
  243. >
  244. <TreeContextMenu onClick={handleClose} contextMenu={contextMenu!} />
  245. </Popover>
  246. </>
  247. );
  248. };
  249. export default DatabaseTree;