plan-item.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import { Plan } from '../type'
  7. import { ALL_PLANS, NUM_INFINITE, contactSalesUrl } from '../config'
  8. import Toast from '../../base/toast'
  9. import { PlanRange } from './select-plan-range'
  10. import { useAppContext } from '@/context/app-context'
  11. import { fetchSubscriptionUrls } from '@/service/billing'
  12. type Props = {
  13. currentPlan: Plan
  14. plan: Plan
  15. planRange: PlanRange
  16. }
  17. const KeyValue = ({ label, value }: { label: string; value: string | number | JSX.Element }) => {
  18. return (
  19. <div className='mt-3.5 leading-[125%] text-[13px] font-medium'>
  20. <div className='text-gray-500'>{label}</div>
  21. <div className='mt-0.5 text-gray-900'>{value}</div>
  22. </div>
  23. )
  24. }
  25. const priceClassName = 'leading-[32px] text-[28px] font-bold text-gray-900'
  26. const style = {
  27. [Plan.sandbox]: {
  28. bg: 'bg-[#F2F4F7]',
  29. title: 'text-gray-900',
  30. hoverAndActive: '',
  31. },
  32. [Plan.professional]: {
  33. bg: 'bg-[#E0F2FE]',
  34. title: 'text-[#026AA2]',
  35. hoverAndActive: 'hover:shadow-lg hover:!text-white hover:!bg-[#0086C9] hover:!border-[#026AA2] active:!text-white active:!bg-[#026AA2] active:!border-[#026AA2]',
  36. },
  37. [Plan.team]: {
  38. bg: 'bg-[#E0EAFF]',
  39. title: 'text-[#3538CD]',
  40. hoverAndActive: 'hover:shadow-lg hover:!text-white hover:!bg-[#444CE7] hover:!border-[#3538CD] active:!text-white active:!bg-[#3538CD] active:!border-[#3538CD]',
  41. },
  42. [Plan.enterprise]: {
  43. bg: 'bg-[#FFEED3]',
  44. title: 'text-[#DC6803]',
  45. hoverAndActive: 'hover:shadow-lg hover:!text-white hover:!bg-[#F79009] hover:!border-[#DC6803] active:!text-white active:!bg-[#DC6803] active:!border-[#DC6803]',
  46. },
  47. }
  48. const PlanItem: FC<Props> = ({
  49. plan,
  50. currentPlan,
  51. planRange,
  52. }) => {
  53. const { t } = useTranslation()
  54. const [loading, setLoading] = React.useState(false)
  55. const i18nPrefix = `billing.plans.${plan}`
  56. const isFreePlan = plan === Plan.sandbox
  57. const isEnterprisePlan = plan === Plan.enterprise
  58. const isMostPopularPlan = plan === Plan.professional
  59. const planInfo = ALL_PLANS[plan]
  60. const isYear = planRange === PlanRange.yearly
  61. const isCurrent = plan === currentPlan
  62. const isPlanDisabled = planInfo.level <= ALL_PLANS[currentPlan].level
  63. const { isCurrentWorkspaceManager } = useAppContext()
  64. const btnText = (() => {
  65. if (isCurrent)
  66. return t('billing.plansCommon.currentPlan')
  67. return ({
  68. [Plan.sandbox]: t('billing.plansCommon.startForFree'),
  69. [Plan.professional]: <>{t('billing.plansCommon.getStartedWith')}<span className='capitalize'>&nbsp;{plan}</span></>,
  70. [Plan.team]: <>{t('billing.plansCommon.getStartedWith')}<span className='capitalize'>&nbsp;{plan}</span></>,
  71. [Plan.enterprise]: t('billing.plansCommon.talkToSales'),
  72. })[plan]
  73. })()
  74. const comingSoon = (
  75. <div className='leading-[12px] text-[9px] font-semibold text-[#3538CD] uppercase'>{t('billing.plansCommon.comingSoon')}</div>
  76. )
  77. const supportContent = (() => {
  78. switch (plan) {
  79. case Plan.sandbox:
  80. return t('billing.plansCommon.supportItems.communityForums')
  81. case Plan.professional:
  82. return t('billing.plansCommon.supportItems.emailSupport')
  83. case Plan.team:
  84. return (
  85. <div>
  86. <div>{t('billing.plansCommon.supportItems.priorityEmail')}</div>
  87. <div className='mt-3.5 flex items-center space-x-1'>
  88. <div>+ {t('billing.plansCommon.supportItems.logoChange')}</div>
  89. <div>{comingSoon}</div>
  90. </div>
  91. <div className='mt-3.5 flex items-center space-x-1'>
  92. <div>+ {t('billing.plansCommon.supportItems.personalizedSupport')}</div>
  93. <div>{comingSoon}</div>
  94. </div>
  95. </div>
  96. )
  97. case Plan.enterprise:
  98. return (
  99. <div>
  100. <div>{t('billing.plansCommon.supportItems.personalizedSupport')}</div>
  101. <div className='mt-3.5 flex items-center space-x-1'>
  102. <div>+ {t('billing.plansCommon.supportItems.dedicatedAPISupport')}</div>
  103. </div>
  104. <div className='mt-3.5 flex items-center space-x-1'>
  105. <div>+ {t('billing.plansCommon.supportItems.customIntegration')}</div>
  106. </div>
  107. </div>
  108. )
  109. default:
  110. return ''
  111. }
  112. })()
  113. const handleGetPayUrl = async () => {
  114. if (loading)
  115. return
  116. if (isPlanDisabled)
  117. return
  118. if (isFreePlan)
  119. return
  120. if (isEnterprisePlan) {
  121. window.location.href = contactSalesUrl
  122. return
  123. }
  124. // Only workspace manager can buy plan
  125. if (!isCurrentWorkspaceManager) {
  126. Toast.notify({
  127. type: 'error',
  128. message: t('billing.buyPermissionDeniedTip'),
  129. className: 'z-[1001]',
  130. })
  131. return
  132. }
  133. setLoading(true)
  134. try {
  135. const res = await fetchSubscriptionUrls(plan, isYear ? 'year' : 'month')
  136. window.location.href = res.url
  137. }
  138. finally {
  139. setLoading(false)
  140. }
  141. }
  142. return (
  143. <div className={cn(isMostPopularPlan ? 'bg-[#0086C9] p-0.5' : 'pt-7', 'flex flex-col min-w-[290px] w-[290px] h-[712px] rounded-xl')}>
  144. {isMostPopularPlan && (
  145. <div className='flex items-center h-7 justify-center leading-[12px] text-xs font-medium text-[#F5F8FF]'>{t('billing.plansCommon.mostPopular')}</div>
  146. )}
  147. <div className={cn(style[plan].bg, 'grow px-6 pt-6 rounded-[10px]')}>
  148. <div className={cn(style[plan].title, 'mb-1 leading-[125%] text-lg font-semibold')}>{t(`${i18nPrefix}.name`)}</div>
  149. <div className={cn(isFreePlan ? 'text-[#FB6514]' : 'text-gray-500', 'mb-4 h-8 leading-[125%] text-[13px] font-normal')}>{t(`${i18nPrefix}.description`)}</div>
  150. {/* Price */}
  151. {isFreePlan && (
  152. <div className={priceClassName}>{t('billing.plansCommon.free')}</div>
  153. )}
  154. {isEnterprisePlan && (
  155. <div className={priceClassName}>{t('billing.plansCommon.contactSales')}</div>
  156. )}
  157. {!isFreePlan && !isEnterprisePlan && (
  158. <div className='flex items-end h-9'>
  159. <div className={priceClassName}>${isYear ? planInfo.price * 10 : planInfo.price}</div>
  160. <div className='ml-1'>
  161. {isYear && <div className='leading-[18px] text-xs font-medium text-[#F26725]'>{t('billing.plansCommon.save')}${planInfo.price * 2}</div>}
  162. <div className='leading-[18px] text-[15px] font-normal text-gray-500'>/{t(`billing.plansCommon.${!isYear ? 'month' : 'year'}`)}</div>
  163. </div>
  164. </div>
  165. )}
  166. <div
  167. className={cn(isMostPopularPlan && !isCurrent && '!bg-[#444CE7] !text-white !border !border-[#3538CD] shadow-sm', isPlanDisabled ? 'opacity-30' : `${style[plan].hoverAndActive} cursor-pointer`, 'mt-4 flex h-11 items-center justify-center border-[2px] border-gray-900 rounded-3xl text-sm font-semibold text-gray-900')}
  168. onClick={handleGetPayUrl}
  169. >
  170. {btnText}
  171. </div>
  172. <div className='my-4 h-[1px] bg-black/5'></div>
  173. <div className='leading-[125%] text-[13px] font-normal text-gray-900'>
  174. {t(`${i18nPrefix}.includesTitle`)}
  175. </div>
  176. <KeyValue
  177. label={t('billing.plansCommon.modelProviders')}
  178. value={planInfo.modelProviders}
  179. />
  180. <KeyValue
  181. label={t('billing.plansCommon.teamMembers')}
  182. value={planInfo.teamMembers === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : planInfo.teamMembers}
  183. />
  184. <KeyValue
  185. label={t('billing.plansCommon.buildApps')}
  186. value={planInfo.buildApps === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : planInfo.buildApps}
  187. />
  188. <KeyValue
  189. label={t('billing.plansCommon.vectorSpace')}
  190. value={planInfo.vectorSpace === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : (planInfo.vectorSpace >= 1000 ? `${planInfo.vectorSpace / 1000}G` : `${planInfo.vectorSpace}MB`)}
  191. />
  192. <KeyValue
  193. label={t('billing.plansCommon.documentProcessingPriority')}
  194. value={t(`billing.plansCommon.priority.${planInfo.documentProcessingPriority}`) as string}
  195. />
  196. <KeyValue
  197. label={t('billing.plansCommon.logsHistory')}
  198. value={planInfo.logHistory === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : `${planInfo.logHistory} ${t('billing.plansCommon.days')}`}
  199. />
  200. <KeyValue
  201. label={t('billing.plansCommon.support')}
  202. value={supportContent}
  203. />
  204. </div>
  205. </div>
  206. )
  207. }
  208. export default React.memo(PlanItem)