index.tsx 8.5 KB

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