index.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useParams } 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 { data: currentApp } = useSWR(appId ? { url: '/apps', id: appId } : null, fetchAppDetail)
  23. const { data: appsData, setSize } = useSWRInfinite(appId ? getKey : () => null, fetchAppList, { revalidateFirstPage: false })
  24. const appItems = flatten(appsData?.map(appData => appData.data))
  25. const handleLoadmore = useCallback(() => {
  26. setSize(size => size + 1)
  27. }, [setSize])
  28. return (
  29. <>
  30. <Nav
  31. icon={<Container className='w-4 h-4' />}
  32. activeIcon={<ContainerSolid className='w-4 h-4' />}
  33. text={t('common.menus.apps')}
  34. activeSegment={['apps', 'app']}
  35. link='/apps'
  36. curNav={currentApp}
  37. navs={appItems.map(item => ({
  38. id: item.id,
  39. name: item.name,
  40. link: `/app/${item.id}/overview`,
  41. icon: item.icon,
  42. icon_background: item.icon_background,
  43. }))}
  44. createText={t('common.menus.newApp')}
  45. onCreate={() => setShowNewAppDialog(true)}
  46. onLoadmore={handleLoadmore}
  47. />
  48. <NewAppDialog show={showNewAppDialog} onClose={() => setShowNewAppDialog(false)} />
  49. </>
  50. )
  51. }
  52. export default AppNav