use-pay.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use client'
  2. import { useCallback, useEffect, useState } from 'react'
  3. import { useRouter, useSearchParams } from 'next/navigation'
  4. import { useTranslation } from 'react-i18next'
  5. import useSWR from 'swr'
  6. import { useContext } from 'use-context-selector'
  7. import I18n from '@/context/i18n'
  8. import { ProviderEnum } from '@/app/components/header/account-setting/model-page/declarations'
  9. import {
  10. fetchDataSourceNotionBinding,
  11. fetchFreeQuotaVerify,
  12. } from '@/service/common'
  13. import type { ConfirmCommonProps } from '@/app/components/base/confirm/common'
  14. import Confirm from '@/app/components/base/confirm/common'
  15. export type ConfirmType = Pick<ConfirmCommonProps, 'type' | 'title' | 'desc'>
  16. export const useAnthropicCheckPay = () => {
  17. const { t } = useTranslation()
  18. const [confirm, setConfirm] = useState<ConfirmType | null>(null)
  19. const searchParams = useSearchParams()
  20. const providerName = searchParams.get('provider_name')
  21. const paymentResult = searchParams.get('payment_result')
  22. useEffect(() => {
  23. if (providerName === ProviderEnum.anthropic && (paymentResult === 'succeeded' || paymentResult === 'cancelled')) {
  24. setConfirm({
  25. type: paymentResult === 'succeeded' ? 'success' : 'danger',
  26. title: paymentResult === 'succeeded' ? t('common.actionMsg.paySucceeded') : t('common.actionMsg.payCancelled'),
  27. })
  28. }
  29. }, [providerName, paymentResult, t])
  30. return confirm
  31. }
  32. export const useBillingPay = () => {
  33. const { t } = useTranslation()
  34. const [confirm, setConfirm] = useState<ConfirmType | null>(null)
  35. const searchParams = useSearchParams()
  36. const paymentType = searchParams.get('payment_type')
  37. const paymentResult = searchParams.get('payment_result')
  38. useEffect(() => {
  39. if (paymentType === 'billing' && (paymentResult === 'succeeded' || paymentResult === 'cancelled')) {
  40. setConfirm({
  41. type: paymentResult === 'succeeded' ? 'success' : 'danger',
  42. title: paymentResult === 'succeeded' ? t('common.actionMsg.paySucceeded') : t('common.actionMsg.payCancelled'),
  43. })
  44. }
  45. }, [paymentType, paymentResult, t])
  46. return confirm
  47. }
  48. const QUOTA_RECEIVE_STATUS = {
  49. [ProviderEnum.spark]: {
  50. success: {
  51. 'en': 'Successful collection, the quota will be automatically increased after 5 minutes.',
  52. 'zh-Hans': '领取成功,将在 5 分钟后自动增加配额',
  53. },
  54. fail: {
  55. 'en': 'Failure to collect',
  56. 'zh-Hans': '领取失败',
  57. },
  58. },
  59. [ProviderEnum.zhipuai]: {
  60. success: {
  61. 'en': 'Successful collection',
  62. 'zh-Hans': '领取成功',
  63. },
  64. fail: {
  65. 'en': 'Failure to collect',
  66. 'zh-Hans': '领取失败',
  67. },
  68. },
  69. }
  70. const FREE_CHECK_PROVIDER = [ProviderEnum.spark, ProviderEnum.zhipuai]
  71. export const useCheckFreeQuota = () => {
  72. const { locale } = useContext(I18n)
  73. const router = useRouter()
  74. const [shouldVerify, setShouldVerify] = useState(false)
  75. const searchParams = useSearchParams()
  76. const type = searchParams.get('type')
  77. const provider = searchParams.get('provider') as (ProviderEnum.spark | ProviderEnum.zhipuai)
  78. const result = searchParams.get('result')
  79. const token = searchParams.get('token')
  80. const { data, error } = useSWR(
  81. shouldVerify
  82. ? `/workspaces/current/model-providers/${provider}/free-quota-qualification-verify?token=${token}`
  83. : null,
  84. fetchFreeQuotaVerify,
  85. )
  86. useEffect(() => {
  87. if (error)
  88. router.replace('/', { forceOptimisticNavigation: false })
  89. }, [error, router])
  90. useEffect(() => {
  91. if (type === 'provider_apply_callback' && FREE_CHECK_PROVIDER.includes(provider) && result === 'success')
  92. setShouldVerify(true)
  93. }, [type, provider, result])
  94. return (data && provider)
  95. ? {
  96. type: data.flag ? 'success' : 'danger',
  97. title: data.flag ? QUOTA_RECEIVE_STATUS[provider].success[locale] : QUOTA_RECEIVE_STATUS[provider].fail[locale],
  98. desc: !data.flag ? data.reason : undefined,
  99. }
  100. : null
  101. }
  102. export const useCheckNotion = () => {
  103. const router = useRouter()
  104. const [confirm, setConfirm] = useState<ConfirmType | null>(null)
  105. const [canBinding, setCanBinding] = useState(false)
  106. const searchParams = useSearchParams()
  107. const type = searchParams.get('type')
  108. const notionCode = searchParams.get('code')
  109. const notionError = searchParams.get('error')
  110. const { data } = useSWR(
  111. canBinding
  112. ? `/oauth/data-source/binding/notion?code=${notionCode}`
  113. : null,
  114. fetchDataSourceNotionBinding,
  115. )
  116. useEffect(() => {
  117. if (data)
  118. router.replace('/', { forceOptimisticNavigation: false })
  119. }, [data, router])
  120. useEffect(() => {
  121. if (type === 'notion') {
  122. if (notionError) {
  123. setConfirm({
  124. type: 'danger',
  125. title: notionError,
  126. })
  127. }
  128. else if (notionCode) {
  129. setCanBinding(true)
  130. }
  131. }
  132. }, [type, notionCode, notionError])
  133. return confirm
  134. }
  135. export const CheckModal = () => {
  136. const router = useRouter()
  137. const { t } = useTranslation()
  138. const [showPayStatusModal, setShowPayStatusModal] = useState(true)
  139. const anthropicConfirmInfo = useAnthropicCheckPay()
  140. const freeQuotaConfirmInfo = useCheckFreeQuota()
  141. const notionConfirmInfo = useCheckNotion()
  142. const billingConfirmInfo = useBillingPay()
  143. const handleCancelShowPayStatusModal = useCallback(() => {
  144. setShowPayStatusModal(false)
  145. router.replace('/', { forceOptimisticNavigation: false })
  146. }, [router])
  147. const confirmInfo = anthropicConfirmInfo || freeQuotaConfirmInfo || notionConfirmInfo || billingConfirmInfo
  148. if (!confirmInfo || !showPayStatusModal)
  149. return null
  150. return (
  151. <Confirm
  152. isShow
  153. onCancel={handleCancelShowPayStatusModal}
  154. onConfirm={handleCancelShowPayStatusModal}
  155. type={confirmInfo.type}
  156. title={confirmInfo.title}
  157. desc={confirmInfo.desc}
  158. showOperateCancel={false}
  159. confirmText={(confirmInfo.type === 'danger' && t('common.operation.ok')) || ''}
  160. />
  161. )
  162. }