index.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use client'
  2. import { useCallback, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useParams, usePathname } from 'next/navigation'
  5. import useSWR from 'swr'
  6. import useSWRInfinite from 'swr/infinite'
  7. import { flatten } from 'lodash-es'
  8. import Nav from '../nav'
  9. import { fetchAppDetail, fetchAppList } from '@/service/apps'
  10. import NewAppDialog from '@/app/(commonLayout)/apps/NewAppDialog'
  11. import { Container } from '@/app/components/base/icons/src/vender/line/development'
  12. import { Container as ContainerSolid } from '@/app/components/base/icons/src/vender/solid/development'
  13. import type { AppListResponse } from '@/models/app'
  14. const getKey = (pageIndex: number, previousPageData: AppListResponse) => {
  15. if (!pageIndex || previousPageData.has_more)
  16. return { url: 'apps', params: { page: pageIndex + 1, limit: 30 } }
  17. return null
  18. }
  19. const AppNav = () => {
  20. const { t } = useTranslation()
  21. const [showNewAppDialog, setShowNewAppDialog] = useState(false)
  22. const { appId } = useParams()
  23. const isAppDetailPage = usePathname().split('/').includes('app')
  24. const { data: currentApp } = useSWR((appId && isAppDetailPage) ? { url: '/apps', id: appId } : null, fetchAppDetail)
  25. const { data: appsData, setSize } = useSWRInfinite(appId ? getKey : () => null, fetchAppList, { revalidateFirstPage: false })
  26. const appItems = flatten(appsData?.map(appData => appData.data))
  27. const handleLoadmore = useCallback(() => {
  28. setSize(size => size + 1)
  29. }, [setSize])
  30. return (
  31. <>
  32. <Nav
  33. icon={<Container className='w-4 h-4' />}
  34. activeIcon={<ContainerSolid className='w-4 h-4' />}
  35. text={t('common.menus.apps')}
  36. activeSegment={['apps', 'app']}
  37. link='/apps'
  38. curNav={currentApp}
  39. navs={appItems.map(item => ({
  40. id: item.id,
  41. name: item.name,
  42. link: `/app/${item.id}/overview`,
  43. icon: item.icon,
  44. icon_background: item.icon_background,
  45. }))}
  46. createText={t('common.menus.newApp')}
  47. onCreate={() => setShowNewAppDialog(true)}
  48. onLoadmore={handleLoadmore}
  49. />
  50. <NewAppDialog show={showNewAppDialog} onClose={() => setShowNewAppDialog(false)} />
  51. </>
  52. )
  53. }
  54. export default AppNav