index.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use client'
  2. import { useCallback, useEffect, useState } 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 AccountDropdown from './account-dropdown'
  8. import AppNav from './app-nav'
  9. import DatasetNav from './dataset-nav'
  10. import EnvNav from './env-nav'
  11. import PluginsNav from './plugins-nav'
  12. import ExploreNav from './explore-nav'
  13. import ToolsNav from './tools-nav'
  14. import { useAppContext } from '@/context/app-context'
  15. import LogoSite from '@/app/components/base/logo/logo-site'
  16. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  17. import { useProviderContext } from '@/context/provider-context'
  18. import { useModalContext } from '@/context/modal-context'
  19. import PlanBadge from './plan-badge'
  20. import LicenseNav from './license-env'
  21. import { Plan } from '../billing/type'
  22. import SkillNav from './skill-nav'
  23. import { fetchDepts } from '@/service/common'
  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, userProfile } = useAppContext()
  31. const selectedSegment = useSelectedLayoutSegment()
  32. const media = useBreakpoints()
  33. const isMobile = media === MediaType.mobile
  34. const [isShowNavMenu, { toggle, setFalse: hideNavMenu }] = useBoolean(false)
  35. const { enableBilling, plan } = useProviderContext()
  36. const { setShowPricingModal, setShowAccountSettingModal } = useModalContext()
  37. const isFreePlan = plan.type === Plan.sandbox
  38. const handlePlanClick = useCallback(() => {
  39. if (isFreePlan)
  40. setShowPricingModal()
  41. else
  42. setShowAccountSettingModal({ payload: 'billing' })
  43. }, [isFreePlan, setShowAccountSettingModal, setShowPricingModal])
  44. useEffect(() => {
  45. hideNavMenu()
  46. // eslint-disable-next-line react-hooks/exhaustive-deps
  47. }, [selectedSegment])
  48. const [deptMap, setDeptMap] = useState<any>(new Map())
  49. const addDeptMap = (key: any, value: any) => {
  50. setDeptMap((prevMap: any) => {
  51. const newMap = new Map(prevMap)
  52. newMap.set(key, value)
  53. return newMap
  54. })
  55. }
  56. const formatDept = (depts: any) => {
  57. depts.forEach((v: any) => {
  58. addDeptMap(v.dept_id, v.dept_name)
  59. if (v.children?.length > 0)
  60. formatDept(v.children)
  61. })
  62. }
  63. useEffect(() => {
  64. fetchDepts({
  65. url: '/depts',
  66. }).then((res: any) => {
  67. formatDept(res.data || [])
  68. })
  69. }, [])
  70. return (
  71. <div className='flex flex-1 items-center justify-between bg-background-body px-4'>
  72. <div className='flex items-center'>
  73. {isMobile && <div
  74. className='flex h-8 w-8 cursor-pointer items-center justify-center'
  75. onClick={toggle}
  76. >
  77. <Bars3Icon className="h-4 w-4 text-gray-500" />
  78. </div>}
  79. {
  80. !isMobile
  81. && <div className='flex w-64 shrink-0 items-center gap-1.5 self-stretch p-2 pl-3'>
  82. <Link href="/apps" className='flex h-8 w-8 shrink-0 items-center justify-center gap-2'>
  83. <LogoSite className='object-contain' />
  84. </Link>
  85. <div className='font-light text-divider-deep'>/</div>
  86. {/* <div className='flex items-center gap-0.5'> */}
  87. {/* <WorkspaceProvider> */}
  88. {/* <WorkplaceSelector /> */}
  89. {/* </WorkspaceProvider> */}
  90. {/* {enableBilling ? <PlanBadge allowHover sandboxAsUpgrade plan={plan.type} onClick={handlePlanClick} /> : <LicenseNav />} */}
  91. {/* </div> */}
  92. <div className="text-gray-500">{deptMap.get(userProfile.dept_id) || userProfile.dept_id}</div>
  93. </div>
  94. }
  95. </div >
  96. {isMobile && (
  97. <div className='flex'>
  98. <Link href="/apps" className='mr-4 flex items-center'>
  99. <LogoSite />
  100. </Link>
  101. <div className='font-light text-divider-deep'>/</div>
  102. {enableBilling ? <PlanBadge allowHover sandboxAsUpgrade plan={plan.type} onClick={handlePlanClick} /> : <LicenseNav />}
  103. </div >
  104. )}
  105. {
  106. !isMobile && (
  107. <div className='flex items-center'>
  108. {/* {!isCurrentWorkspaceDatasetOperator && <ExploreNav className={navClassName} />} */}
  109. {/* {!isCurrentWorkspaceDatasetOperator && <AppNav />} */}
  110. {/* {(isCurrentWorkspaceEditor || isCurrentWorkspaceDatasetOperator) && <DatasetNav />} */}
  111. {/* {!isCurrentWorkspaceDatasetOperator && <ToolsNav className={navClassName} />} */}
  112. {/* {!isCurrentWorkspaceDatasetOperator && <SkillNav className={navClassName} />} */}
  113. <ExploreNav className={navClassName} />
  114. <AppNav />
  115. <DatasetNav />
  116. <ToolsNav className={navClassName} />
  117. <SkillNav className={navClassName} />
  118. </div>
  119. )
  120. }
  121. <div className='flex shrink-0 items-center'>
  122. <EnvNav />
  123. <div className='mr-2'>
  124. <PluginsNav />
  125. </div>
  126. <AccountDropdown />
  127. </div>
  128. {
  129. (isMobile && isShowNavMenu) && (
  130. <div className='flex w-full flex-col gap-y-1 p-2'>
  131. {/* {!isCurrentWorkspaceDatasetOperator && <ExploreNav className={navClassName} />} */}
  132. {/* {!isCurrentWorkspaceDatasetOperator && <AppNav />} */}
  133. {/* {(isCurrentWorkspaceEditor || isCurrentWorkspaceDatasetOperator) && <DatasetNav />} */}
  134. {/* {!isCurrentWorkspaceDatasetOperator && <ToolsNav className={navClassName} />} */}
  135. {/* {!isCurrentWorkspaceDatasetOperator && <SkillNav className={navClassName} />} */}
  136. <ExploreNav className={navClassName} />
  137. <AppNav />
  138. <DatasetNav />
  139. <ToolsNav className={navClassName} />
  140. <SkillNav className={navClassName} />
  141. </div>
  142. )
  143. }
  144. </div >
  145. )
  146. }
  147. export default Header