Datasets.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use client'
  2. import { 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])
  60. useEffect(() => {
  61. const onScroll = debounce(() => {
  62. if (!loadingStateRef.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. containerRef.current?.addEventListener('scroll', onScroll)
  70. return () => containerRef.current?.removeEventListener('scroll', onScroll)
  71. }, [])
  72. return (
  73. <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 grow shrink-0'>
  74. { isCurrentWorkspaceEditor && <NewDatasetCard ref={anchorRef} /> }
  75. {data?.map(({ data: datasets }) => datasets.map(dataset => (
  76. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate} />),
  77. ))}
  78. </nav>
  79. )
  80. }
  81. export default Datasets