index.tsx 2.2 KB

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