Datasets.tsx 2.8 KB

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