Datasets.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.category_ids = [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. }
  45. const Datasets = ({
  46. containerRef,
  47. tags,
  48. keywords,
  49. includeAll,
  50. type,
  51. }: Props) => {
  52. const { isCurrentWorkspaceEditor } = useAppContext()
  53. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  54. (pageIndex: number, previousPageData: DataSetListResponse) => getKey(pageIndex, previousPageData, tags, keywords, includeAll, type),
  55. fetchDatasets,
  56. { revalidateFirstPage: false, revalidateAll: true },
  57. )
  58. const loadingStateRef = useRef(false)
  59. const anchorRef = useRef<HTMLAnchorElement>(null)
  60. const { t } = useTranslation()
  61. useEffect(() => {
  62. loadingStateRef.current = isLoading
  63. document.title = `${t('dataset.knowledge')} - Dify`
  64. }, [isLoading, t])
  65. const onScroll = useCallback(
  66. debounce(() => {
  67. if (!loadingStateRef.current && containerRef.current && anchorRef.current) {
  68. const { scrollTop, clientHeight } = containerRef.current
  69. const anchorOffset = anchorRef.current.offsetTop
  70. if (anchorOffset - scrollTop - clientHeight < 100)
  71. setSize(size => size + 1)
  72. }
  73. }, 50),
  74. [setSize],
  75. )
  76. useEffect(() => {
  77. const currentContainer = containerRef.current
  78. currentContainer?.addEventListener('scroll', onScroll)
  79. return () => {
  80. currentContainer?.removeEventListener('scroll', onScroll)
  81. onScroll.cancel()
  82. }
  83. }, [onScroll])
  84. return (
  85. <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'>
  86. { isCurrentWorkspaceEditor && <NewDatasetCard ref={anchorRef} /> }
  87. {data?.map(({ data: datasets }) => datasets.map(dataset => (
  88. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate}/>),
  89. ))}
  90. </nav>
  91. )
  92. }
  93. export default Datasets