| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { useCallback, useEffect, useState } from 'react' | 
					
						
							|  |  |  | import { useRouter, useSearchParams } from 'next/navigation' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import useSWR from 'swr' | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   fetchDataSourceNotionBinding, | 
					
						
							|  |  |  | } from '@/service/common' | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  | import type { IConfirm } from '@/app/components/base/confirm' | 
					
						
							|  |  |  | import Confirm from '@/app/components/base/confirm' | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  | export type ConfirmType = Pick<IConfirm, 'type' | 'title' | 'content'> | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useAnthropicCheckPay = () => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [confirm, setConfirm] = useState<ConfirmType | null>(null) | 
					
						
							|  |  |  |   const searchParams = useSearchParams() | 
					
						
							|  |  |  |   const providerName = searchParams.get('provider_name') | 
					
						
							|  |  |  |   const paymentResult = searchParams.get('payment_result') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |     if (providerName === 'anthropic' && (paymentResult === 'succeeded' || paymentResult === 'cancelled')) { | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  |       setConfirm({ | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |         type: paymentResult === 'succeeded' ? 'info' : 'warning', | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  |         title: paymentResult === 'succeeded' ? t('common.actionMsg.paySucceeded') : t('common.actionMsg.payCancelled'), | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [providerName, paymentResult, t]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return confirm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  | export const useBillingPay = () => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [confirm, setConfirm] = useState<ConfirmType | null>(null) | 
					
						
							|  |  |  |   const searchParams = useSearchParams() | 
					
						
							|  |  |  |   const paymentType = searchParams.get('payment_type') | 
					
						
							|  |  |  |   const paymentResult = searchParams.get('payment_result') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (paymentType === 'billing' && (paymentResult === 'succeeded' || paymentResult === 'cancelled')) { | 
					
						
							|  |  |  |       setConfirm({ | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |         type: paymentResult === 'succeeded' ? 'info' : 'warning', | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |         title: paymentResult === 'succeeded' ? t('common.actionMsg.paySucceeded') : t('common.actionMsg.payCancelled'), | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [paymentType, paymentResult, t]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return confirm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  | export const useCheckNotion = () => { | 
					
						
							|  |  |  |   const router = useRouter() | 
					
						
							|  |  |  |   const [confirm, setConfirm] = useState<ConfirmType | null>(null) | 
					
						
							|  |  |  |   const [canBinding, setCanBinding] = useState(false) | 
					
						
							|  |  |  |   const searchParams = useSearchParams() | 
					
						
							|  |  |  |   const type = searchParams.get('type') | 
					
						
							|  |  |  |   const notionCode = searchParams.get('code') | 
					
						
							|  |  |  |   const notionError = searchParams.get('error') | 
					
						
							|  |  |  |   const { data } = useSWR( | 
					
						
							| 
									
										
										
										
											2024-02-27 11:59:54 +08:00
										 |  |  |     (canBinding && notionCode) | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  |       ? `/oauth/data-source/binding/notion?code=${notionCode}` | 
					
						
							|  |  |  |       : null, | 
					
						
							|  |  |  |     fetchDataSourceNotionBinding, | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (data) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |       router.replace('/') | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  |   }, [data, router]) | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (type === 'notion') { | 
					
						
							|  |  |  |       if (notionError) { | 
					
						
							|  |  |  |         setConfirm({ | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |           type: 'warning', | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  |           title: notionError, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if (notionCode) { | 
					
						
							|  |  |  |         setCanBinding(true) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, [type, notionCode, notionError]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return confirm | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | export const CheckModal = () => { | 
					
						
							|  |  |  |   const router = useRouter() | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [showPayStatusModal, setShowPayStatusModal] = useState(true) | 
					
						
							|  |  |  |   const anthropicConfirmInfo = useAnthropicCheckPay() | 
					
						
							| 
									
										
										
										
											2023-09-28 14:39:13 +08:00
										 |  |  |   const notionConfirmInfo = useCheckNotion() | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   const billingConfirmInfo = useBillingPay() | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleCancelShowPayStatusModal = useCallback(() => { | 
					
						
							|  |  |  |     setShowPayStatusModal(false) | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |     router.replace('/') | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  |   }, [router]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const confirmInfo = anthropicConfirmInfo || notionConfirmInfo || billingConfirmInfo | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!confirmInfo || !showPayStatusModal) | 
					
						
							|  |  |  |     return null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <Confirm | 
					
						
							|  |  |  |       isShow | 
					
						
							|  |  |  |       onCancel={handleCancelShowPayStatusModal} | 
					
						
							|  |  |  |       onConfirm={handleCancelShowPayStatusModal} | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |       showCancel={false} | 
					
						
							|  |  |  |       type={confirmInfo.type === 'info' ? 'info' : 'warning' } | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  |       title={confirmInfo.title} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |       content={(confirmInfo as unknown as { desc: string }).desc || ''} | 
					
						
							| 
									
										
										
										
											2024-08-06 14:31:13 +08:00
										 |  |  |       confirmText={(confirmInfo.type === 'info' && t('common.operation.ok')) || ''} | 
					
						
							| 
									
										
										
										
											2023-09-18 18:02:05 +08:00
										 |  |  |     /> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } |