index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use client'
  2. import { useCallback, useEffect } from 'react'
  3. import Link from 'next/link'
  4. import { useBoolean } from 'ahooks'
  5. import { useSelectedLayoutSegment } from 'next/navigation'
  6. import { Bars3Icon } from '@heroicons/react/20/solid'
  7. import { useContextSelector } from 'use-context-selector'
  8. import HeaderBillingBtn from '../billing/header-billing-btn'
  9. import AccountDropdown from './account-dropdown'
  10. import AppNav from './app-nav'
  11. import DatasetNav from './dataset-nav'
  12. import EnvNav from './env-nav'
  13. import ExploreNav from './explore-nav'
  14. import ToolsNav from './tools-nav'
  15. import GithubStar from './github-star'
  16. import LicenseNav from './license-env'
  17. import { WorkspaceProvider } from '@/context/workspace-context'
  18. import AppContext, { useAppContext } from '@/context/app-context'
  19. import LogoSite from '@/app/components/base/logo/logo-site'
  20. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  21. import { useProviderContext } from '@/context/provider-context'
  22. import { useModalContext } from '@/context/modal-context'
  23. import { LicenseStatus } from '@/types/feature'
  24. const navClassName = `
  25. flex items-center relative mr-0 sm:mr-3 px-3 h-8 rounded-xl
  26. font-medium text-sm
  27. cursor-pointer
  28. `
  29. const Header = () => {
  30. const { isCurrentWorkspaceEditor, isCurrentWorkspaceDatasetOperator } = useAppContext()
  31. const systemFeatures = useContextSelector(AppContext, v => v.systemFeatures)
  32. const selectedSegment = useSelectedLayoutSegment()
  33. const media = useBreakpoints()
  34. const isMobile = media === MediaType.mobile
  35. const [isShowNavMenu, { toggle, setFalse: hideNavMenu }] = useBoolean(false)
  36. const { enableBilling, plan } = useProviderContext()
  37. const { setShowPricingModal, setShowAccountSettingModal } = useModalContext()
  38. const isFreePlan = plan.type === 'sandbox'
  39. const handlePlanClick = useCallback(() => {
  40. if (isFreePlan)
  41. setShowPricingModal()
  42. else
  43. setShowAccountSettingModal({ payload: 'billing' })
  44. }, [isFreePlan, setShowAccountSettingModal, setShowPricingModal])
  45. useEffect(() => {
  46. hideNavMenu()
  47. // eslint-disable-next-line react-hooks/exhaustive-deps
  48. }, [selectedSegment])
  49. return (
  50. <div className='flex flex-1 items-center justify-between px-4'>
  51. <div className='flex items-center'>
  52. {isMobile && <div
  53. className='flex items-center justify-center h-8 w-8 cursor-pointer'
  54. onClick={toggle}
  55. >
  56. <Bars3Icon className="h-4 w-4 text-gray-500" />
  57. </div>}
  58. {!isMobile && <>
  59. <Link href="/apps" className='flex items-center mr-4'>
  60. <LogoSite className='object-contain' />
  61. </Link>
  62. {systemFeatures.license.status === LicenseStatus.NONE && <GithubStar />}
  63. </>}
  64. </div>
  65. {isMobile && (
  66. <div className='flex'>
  67. <Link href="/apps" className='flex items-center mr-4'>
  68. <LogoSite />
  69. </Link>
  70. {systemFeatures.license.status === LicenseStatus.NONE && <GithubStar />}
  71. </div>
  72. )}
  73. {!isMobile && (
  74. <div className='flex items-center'>
  75. {!isCurrentWorkspaceDatasetOperator && <ExploreNav className={navClassName} />}
  76. {!isCurrentWorkspaceDatasetOperator && <AppNav />}
  77. {(isCurrentWorkspaceEditor || isCurrentWorkspaceDatasetOperator) && <DatasetNav />}
  78. {!isCurrentWorkspaceDatasetOperator && <ToolsNav className={navClassName} />}
  79. </div>
  80. )}
  81. <div className='flex items-center flex-shrink-0'>
  82. <LicenseNav />
  83. <EnvNav />
  84. {enableBilling && (
  85. <div className='mr-3 select-none'>
  86. <HeaderBillingBtn onClick={handlePlanClick} />
  87. </div>
  88. )}
  89. <WorkspaceProvider>
  90. <AccountDropdown isMobile={isMobile} />
  91. </WorkspaceProvider>
  92. </div>
  93. {(isMobile && isShowNavMenu) && (
  94. <div className='w-full flex flex-col p-2 gap-y-1'>
  95. {!isCurrentWorkspaceDatasetOperator && <ExploreNav className={navClassName} />}
  96. {!isCurrentWorkspaceDatasetOperator && <AppNav />}
  97. {(isCurrentWorkspaceEditor || isCurrentWorkspaceDatasetOperator) && <DatasetNav />}
  98. {!isCurrentWorkspaceDatasetOperator && <ToolsNav className={navClassName} />}
  99. </div>
  100. )}
  101. </div>
  102. )
  103. }
  104. export default Header