index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { useEffect, useRef, useState } from 'react'
  4. import {
  5. RiBook3Fill,
  6. RiBook3Line,
  7. RiBrain2Fill,
  8. RiBrain2Line, RiBuildingFill, RiBuildingLine,
  9. RiCloseLine,
  10. RiColorFilterFill,
  11. RiColorFilterLine,
  12. RiDatabase2Fill,
  13. RiDatabase2Line,
  14. RiGroup2Fill,
  15. RiGroup2Line,
  16. RiMoneyDollarCircleFill,
  17. RiMoneyDollarCircleLine,
  18. RiPagesFill,
  19. RiPagesLine,
  20. RiPuzzle2Fill,
  21. RiPuzzle2Line,
  22. RiTranslate2,
  23. } from '@remixicon/react'
  24. import Button from '../../base/button'
  25. import MembersPage from './members-page'
  26. import LanguagePage from './language-page'
  27. import ApiBasedExtensionPage from './api-based-extension-page'
  28. import DataSourcePage from './data-source-page'
  29. import ModelProviderPage from './model-provider-page'
  30. import TypesPage from './types-page'
  31. import KnowledgesPage from './knowledges-page'
  32. import cn from '@/utils/classnames'
  33. import BillingPage from '@/app/components/billing/billing-page'
  34. import CustomPage from '@/app/components/custom/custom-page'
  35. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  36. import { useProviderContext } from '@/context/provider-context'
  37. import { useAppContext } from '@/context/app-context'
  38. import MenuDialog from '@/app/components/header/account-setting/menu-dialog'
  39. import Input from '@/app/components/base/input'
  40. import DeptsPage from './dept-page'
  41. const iconClassName = `
  42. w-5 h-5 mr-2
  43. `
  44. type IAccountSettingProps = {
  45. onCancel: () => void
  46. activeTab?: string
  47. }
  48. type GroupItem = {
  49. key: string
  50. name: string
  51. description?: string
  52. icon: React.JSX.Element
  53. activeIcon: React.JSX.Element
  54. }
  55. export default function AccountSetting({
  56. onCancel,
  57. activeTab = 'members',
  58. }: IAccountSettingProps) {
  59. const [activeMenu, setActiveMenu] = useState(activeTab)
  60. const { t } = useTranslation()
  61. const { enableBilling, enableReplaceWebAppLogo } = useProviderContext()
  62. const { isCurrentWorkspaceDatasetOperator } = useAppContext()
  63. const workplaceGroupItems = (() => {
  64. if (isCurrentWorkspaceDatasetOperator)
  65. return []
  66. return [
  67. {
  68. key: 'provider',
  69. name: t('common.settings.provider'),
  70. icon: <RiBrain2Line className={iconClassName} />,
  71. activeIcon: <RiBrain2Fill className={iconClassName} />,
  72. },
  73. {
  74. key: 'members',
  75. name: t('common.settings.members'),
  76. icon: <RiGroup2Line className={iconClassName} />,
  77. activeIcon: <RiGroup2Fill className={iconClassName} />,
  78. },
  79. {
  80. // Use key false to hide this item
  81. key: enableBilling ? 'billing' : false,
  82. name: t('common.settings.billing'),
  83. description: t('billing.plansCommon.receiptInfo'),
  84. icon: <RiMoneyDollarCircleLine className={iconClassName} />,
  85. activeIcon: <RiMoneyDollarCircleFill className={iconClassName} />,
  86. },
  87. {
  88. key: 'data-source',
  89. name: t('common.settings.dataSource'),
  90. icon: <RiDatabase2Line className={iconClassName} />,
  91. activeIcon: <RiDatabase2Fill className={iconClassName} />,
  92. },
  93. {
  94. key: 'api-based-extension',
  95. name: t('common.settings.apiBasedExtension'),
  96. icon: <RiPuzzle2Line className={iconClassName} />,
  97. activeIcon: <RiPuzzle2Fill className={iconClassName} />,
  98. },
  99. {
  100. key: (enableReplaceWebAppLogo || enableBilling) ? 'custom' : false,
  101. name: t('custom.custom'),
  102. icon: <RiColorFilterLine className={iconClassName} />,
  103. activeIcon: <RiColorFilterFill className={iconClassName} />,
  104. },
  105. {
  106. key: 'type',
  107. name: '类型管理',
  108. icon: <RiPagesLine className={iconClassName} />,
  109. activeIcon: <RiPagesFill className={iconClassName} />,
  110. },
  111. {
  112. key: 'knowledge',
  113. name: '知识服务',
  114. icon: <RiBook3Line className={iconClassName} />,
  115. activeIcon: <RiBook3Fill className={iconClassName} />,
  116. },
  117. {
  118. key: 'dept',
  119. name: '部门',
  120. icon: <RiBuildingLine className={iconClassName} />,
  121. activeIcon: <RiBuildingFill className={iconClassName} />,
  122. },
  123. ].filter(item => !!item.key) as GroupItem[]
  124. })()
  125. const media = useBreakpoints()
  126. const isMobile = media === MediaType.mobile
  127. const menuItems = [
  128. {
  129. key: 'workspace-group',
  130. name: t('common.settings.workplaceGroup'),
  131. items: workplaceGroupItems,
  132. },
  133. {
  134. key: 'account-group',
  135. name: t('common.settings.generalGroup'),
  136. items: [
  137. {
  138. key: 'language',
  139. name: t('common.settings.language'),
  140. icon: <RiTranslate2 className={iconClassName} />,
  141. activeIcon: <RiTranslate2 className={iconClassName} />,
  142. },
  143. ],
  144. },
  145. ]
  146. const scrollRef = useRef<HTMLDivElement>(null)
  147. const [scrolled, setScrolled] = useState(false)
  148. useEffect(() => {
  149. const targetElement = scrollRef.current
  150. const scrollHandle = (e: Event) => {
  151. const userScrolled = (e.target as HTMLDivElement).scrollTop > 0
  152. setScrolled(userScrolled)
  153. }
  154. targetElement?.addEventListener('scroll', scrollHandle)
  155. return () => {
  156. targetElement?.removeEventListener('scroll', scrollHandle)
  157. }
  158. }, [])
  159. const activeItem = [...menuItems[0].items, ...menuItems[1].items].find(item => item.key === activeMenu)
  160. const [searchValue, setSearchValue] = useState<string>('')
  161. return (
  162. <MenuDialog
  163. show
  164. onClose={onCancel}
  165. >
  166. <div className='mx-auto flex h-[100vh] max-w-[1048px]'>
  167. <div className='flex w-[44px] flex-col border-r border-divider-burn pl-4 pr-6 sm:w-[224px]'>
  168. <div className='title-2xl-semi-bold mb-8 mt-6 px-3 py-2 text-text-primary'>{t('common.userProfile.settings')}</div>
  169. <div className='w-full'>
  170. {
  171. menuItems.map(menuItem => (
  172. <div key={menuItem.key} className='mb-2'>
  173. {!isCurrentWorkspaceDatasetOperator && (
  174. <div className='system-xs-medium-uppercase mb-0.5 py-2 pb-1 pl-3 text-text-tertiary'>{menuItem.name}</div>
  175. )}
  176. <div>
  177. {
  178. menuItem.items.map(item => (
  179. <div
  180. key={item.key}
  181. className={cn(
  182. 'mb-0.5 flex h-[37px] cursor-pointer items-center rounded-lg p-1 pl-3 text-sm',
  183. activeMenu === item.key ? 'system-sm-semibold bg-state-base-active text-components-menu-item-text-active' : 'system-sm-medium text-components-menu-item-text')}
  184. title={item.name}
  185. onClick={() => setActiveMenu(item.key)}
  186. >
  187. {activeMenu === item.key ? item.activeIcon : item.icon}
  188. {!isMobile && <div className='truncate'>{item.name}</div>}
  189. </div>
  190. ))
  191. }
  192. </div>
  193. </div>
  194. ))
  195. }
  196. </div>
  197. </div>
  198. <div className='relative flex w-[824px]'>
  199. <div className='absolute -right-11 top-6 z-[9999] flex flex-col items-center'>
  200. <Button
  201. variant='tertiary'
  202. size='large'
  203. className='px-2'
  204. onClick={onCancel}
  205. >
  206. <RiCloseLine className='h-5 w-5' />
  207. </Button>
  208. <div className='system-2xs-medium-uppercase mt-1 text-text-tertiary'>ESC</div>
  209. </div>
  210. <div ref={scrollRef} className='w-full overflow-y-auto bg-components-panel-bg pb-4'>
  211. <div className={cn('sticky top-0 z-20 mx-8 mb-[18px] flex items-center bg-components-panel-bg pb-2 pt-[27px]', scrolled && 'border-b border-divider-regular')}>
  212. <div className='title-2xl-semi-bold shrink-0 text-text-primary'>
  213. {activeItem?.name}
  214. {activeItem?.description && (
  215. <div className='system-sm-regular mt-1 text-text-tertiary'>{activeItem?.description}</div>
  216. )}
  217. </div>
  218. {activeItem?.key === 'provider' && (
  219. <div className='flex grow justify-end'>
  220. <Input
  221. showLeftIcon
  222. wrapperClassName='!w-[200px]'
  223. className='!h-8 !text-[13px]'
  224. onChange={e => setSearchValue(e.target.value)}
  225. value={searchValue}
  226. />
  227. </div>
  228. )}
  229. </div>
  230. <div className='px-4 pt-2 sm:px-8'>
  231. {activeMenu === 'provider' && <ModelProviderPage searchText={searchValue} />}
  232. {activeMenu === 'members' && <MembersPage />}
  233. {activeMenu === 'billing' && <BillingPage />}
  234. {activeMenu === 'data-source' && <DataSourcePage />}
  235. {activeMenu === 'api-based-extension' && <ApiBasedExtensionPage />}
  236. {activeMenu === 'custom' && <CustomPage />}
  237. {activeMenu === 'language' && <LanguagePage />}
  238. {activeMenu === 'type' && <TypesPage />}
  239. {activeMenu === 'knowledge' && <KnowledgesPage />}
  240. {activeMenu === 'dept' && <DeptsPage />}
  241. </div>
  242. </div>
  243. </div>
  244. </div>
  245. </MenuDialog>
  246. )
  247. }