Datasets.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ) => {
  20. if (!pageIndex || previousPageData.has_more) {
  21. const params: FetchDatasetsParams = {
  22. url: 'datasets',
  23. params: {
  24. page: pageIndex + 1,
  25. limit: 30,
  26. include_all: includeAll,
  27. },
  28. }
  29. if (tags.length)
  30. params.params.tag_ids = tags
  31. if (keyword)
  32. params.params.keyword = keyword
  33. if (type)
  34. params.params.category_ids = [type]
  35. if (dept)
  36. params.params.dept = dept
  37. return params
  38. }
  39. return null
  40. }
  41. type Props = {
  42. containerRef: React.RefObject<HTMLDivElement>
  43. tags: string[]
  44. keywords: string
  45. includeAll: boolean,
  46. type: string,
  47. dept: string,
  48. }
  49. const Datasets = ({
  50. containerRef,
  51. tags,
  52. keywords,
  53. includeAll,
  54. type,
  55. dept,
  56. }: Props) => {
  57. const { isCurrentWorkspaceEditor } = useAppContext()
  58. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  59. (pageIndex: number, previousPageData: DataSetListResponse) => getKey(pageIndex, previousPageData, tags, keywords, includeAll, type, dept),
  60. fetchDatasets,
  61. { revalidateFirstPage: false, revalidateAll: true },
  62. )
  63. const loadingStateRef = useRef(false)
  64. const anchorRef = useRef<HTMLAnchorElement>(null)
  65. const { t } = useTranslation()
  66. useEffect(() => {
  67. loadingStateRef.current = isLoading
  68. document.title = `${t('dataset.knowledge')} - Dify`
  69. }, [isLoading, t])
  70. const onScroll = useCallback(
  71. debounce(() => {
  72. if (!loadingStateRef.current && containerRef.current && anchorRef.current) {
  73. const { scrollTop, clientHeight } = containerRef.current
  74. const anchorOffset = anchorRef.current.offsetTop
  75. if (anchorOffset - scrollTop - clientHeight < 100)
  76. setSize(size => size + 1)
  77. }
  78. }, 50),
  79. [setSize],
  80. )
  81. useEffect(() => {
  82. const currentContainer = containerRef.current
  83. currentContainer?.addEventListener('scroll', onScroll)
  84. return () => {
  85. currentContainer?.removeEventListener('scroll', onScroll)
  86. onScroll.cancel()
  87. }
  88. }, [onScroll])
  89. return (
  90. <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'>
  91. { isCurrentWorkspaceEditor && <NewDatasetCard ref={anchorRef} /> }
  92. {data?.map(({ data: datasets }) => datasets.map(dataset => (
  93. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate}/>),
  94. ))}
  95. </nav>
  96. )
  97. }
  98. export default Datasets