index.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use client'
  2. import { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useParams, useRouter } from 'next/navigation'
  5. import {
  6. RiBookFill,
  7. RiBookLine,
  8. } from '@remixicon/react'
  9. import useSWR from 'swr'
  10. import useSWRInfinite from 'swr/infinite'
  11. import { flatten } from 'lodash-es'
  12. import Nav from '../nav'
  13. import type { NavItem } from '../nav/nav-selector'
  14. import { fetchDatasetDetail, fetchDatasets } from '@/service/datasets'
  15. import type { DataSetListResponse } from '@/models/datasets'
  16. const getKey = (pageIndex: number, previousPageData: DataSetListResponse) => {
  17. if (!pageIndex || previousPageData.has_more)
  18. return { url: 'datasets', params: { page: pageIndex + 1, limit: 30 } }
  19. return null
  20. }
  21. const DatasetNav = () => {
  22. const { t } = useTranslation()
  23. const router = useRouter()
  24. const { datasetId } = useParams()
  25. const { data: currentDataset } = useSWR(
  26. datasetId
  27. ? {
  28. url: 'fetchDatasetDetail',
  29. datasetId,
  30. }
  31. : null,
  32. apiParams => fetchDatasetDetail(apiParams.datasetId as string))
  33. const { data: datasetsData, setSize } = useSWRInfinite(datasetId ? getKey : () => null, fetchDatasets, { revalidateFirstPage: false, revalidateAll: true })
  34. const datasetItems = flatten(datasetsData?.map(datasetData => datasetData.data))
  35. const handleLoadmore = useCallback(() => {
  36. setSize(size => size + 1)
  37. }, [setSize])
  38. return (
  39. <Nav
  40. icon={<RiBookFill className='w-4 h-4' />}
  41. activeIcon={<RiBookLine className='w-4 h-4' />}
  42. text={t('common.menus.datasets')}
  43. activeSegment='datasets'
  44. link='/datasets'
  45. curNav={currentDataset as Omit<NavItem, 'link'>}
  46. navs={datasetItems.map(dataset => ({
  47. id: dataset.id,
  48. name: dataset.name,
  49. link: dataset.provider === 'external' ? `/datasets/${dataset.id}/hitTesting` : `/datasets/${dataset.id}/documents`,
  50. icon: dataset.icon,
  51. icon_background: dataset.icon_background,
  52. })) as NavItem[]}
  53. createText={t('common.menus.newDataset')}
  54. onCreate={() => router.push('/datasets/create')}
  55. onLoadmore={handleLoadmore}
  56. />
  57. )
  58. }
  59. export default DatasetNav