plan-item.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if ((window as any).gtag) {
  137. (window as any).gtag('event', 'click_pay_btn', {
  138. plan,
  139. interval: isYear ? 'year' : 'month',
  140. event_callback: () => {
  141. window.location.href = res.url
  142. },
  143. })
  144. }
  145. else {
  146. window.location.href = res.url
  147. }
  148. }
  149. finally {
  150. setLoading(false)
  151. }
  152. }
  153. return (
  154. <div className={cn(isMostPopularPlan ? 'bg-[#0086C9] p-0.5' : 'pt-7', 'flex flex-col min-w-[290px] w-[290px] h-[712px] rounded-xl')}>
  155. {isMostPopularPlan && (
  156. <div className='flex items-center h-7 justify-center leading-[12px] text-xs font-medium text-[#F5F8FF]'>{t('billing.plansCommon.mostPopular')}</div>
  157. )}
  158. <div className={cn(style[plan].bg, 'grow px-6 pt-6 rounded-[10px]')}>
  159. <div className={cn(style[plan].title, 'mb-1 leading-[125%] text-lg font-semibold')}>{t(`${i18nPrefix}.name`)}</div>
  160. <div className={cn(isFreePlan ? 'text-[#FB6514]' : 'text-gray-500', 'mb-4 h-8 leading-[125%] text-[13px] font-normal')}>{t(`${i18nPrefix}.description`)}</div>
  161. {/* Price */}
  162. {isFreePlan && (
  163. <div className={priceClassName}>{t('billing.plansCommon.free')}</div>
  164. )}
  165. {isEnterprisePlan && (
  166. <div className={priceClassName}>{t('billing.plansCommon.contactSales')}</div>
  167. )}
  168. {!isFreePlan && !isEnterprisePlan && (
  169. <div className='flex items-end h-9'>
  170. <div className={priceClassName}>${isYear ? planInfo.price * 10 : planInfo.price}</div>
  171. <div className='ml-1'>
  172. {isYear && <div className='leading-[18px] text-xs font-medium text-[#F26725]'>{t('billing.plansCommon.save')}${planInfo.price * 2}</div>}
  173. <div className='leading-[18px] text-[15px] font-normal text-gray-500'>/{t(`billing.plansCommon.${!isYear ? 'month' : 'year'}`)}</div>
  174. </div>
  175. </div>
  176. )}
  177. <div
  178. 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')}
  179. onClick={handleGetPayUrl}
  180. >
  181. {btnText}
  182. </div>
  183. <div className='my-4 h-[1px] bg-black/5'></div>
  184. <div className='leading-[125%] text-[13px] font-normal text-gray-900'>
  185. {t(`${i18nPrefix}.includesTitle`)}
  186. </div>
  187. <KeyValue
  188. label={t('billing.plansCommon.modelProviders')}
  189. value={planInfo.modelProviders}
  190. />
  191. <KeyValue
  192. label={t('billing.plansCommon.teamMembers')}
  193. value={planInfo.teamMembers === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : planInfo.teamMembers}
  194. />
  195. <KeyValue
  196. label={t('billing.plansCommon.buildApps')}
  197. value={planInfo.buildApps === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : planInfo.buildApps}
  198. />
  199. <KeyValue
  200. label={t('billing.plansCommon.vectorSpace')}
  201. value={planInfo.vectorSpace === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : (planInfo.vectorSpace >= 1000 ? `${planInfo.vectorSpace / 1000}G` : `${planInfo.vectorSpace}MB`)}
  202. />
  203. <KeyValue
  204. label={t('billing.plansCommon.documentProcessingPriority')}
  205. value={t(`billing.plansCommon.priority.${planInfo.documentProcessingPriority}`) as string}
  206. />
  207. <KeyValue
  208. label={t('billing.plansCommon.logsHistory')}
  209. value={planInfo.logHistory === NUM_INFINITE ? t('billing.plansCommon.unlimited') as string : `${planInfo.logHistory} ${t('billing.plansCommon.days')}`}
  210. />
  211. <KeyValue
  212. label={t('billing.plansCommon.support')}
  213. value={supportContent}
  214. />
  215. </div>
  216. </div>
  217. )
  218. }
  219. export default React.memo(PlanItem)