index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use client'
  2. import { useCallback, useState } from 'react'
  3. import type { FC } from 'react'
  4. import useSWRInfinite from 'swr/infinite'
  5. import { useTranslation } from 'react-i18next'
  6. import { flatten } from 'lodash-es'
  7. import { useRouter, useSelectedLayoutSegment } from 'next/navigation'
  8. import classNames from 'classnames'
  9. import { CircleStackIcon, PuzzlePieceIcon } from '@heroicons/react/24/outline'
  10. import { CommandLineIcon, Squares2X2Icon } from '@heroicons/react/24/solid'
  11. import Link from 'next/link'
  12. import AccountDropdown from './account-dropdown'
  13. import Nav from './nav'
  14. import s from './index.module.css'
  15. import type { AppListResponse } from '@/models/app'
  16. import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
  17. import NewAppDialog from '@/app/(commonLayout)/apps/NewAppDialog'
  18. import { WorkspaceProvider } from '@/context/workspace-context'
  19. import { useDatasetsContext } from '@/context/datasets-context'
  20. import { fetchAppList } from '@/service/apps'
  21. const BuildAppsIcon = ({ isSelected }: { isSelected: boolean }) => (
  22. <svg className='mr-1' width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
  23. <path d="M13.6666 4.85221L7.99998 8.00036M7.99998 8.00036L2.33331 4.85221M7.99998 8.00036L8 14.3337M14 10.7061V5.29468C14 5.06625 14 4.95204 13.9663 4.85017C13.9366 4.76005 13.8879 4.67733 13.8236 4.60754C13.7509 4.52865 13.651 4.47318 13.4514 4.36224L8.51802 1.6215C8.32895 1.51646 8.23442 1.46395 8.1343 1.44336C8.0457 1.42513 7.95431 1.42513 7.8657 1.44336C7.76559 1.46395 7.67105 1.51646 7.48198 1.6215L2.54865 4.36225C2.34896 4.47318 2.24912 4.52865 2.17642 4.60754C2.11211 4.67733 2.06343 4.76005 2.03366 4.85017C2 4.95204 2 5.06625 2 5.29468V10.7061C2 10.9345 2 11.0487 2.03366 11.1506C2.06343 11.2407 2.11211 11.3234 2.17642 11.3932C2.24912 11.4721 2.34897 11.5276 2.54865 11.6385L7.48198 14.3793C7.67105 14.4843 7.76559 14.5368 7.8657 14.5574C7.95431 14.5756 8.0457 14.5756 8.1343 14.5574C8.23442 14.5368 8.32895 14.4843 8.51802 14.3793L13.4514 11.6385C13.651 11.5276 13.7509 11.4721 13.8236 11.3932C13.8879 11.3234 13.9366 11.2407 13.9663 11.1506C14 11.0487 14 10.9345 14 10.7061Z" stroke={isSelected ? '#155EEF' : '#667085'} strokeWidth="1.25" strokeLinecap="round" strokeLinejoin="round"/>
  24. </svg>
  25. )
  26. export type IHeaderProps = {
  27. curAppId?: string
  28. userProfile: UserProfileResponse
  29. onLogout: () => void
  30. langeniusVersionInfo: LangGeniusVersionResponse
  31. isBordered: boolean
  32. }
  33. const navClassName = `
  34. flex items-center relative mr-3 px-3 h-8 rounded-xl
  35. font-medium text-[14px]
  36. cursor-pointer
  37. `
  38. const headerEnvClassName: { [k: string]: string } = {
  39. DEVELOPMENT: 'bg-[#FEC84B] border-[#FDB022] text-[#93370D]',
  40. TESTING: 'bg-[#A5F0FC] border-[#67E3F9] text-[#164C63]',
  41. }
  42. const getKey = (pageIndex: number, previousPageData: AppListResponse) => {
  43. if (!pageIndex || previousPageData.has_more)
  44. return { url: 'apps', params: { page: pageIndex + 1, limit: 30 } }
  45. return null
  46. }
  47. const Header: FC<IHeaderProps> = ({
  48. curAppId,
  49. userProfile,
  50. onLogout,
  51. langeniusVersionInfo,
  52. isBordered,
  53. }) => {
  54. const { t } = useTranslation()
  55. const [showNewAppDialog, setShowNewAppDialog] = useState(false)
  56. const { data: appsData, isLoading, setSize } = useSWRInfinite(curAppId ? getKey : () => null, fetchAppList, { revalidateFirstPage: false })
  57. const { datasets, currentDataset } = useDatasetsContext()
  58. const router = useRouter()
  59. const showEnvTag = langeniusVersionInfo.current_env === 'TESTING' || langeniusVersionInfo.current_env === 'DEVELOPMENT'
  60. const selectedSegment = useSelectedLayoutSegment()
  61. const isPluginsComingSoon = selectedSegment === 'plugins-coming-soon'
  62. const isExplore = selectedSegment === 'explore'
  63. const appItems = flatten(appsData?.map(appData => appData.data))
  64. const handleLoadmore = useCallback(() => {
  65. if (isLoading)
  66. return
  67. setSize(size => size + 1)
  68. }, [setSize, isLoading])
  69. return (
  70. <div className={classNames(
  71. 'sticky top-0 left-0 right-0 z-20 flex bg-gray-100 grow-0 shrink-0 basis-auto h-14',
  72. s.header,
  73. isBordered ? 'border-b border-gray-200' : '',
  74. )}>
  75. <div className={classNames(
  76. s[`header-${langeniusVersionInfo.current_env}`],
  77. 'flex flex-1 items-center justify-between px-4',
  78. )}>
  79. <div className='flex items-center'>
  80. <Link href="/apps" className='flex items-center mr-3'>
  81. <div className={s.logo} />
  82. </Link>
  83. {/* Add it when has many stars */}
  84. <div className='
  85. flex items-center h-[26px] px-2 bg-white
  86. border border-solid border-[#E5E7EB] rounded-l-[6px] rounded-r-[6px]
  87. '>
  88. <div className={s.alpha} />
  89. <div className='ml-1 text-xs font-semibold text-gray-700'>{t('common.menus.status')}</div>
  90. </div>
  91. </div>
  92. <div className='flex items-center'>
  93. <Link href="/explore/apps" className={classNames(
  94. navClassName, 'group',
  95. isExplore && 'bg-white shadow-[0_2px_5px_-1px_rgba(0,0,0,0.05),0_2px_4px_-2px_rgba(0,0,0,0.05)]',
  96. isExplore ? 'text-primary-600' : 'text-gray-500 hover:bg-gray-200 hover:text-gray-700',
  97. )}>
  98. <Squares2X2Icon className='mr-1 w-[18px] h-[18px]' />
  99. {t('common.menus.explore')}
  100. </Link>
  101. <Nav
  102. icon={<BuildAppsIcon isSelected={['apps', 'app'].includes(selectedSegment || '')} />}
  103. text={t('common.menus.apps')}
  104. activeSegment={['apps', 'app']}
  105. link='/apps'
  106. curNav={appItems.find(appItem => appItem.id === curAppId)}
  107. navs={appItems.map(item => ({
  108. id: item.id,
  109. name: item.name,
  110. link: `/app/${item.id}/overview`,
  111. icon: item.icon,
  112. icon_background: item.icon_background,
  113. }))}
  114. createText={t('common.menus.newApp')}
  115. onCreate={() => setShowNewAppDialog(true)}
  116. onLoadmore={handleLoadmore}
  117. />
  118. <Link href="/plugins-coming-soon" className={classNames(
  119. navClassName, 'group',
  120. isPluginsComingSoon && 'bg-white shadow-[0_2px_5px_-1px_rgba(0,0,0,0.05),0_2px_4px_-2px_rgba(0,0,0,0.05)]',
  121. isPluginsComingSoon ? 'text-primary-600' : 'text-gray-500 hover:bg-gray-200 hover:text-gray-700',
  122. )}>
  123. <PuzzlePieceIcon className='mr-1 w-[18px] h-[18px]' />
  124. {t('common.menus.plugins')}
  125. </Link>
  126. <Nav
  127. icon={<CircleStackIcon className='mr-1 w-[18px] h-[18px]' />}
  128. text={t('common.menus.datasets')}
  129. activeSegment='datasets'
  130. link='/datasets'
  131. curNav={currentDataset && { id: currentDataset.id, name: currentDataset.name, icon: currentDataset.icon, icon_background: currentDataset.icon_background }}
  132. navs={datasets.map(dataset => ({
  133. id: dataset.id,
  134. name: dataset.name,
  135. link: `/datasets/${dataset.id}/documents`,
  136. icon: dataset.icon,
  137. icon_background: dataset.icon_background,
  138. }))}
  139. createText={t('common.menus.newDataset')}
  140. onCreate={() => router.push('/datasets/create')}
  141. />
  142. </div>
  143. <div className='flex items-center flex-shrink-0'>
  144. {
  145. showEnvTag && (
  146. <div className={`
  147. flex items-center h-[22px] mr-4 rounded-md px-2 text-xs font-medium border
  148. ${headerEnvClassName[langeniusVersionInfo.current_env]}
  149. `}>
  150. {
  151. langeniusVersionInfo.current_env === 'TESTING' && (
  152. <>
  153. <div className={s['beaker-icon']} />
  154. {t('common.environment.testing')}
  155. </>
  156. )
  157. }
  158. {
  159. langeniusVersionInfo.current_env === 'DEVELOPMENT' && (
  160. <>
  161. <CommandLineIcon className='w-3 h-3 mr-1' />
  162. {t('common.environment.development')}
  163. </>
  164. )
  165. }
  166. </div>
  167. )
  168. }
  169. <WorkspaceProvider>
  170. <AccountDropdown userProfile={userProfile} onLogout={onLogout} langeniusVersionInfo={langeniusVersionInfo} />
  171. </WorkspaceProvider>
  172. </div>
  173. </div>
  174. <NewAppDialog show={showNewAppDialog} onClose={() => setShowNewAppDialog(false)} />
  175. </div>
  176. )
  177. }
  178. export default Header