Datasets.tsx 2.9 KB

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