index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use client'
  2. import { useTranslation } from 'react-i18next'
  3. import { useEffect, useRef, useState } from 'react'
  4. import cn from 'classnames'
  5. import { GoldCoin } from '../../base/icons/src/vender/solid/FinanceAndECommerce'
  6. import { GoldCoin as GoldCoinOutLine } from '../../base/icons/src/vender/line/financeAndECommerce'
  7. import AccountPage from './account-page'
  8. import MembersPage from './members-page'
  9. import IntegrationsPage from './Integrations-page'
  10. import LanguagePage from './language-page'
  11. import PluginPage from './plugin-page'
  12. import ApiBasedExtensionPage from './api-based-extension-page'
  13. import DataSourcePage from './data-source-page'
  14. import ModelProviderPage from './model-provider-page'
  15. import s from './index.module.css'
  16. import BillingPage from '@/app/components/billing/billing-page'
  17. import CustomPage from '@/app/components/custom/custom-page'
  18. import Modal from '@/app/components/base/modal'
  19. import {
  20. Database03,
  21. PuzzlePiece01,
  22. Webhooks,
  23. } from '@/app/components/base/icons/src/vender/line/development'
  24. import { Database03 as Database03Solid, PuzzlePiece01 as PuzzlePiece01Solid } from '@/app/components/base/icons/src/vender/solid/development'
  25. import { User01, Users01 } from '@/app/components/base/icons/src/vender/line/users'
  26. import { User01 as User01Solid, Users01 as Users01Solid } from '@/app/components/base/icons/src/vender/solid/users'
  27. import { Globe01 } from '@/app/components/base/icons/src/vender/line/mapsAndTravel'
  28. import { AtSign, XClose } from '@/app/components/base/icons/src/vender/line/general'
  29. import { CubeOutline } from '@/app/components/base/icons/src/vender/line/shapes'
  30. import { Colors } from '@/app/components/base/icons/src/vender/line/editor'
  31. import { Colors as ColorsSolid } from '@/app/components/base/icons/src/vender/solid/editor'
  32. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  33. import { useProviderContext } from '@/context/provider-context'
  34. const iconClassName = `
  35. w-4 h-4 ml-3 mr-2
  36. `
  37. const scrolledClassName = `
  38. border-b shadow-xs bg-white/[.98]
  39. `
  40. type IAccountSettingProps = {
  41. onCancel: () => void
  42. activeTab?: string
  43. }
  44. type GroupItem = {
  45. key: string
  46. name: string
  47. description?: string
  48. icon: JSX.Element
  49. activeIcon: JSX.Element
  50. }
  51. export default function AccountSetting({
  52. onCancel,
  53. activeTab = 'account',
  54. }: IAccountSettingProps) {
  55. const [activeMenu, setActiveMenu] = useState(activeTab)
  56. const { t } = useTranslation()
  57. const { enableBilling, enableReplaceWebAppLogo } = useProviderContext()
  58. const workplaceGroupItems = (() => {
  59. return [
  60. {
  61. key: 'provider',
  62. name: t('common.settings.provider'),
  63. icon: <CubeOutline className={iconClassName} />,
  64. activeIcon: <CubeOutline className={iconClassName} />,
  65. },
  66. {
  67. key: 'members',
  68. name: t('common.settings.members'),
  69. icon: <Users01 className={iconClassName} />,
  70. activeIcon: <Users01Solid className={iconClassName} />,
  71. },
  72. {
  73. // Use key false to hide this item
  74. key: enableBilling ? 'billing' : false,
  75. name: t('common.settings.billing'),
  76. description: t('billing.plansCommon.receiptInfo'),
  77. icon: <GoldCoinOutLine className={iconClassName} />,
  78. activeIcon: <GoldCoin className={iconClassName} />,
  79. },
  80. {
  81. key: 'data-source',
  82. name: t('common.settings.dataSource'),
  83. icon: <Database03 className={iconClassName} />,
  84. activeIcon: <Database03Solid className={iconClassName} />,
  85. },
  86. {
  87. key: 'plugin',
  88. name: t('common.settings.plugin'),
  89. icon: <PuzzlePiece01 className={iconClassName} />,
  90. activeIcon: <PuzzlePiece01Solid className={iconClassName} />,
  91. },
  92. {
  93. key: 'api-based-extension',
  94. name: t('common.settings.apiBasedExtension'),
  95. icon: <Webhooks className={iconClassName} />,
  96. activeIcon: <Webhooks className={iconClassName} />,
  97. },
  98. {
  99. key: (enableReplaceWebAppLogo || enableBilling) ? 'custom' : false,
  100. name: t('custom.custom'),
  101. icon: <Colors className={iconClassName} />,
  102. activeIcon: <ColorsSolid className={iconClassName} />,
  103. },
  104. ].filter(item => !!item.key) as GroupItem[]
  105. })()
  106. const media = useBreakpoints()
  107. const isMobile = media === MediaType.mobile
  108. const menuItems = [
  109. {
  110. key: 'workspace-group',
  111. name: t('common.settings.workplaceGroup'),
  112. items: workplaceGroupItems,
  113. },
  114. {
  115. key: 'account-group',
  116. name: t('common.settings.accountGroup'),
  117. items: [
  118. {
  119. key: 'account',
  120. name: t('common.settings.account'),
  121. icon: <User01 className={iconClassName} />,
  122. activeIcon: <User01Solid className={iconClassName} />,
  123. },
  124. {
  125. key: 'integrations',
  126. name: t('common.settings.integrations'),
  127. icon: <AtSign className={iconClassName} />,
  128. activeIcon: <AtSign className={iconClassName} />,
  129. },
  130. {
  131. key: 'language',
  132. name: t('common.settings.language'),
  133. icon: <Globe01 className={iconClassName} />,
  134. activeIcon: <Globe01 className={iconClassName} />,
  135. },
  136. ],
  137. },
  138. ]
  139. const scrollRef = useRef<HTMLDivElement>(null)
  140. const [scrolled, setScrolled] = useState(false)
  141. const scrollHandle = (e: Event) => {
  142. if ((e.target as HTMLDivElement).scrollTop > 0)
  143. setScrolled(true)
  144. else
  145. setScrolled(false)
  146. }
  147. useEffect(() => {
  148. const targetElement = scrollRef.current
  149. targetElement?.addEventListener('scroll', scrollHandle)
  150. return () => {
  151. targetElement?.removeEventListener('scroll', scrollHandle)
  152. }
  153. }, [])
  154. const activeItem = [...menuItems[0].items, ...menuItems[1].items].find(item => item.key === activeMenu)
  155. return (
  156. <Modal
  157. isShow
  158. onClose={() => { }}
  159. className={s.modal}
  160. wrapperClassName='!z-20 pt-[60px]'
  161. >
  162. <div className='flex'>
  163. <div className='w-[44px] sm:w-[200px] px-[1px] py-4 sm:p-4 border border-gray-100 shrink-0 sm:shrink-1 flex flex-col items-center sm:items-start'>
  164. <div className='mb-8 ml-0 sm:ml-2 text-sm sm:text-base font-medium leading-6 text-gray-900'>{t('common.userProfile.settings')}</div>
  165. <div className='w-full'>
  166. {
  167. menuItems.map(menuItem => (
  168. <div key={menuItem.key} className='mb-4'>
  169. <div className='px-2 mb-[6px] text-[10px] sm:text-xs font-medium text-gray-500'>{menuItem.name}</div>
  170. <div>
  171. {
  172. menuItem.items.map(item => (
  173. <div
  174. key={item.key}
  175. className={`
  176. flex items-center h-[37px] mb-[2px] text-sm cursor-pointer rounded-lg
  177. ${activeMenu === item.key ? 'font-semibold text-primary-600 bg-primary-50' : 'font-light text-gray-700'}
  178. `}
  179. title={item.name}
  180. onClick={() => setActiveMenu(item.key)}
  181. >
  182. {activeMenu === item.key ? item.activeIcon : item.icon}
  183. {!isMobile && <div className='truncate'>{item.name}</div>}
  184. </div>
  185. ))
  186. }
  187. </div>
  188. </div>
  189. ))
  190. }
  191. </div>
  192. </div>
  193. <div ref={scrollRef} className='relative w-[824px] h-[720px] pb-4 overflow-y-auto'>
  194. <div className={cn('sticky top-0 px-6 py-4 flex items-center h-14 mb-4 bg-white text-base font-medium text-gray-900 z-20', scrolled && scrolledClassName)}>
  195. <div className='shrink-0'>{activeItem?.name}</div>
  196. {
  197. activeItem?.description && (
  198. <div className='shrink-0 ml-2 text-xs text-gray-600'>{activeItem?.description}</div>
  199. )
  200. }
  201. <div className='grow flex justify-end'>
  202. <div className='flex items-center justify-center -mr-4 w-6 h-6 cursor-pointer' onClick={onCancel}>
  203. <XClose className='w-4 h-4 text-gray-400' />
  204. </div>
  205. </div>
  206. </div>
  207. <div className='px-4 sm:px-8 pt-2'>
  208. {activeMenu === 'account' && <AccountPage />}
  209. {activeMenu === 'members' && <MembersPage />}
  210. {activeMenu === 'billing' && <BillingPage />}
  211. {activeMenu === 'integrations' && <IntegrationsPage />}
  212. {activeMenu === 'language' && <LanguagePage />}
  213. {activeMenu === 'provider' && <ModelProviderPage />}
  214. {activeMenu === 'data-source' && <DataSourcePage />}
  215. {activeMenu === 'plugin' && <PluginPage />}
  216. {activeMenu === 'api-based-extension' && <ApiBasedExtensionPage /> }
  217. {activeMenu === 'custom' && <CustomPage /> }
  218. </div>
  219. </div>
  220. </div>
  221. </Modal>
  222. )
  223. }