Database.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Database.js
  2. 'use client'
  3. import { useEffect, useRef } from 'react';
  4. import useSWRInfinite from 'swr/infinite';
  5. import { debounce } from 'lodash-es';
  6. import { useTranslation } from 'react-i18next';
  7. import NewDatabaseCard from './NewDatabaseCard';
  8. import DatasetCard from './DatabaseCard'; // 确保正确导入 DatasetCard 组件
  9. import type { DataSetListResponse } from '@/models/datasets';
  10. import { fetchDatasets } from '@/service/datasets';
  11. import { useAppContext } from '@/context/app-context';
  12. import { RiStackLine } from '@remixicon/react';
  13. import { link } from 'fs';
  14. const getKey = (
  15. pageIndex: number,
  16. previousPageData: DataSetListResponse,
  17. tags: string[],
  18. keyword: string,
  19. ) => {
  20. if (!pageIndex || previousPageData.has_more) {
  21. const params: any = {
  22. url: 'datasets',
  23. params: {
  24. page: pageIndex + 1,
  25. limit: 30,
  26. },
  27. };
  28. if (tags.length)
  29. params.params.tag_ids = tags;
  30. if (keyword)
  31. params.params.keyword = keyword;
  32. return params;
  33. }
  34. return null;
  35. };
  36. type Props = {
  37. containerRef: React.RefObject<HTMLDivElement>,
  38. tags: string[],
  39. keywords: string,
  40. };
  41. // 创建虚拟数据集
  42. const mockDatasets = [
  43. {
  44. id: 'mock-1',
  45. name: '种植数据库',
  46. description: '',
  47. tags: [{ id: '1', name: '' }],
  48. provider: 'internal',
  49. embedding_available: true,
  50. document_count: 500,
  51. word_count: 100000,
  52. app_count: 3,
  53. },
  54. {
  55. id: 'mock-2',
  56. name: '病害数据库',
  57. description: '',
  58. tags: [{ id: '3', name: '' }],
  59. provider: 'internal',
  60. embedding_available: true,
  61. document_count: 300,
  62. word_count: 75000,
  63. app_count: 2,
  64. },
  65. {
  66. id: 'mock-3',
  67. name: '产量数据库',
  68. description: '',
  69. tags: [{ id: '5', name: '' }],
  70. provider: 'internal',
  71. embedding_available: true,
  72. document_count: 400,
  73. word_count: 90000,
  74. app_count: 4,
  75. },
  76. ];
  77. const Database = ({
  78. containerRef,
  79. tags,
  80. keywords,
  81. }: Props) => {
  82. const { isCurrentWorkspaceEditor } = useAppContext();
  83. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  84. (pageIndex: number, previousPageData: DataSetListResponse) => getKey(pageIndex, previousPageData, tags, keywords),
  85. fetchDatasets,
  86. { revalidateFirstPage: false, revalidateAll: true },
  87. );
  88. const loadingStateRef = useRef(false);
  89. const anchorRef = useRef<HTMLAnchorElement>(null);
  90. const { t } = useTranslation();
  91. useEffect(() => {
  92. loadingStateRef.current = isLoading;
  93. document.title = `${t('dataset.database')} - 智脑云平台`;
  94. }, [isLoading]);
  95. useEffect(() => {
  96. const onScroll = debounce(() => {
  97. if (!loadingStateRef.current) {
  98. const { scrollTop, clientHeight } = containerRef.current!;
  99. const anchorOffset = anchorRef.current!.offsetTop;
  100. if (anchorOffset - scrollTop - clientHeight < 100)
  101. setSize(size => size + 1);
  102. }
  103. }, 50);
  104. containerRef.current?.addEventListener('scroll', onScroll);
  105. return () => containerRef.current?.removeEventListener('scroll', onScroll);
  106. }, []);
  107. // Flatten the datasets array and concatenate with mock data if no real data is available yet.
  108. const allDatasets = data ? data.flatMap(({ data: datasets }) => datasets) : [];
  109. const displayedDatasets = mockDatasets.length > 0 ? mockDatasets : mockDatasets;
  110. return (
  111. <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 grow shrink-0'>
  112. {isCurrentWorkspaceEditor && <NewDatabaseCard ref={anchorRef} />}
  113. {displayedDatasets.map(dataset => (
  114. <a href="#">
  115. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate} />
  116. </a>
  117. ))}
  118. </nav>
  119. );
  120. };
  121. export default Database;