use-pay.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import useSWR from 'swr'
  5. import { useSearchParams } from 'next/navigation'
  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 { fetchSparkFreeQuotaVerify } from '@/service/common'
  10. import type { ConfirmCommonProps } from '@/app/components/base/confirm/common'
  11. export type ConfirmType = Pick<ConfirmCommonProps, 'type' | 'title' | 'desc'>
  12. export const useAnthropicCheckPay = () => {
  13. const { t } = useTranslation()
  14. const [confirm, setConfirm] = useState<ConfirmType | null>(null)
  15. const searchParams = useSearchParams()
  16. const providerName = searchParams.get('provider_name')
  17. const paymentResult = searchParams.get('payment_result')
  18. useEffect(() => {
  19. if (providerName === ProviderEnum.anthropic && (paymentResult === 'succeeded' || paymentResult === 'cancelled')) {
  20. setConfirm({
  21. type: paymentResult === 'succeeded' ? 'success' : 'danger',
  22. title: paymentResult === 'succeeded' ? t('common.actionMsg.paySucceeded') : t('common.actionMsg.payCancelled'),
  23. })
  24. }
  25. }, [providerName, paymentResult, t])
  26. return confirm
  27. }
  28. const QUOTA_RECEIVE_STATUS = {
  29. success: {
  30. 'en': 'Anthropic',
  31. 'zh-Hans': '领取成功,将在 5 分钟后自动增加配额',
  32. },
  33. fail: {
  34. 'en': 'Anthropic',
  35. 'zh-Hans': '领取失败',
  36. },
  37. }
  38. export const useSparkCheckQuota = () => {
  39. const { locale } = useContext(I18n)
  40. const [shouldVerify, setShouldVerify] = useState(false)
  41. const { data } = useSWR(
  42. shouldVerify
  43. ? `/workspaces/current/model-providers/${ProviderEnum.spark}/free-quota-qualification-verify`
  44. : null,
  45. fetchSparkFreeQuotaVerify,
  46. )
  47. const searchParams = useSearchParams()
  48. const type = searchParams.get('type')
  49. const provider = searchParams.get('provider')
  50. const result = searchParams.get('result')
  51. useEffect(() => {
  52. if (type === 'provider_apply_callback' && provider === ProviderEnum.spark && result === 'success')
  53. setShouldVerify(true)
  54. }, [type, provider, result])
  55. return data
  56. ? {
  57. type: data.flag ? 'success' : 'danger',
  58. title: data.flag ? QUOTA_RECEIVE_STATUS.success[locale] : QUOTA_RECEIVE_STATUS.fail[locale],
  59. desc: !data.flag ? data.reason : undefined,
  60. }
  61. : null
  62. }