From 367965e41f5b716b31e7908163910970c05d9d93 Mon Sep 17 00:00:00 2001 From: javayhu Date: Sat, 12 Jul 2025 12:46:57 +0800 Subject: [PATCH] feat: ensure handler session id only once in credit package --- src/components/settings/credits/credit-packages.tsx | 6 ++++-- src/stores/credits-store.ts | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/settings/credits/credit-packages.tsx b/src/components/settings/credits/credit-packages.tsx index 527b23a..4bf9b37 100644 --- a/src/components/settings/credits/credit-packages.tsx +++ b/src/components/settings/credits/credit-packages.tsx @@ -18,7 +18,7 @@ import { Routes } from '@/routes'; import { CircleCheckBigIcon, CoinsIcon, Loader2Icon } from 'lucide-react'; import { useTranslations } from 'next-intl'; import { useSearchParams } from 'next/navigation'; -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { toast } from 'sonner'; import { CreditCheckoutButton } from './credit-checkout-button'; @@ -30,6 +30,7 @@ export function CreditPackages() { const t = useTranslations('Dashboard.settings.credits.packages'); const searchParams = useSearchParams(); const localeRouter = useLocaleRouter(); + const hasHandledSession = useRef(false); // Use the new useCredits hook const { balance, isLoading, refresh } = useCredits(); @@ -45,7 +46,8 @@ export function CreditPackages() { // Check for payment success and show success message useEffect(() => { const sessionId = searchParams.get('session_id'); - if (sessionId) { + if (sessionId && !hasHandledSession.current) { + hasHandledSession.current = true; // Show success toast (delayed to avoid React lifecycle conflicts) setTimeout(() => { toast.success(t('creditsAdded')); diff --git a/src/stores/credits-store.ts b/src/stores/credits-store.ts index 7c39180..8421c1e 100644 --- a/src/stores/credits-store.ts +++ b/src/stores/credits-store.ts @@ -153,7 +153,13 @@ export const useCreditsStore = create((set, get) => ({ * @param user Current user from auth session */ refreshCredits: async (user) => { - if (!user) return; + if (!user) { + set({ + error: 'No user found', + isLoading: false, + }); + return; + } set({ isLoading: true,