Datasets.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use client'
  2. import { useCallback, useEffect, useRef } from 'react'
  3. import useSWRInfinite from 'swr/infinite'
  4. import { debounce } from 'lodash-es'
  5. import { useTranslation } from 'react-i18next'
  6. import NewDatasetCard from './NewDatasetCard'
  7. import DatasetCard from './DatasetCard'
  8. import type { DataSetListResponse, FetchDatasetsParams } from '@/models/datasets'
  9. import { fetchDatasets } from '@/service/datasets'
  10. import { useAppContext } from '@/context/app-context'
  11. const getKey = (
  12. pageIndex: number,
  13. previousPageData: DataSetListResponse,
  14. tags: string[],
  15. keyword: string,
  16. includeAll: boolean,
  17. type: string,
  18. dept: string,
  19. authType: string,
  20. creator: string,
  21. ) => {
  22. if (!pageIndex || previousPageData.has_more) {
  23. const params: FetchDatasetsParams = {
  24. url: 'datasets',
  25. params: {
  26. page: pageIndex + 1,
  27. limit: 30,
  28. include_all: includeAll,
  29. },
  30. }
  31. if (tags.length)
  32. params.params.tag_ids = tags
  33. if (keyword)
  34. params.params.keyword = keyword
  35. if (type)
  36. params.params.category_ids = [type]
  37. if (dept)
  38. params.params.creatorDept = dept
  39. if (authType)
  40. params.params.authType = authType
  41. if (creator)
  42. params.params.creator = creator
  43. return params
  44. }
  45. return null
  46. }
  47. type Props = {
  48. containerRef: React.RefObject<HTMLDivElement>
  49. tags: string[]
  50. keywords: string
  51. includeAll: boolean,
  52. type: string,
  53. dept: string,
  54. authType: string,
  55. creator: string,
  56. }
  57. const Datasets = ({
  58. containerRef,
  59. tags,
  60. keywords,
  61. includeAll,
  62. type,
  63. dept,
  64. authType,
  65. creator,
  66. }: Props) => {
  67. const { isCurrentWorkspaceEditor } = useAppContext()
  68. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  69. (pageIndex: number, previousPageData: DataSetListResponse) => getKey(pageIndex, previousPageData, tags, keywords, includeAll, type, dept, authType, creator),
  70. fetchDatasets,
  71. { revalidateFirstPage: false, revalidateAll: true },
  72. )
  73. const loadingStateRef = useRef(false)
  74. const anchorRef = useRef<HTMLAnchorElement>(null)
  75. const { t } = useTranslation()
  76. useEffect(() => {
  77. loadingStateRef.current = isLoading
  78. document.title = `${t('dataset.knowledge')} - Dify`
  79. }, [isLoading, t])
  80. const onScroll = useCallback(
  81. debounce(() => {
  82. if (!loadingStateRef.current && containerRef.current && anchorRef.current) {
  83. const { scrollTop, clientHeight } = containerRef.current
  84. const anchorOffset = anchorRef.current.offsetTop
  85. if (anchorOffset - scrollTop - clientHeight < 100)
  86. setSize(size => size + 1)
  87. }
  88. }, 50),
  89. [setSize],
  90. )
  91. useEffect(() => {
  92. const currentContainer = containerRef.current
  93. currentContainer?.addEventListener('scroll', onScroll)
  94. return () => {
  95. currentContainer?.removeEventListener('scroll', onScroll)
  96. onScroll.cancel()
  97. }
  98. }, [onScroll])
  99. return (
  100. <nav className='grid shrink-0 grow grid-cols-1 content-start gap-4 px-12 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4'>
  101. { isCurrentWorkspaceEditor && <NewDatasetCard ref={anchorRef} /> }
  102. {data?.map(({ data: datasets }) => datasets.map(dataset => (
  103. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate}/>),
  104. ))}
  105. </nav>
  106. )
  107. }
  108. export default Datasets