Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Datasets.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use client'
  2. import { useCallback, useEffect, useRef } from 'react'
  3. import useSWRInfinite from 'swr/infinite'
  4. import { debounce } from 'lodash-es'
  5. import NewDatasetCard from './NewDatasetCard'
  6. import DatasetCard from './DatasetCard'
  7. import type { DataSetListResponse, FetchDatasetsParams } from '@/models/datasets'
  8. import { fetchDatasets } from '@/service/datasets'
  9. import { useAppContext } from '@/context/app-context'
  10. import { useTranslation } from 'react-i18next'
  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 { t } = useTranslation()
  48. const { isCurrentWorkspaceEditor } = useAppContext()
  49. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  50. (pageIndex: number, previousPageData: DataSetListResponse) => getKey(pageIndex, previousPageData, tags, keywords, includeAll),
  51. fetchDatasets,
  52. { revalidateFirstPage: false, revalidateAll: true },
  53. )
  54. const loadingStateRef = useRef(false)
  55. const anchorRef = useRef<HTMLAnchorElement>(null)
  56. useEffect(() => {
  57. loadingStateRef.current = isLoading
  58. }, [isLoading, t])
  59. const onScroll = useCallback(
  60. debounce(() => {
  61. if (!loadingStateRef.current && containerRef.current && anchorRef.current) {
  62. const { scrollTop, clientHeight } = containerRef.current
  63. const anchorOffset = anchorRef.current.offsetTop
  64. if (anchorOffset - scrollTop - clientHeight < 100)
  65. setSize(size => size + 1)
  66. }
  67. }, 50),
  68. [setSize],
  69. )
  70. useEffect(() => {
  71. const currentContainer = containerRef.current
  72. currentContainer?.addEventListener('scroll', onScroll)
  73. return () => {
  74. currentContainer?.removeEventListener('scroll', onScroll)
  75. onScroll.cancel()
  76. }
  77. }, [onScroll])
  78. return (
  79. <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'>
  80. {isCurrentWorkspaceEditor && <NewDatasetCard ref={anchorRef} />}
  81. {data?.map(({ data: datasets }) => datasets.map(dataset => (
  82. <DatasetCard key={dataset.id} dataset={dataset} onSuccess={mutate} />),
  83. ))}
  84. </nav>
  85. )
  86. }
  87. export default Datasets