use-pay.tsx 5.6 KB

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