Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 防止计费确认重复排队

liugaoweie051e524d5d8
diff --git a/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx b/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx
index 700b995f70..af0c7a0dd5 100644
--- a/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx
+++ b/apps/desktop/src/renderer/features/billing/BillingCheckoutDialog.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useMemo, useState } from 'react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import * as Dialog from '@radix-ui/react-dialog';
import * as QRCode from 'qrcode';
import { Check, CircleAlert, ExternalLink, LoaderCircle, RotateCcw, X } from 'lucide-react';
@@ -32,6 +32,7 @@ export function BillingCheckoutDialog({
}: BillingCheckoutDialogProps) {
const { t } = useTranslation();
const { confirm } = useConfirmDialog();
+ const confirmationPendingRef = useRef(false);
const action = actionOf(state);
const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);
const [remainingSeconds, setRemainingSeconds] = useState<number | null>(null);
@@ -93,9 +94,21 @@ export function BillingCheckoutDialog({
state.kind === 'TOPUP' &&
state.order !== null &&
(state.order.status === 'CREATED' || state.order.status === 'PENDING');
+ const confirmOnce = useCallback(
+ async (options: Parameters<typeof confirm>[0]) => {
+ if (confirmationPendingRef.current) return false;
+ confirmationPendingRef.current = true;
+ try {
+ return await confirm(options);
+ } finally {
+ confirmationPendingRef.current = false;
+ }
+ },
+ [confirm],
+ );
const confirmCancel = async () => {
if (!state.order) return;
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.cancelPaymentTitle'),
description: t('billing.confirmActions.cancelPaymentDescription', {
amount: formatMoney(state.order.amount, state.order.currency),
@@ -106,7 +119,7 @@ export function BillingCheckoutDialog({
if (accepted) onCancel();
};
const confirmRetry = async () => {
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.retryPaymentTitle'),
description: state.order
? t('billing.confirmActions.retryPaymentDescription', {
diff --git a/apps/desktop/src/renderer/features/billing/BillingPage.tsx b/apps/desktop/src/renderer/features/billing/BillingPage.tsx
index 6393b2456a..1027e70af5 100644
--- a/apps/desktop/src/renderer/features/billing/BillingPage.tsx
+++ b/apps/desktop/src/renderer/features/billing/BillingPage.tsx
@@ -188,6 +188,7 @@ export function BillingPage() {
export function BillingSettingsSection({ accountId }: { accountId: string | null }) {
const { t } = useTranslation();
const { confirm } = useConfirmDialog();
+ const confirmationPendingRef = useRef(false);
const [catalog, setCatalog] = useState<BillingCatalog | null>(null);
const [catalogError, setCatalogError] = useState(false);
const [loadingCatalog, setLoadingCatalog] = useState(true);
@@ -301,6 +302,18 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
checkout.state.intent !== null &&
checkout.state.order === null &&
checkout.state.subscription === null;
+ const confirmOnce = useCallback(
+ async (options: Parameters<typeof confirm>[0]) => {
+ if (confirmationPendingRef.current) return false;
+ confirmationPendingRef.current = true;
+ try {
+ return await confirm(options);
+ } finally {
+ confirmationPendingRef.current = false;
+ }
+ },
+ [confirm],
+ );
const offers = useMemo<PurchasableOffer[]>(() => {
if (!catalog) return [];
@@ -449,7 +462,7 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
);
const accepted =
selected.product.kind === 'CREDIT_TOPUP'
- ? await confirm({
+ ? await confirmOnce({
title: t('billing.confirmActions.purchaseTopupTitle'),
description: t('billing.confirmActions.purchaseTopupDescription', {
product: selected.product.name,
@@ -460,7 +473,7 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
cancelText: t('billing.confirmActions.back'),
autoFocusConfirm: true,
})
- : await confirm({
+ : await confirmOnce({
title: t('billing.confirmActions.purchaseSubscriptionTitle', {
plan: selected.product.name,
}),
@@ -512,7 +525,7 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
const selectPlanChangeTarget = async (candidate: PlanChangeCandidate) => {
if (candidate.offer.interval === null) return;
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.quotePlanChangeTitle'),
description: t('billing.confirmActions.quotePlanChangeDescription', {
current: currentPlanName ?? t('billing.settings.subscriptionCard.unnamedPlan'),
@@ -536,7 +549,7 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
};
const cancelPendingPlanChange = async (pending: BillingPendingPlanChange) => {
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.cancelPlanChangeTitle'),
description: t('billing.confirmActions.cancelPlanChangeDescription', {
target:
diff --git a/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx b/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx
index 15599432e9..94681604e3 100644
--- a/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx
+++ b/apps/desktop/src/renderer/features/billing/PlanChangeDialog.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useMemo, useState } from 'react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import * as Dialog from '@radix-ui/react-dialog';
import * as QRCode from 'qrcode';
import {
@@ -192,6 +192,7 @@ export function PlanChangeStatusDialog({
}) {
const { t } = useTranslation();
const { confirm } = useConfirmDialog();
+ const confirmationPendingRef = useRef(false);
const change = state.planChange;
const action: BillingPaymentAction | null = change?.paymentAction ?? null;
const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);
@@ -256,6 +257,18 @@ export function PlanChangeStatusDialog({
state.phase === 'CANCELED' ||
state.phase === 'FAILED' ||
state.phase === 'EXPIRED';
+ const confirmOnce = useCallback(
+ async (options: Parameters<typeof confirm>[0]) => {
+ if (confirmationPendingRef.current) return false;
+ confirmationPendingRef.current = true;
+ try {
+ return await confirm(options);
+ } finally {
+ confirmationPendingRef.current = false;
+ }
+ },
+ [confirm],
+ );
const confirmPlanChange = async () => {
if (!change) return;
const impact =
@@ -266,7 +279,7 @@ export function PlanChangeStatusDialog({
: t('billing.planChange.downgradeAt', {
date: formatEffectiveDate(change.effectiveAt),
});
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.confirmPlanChangeTitle'),
description: t('billing.confirmActions.confirmPlanChangeDescription', {
target: targetName ?? t('billing.settings.subscriptionCard.unnamedPlan'),
@@ -279,7 +292,7 @@ export function PlanChangeStatusDialog({
if (accepted) onConfirm();
};
const abandonPlanChange = async () => {
- const accepted = await confirm({
+ const accepted = await confirmOnce({
title: t('billing.confirmActions.cancelPlanChangeTitle'),
description: t('billing.confirmActions.cancelPlanChangeDescription', {
target: targetName ?? t('billing.settings.subscriptionCard.unnamedPlan'),
diff --git a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
index 38362af6bc..5b9a4ed51e 100644
--- a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
+++ b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
@@ -624,6 +624,32 @@ describe('BillingPage remote catalog rendering', () => {
expect(screen.getByText('Configured top-up')).toBeTruthy();
});
+ it('queues only one confirmation when the payment action is double-clicked', async () => {
+ let acceptConfirmation: ((value: boolean) => void) | null = null;
+ confirm.mockReturnValue(
+ new Promise<boolean>((resolve) => {
+ acceptConfirmation = resolve;
+ }),
+ );
+ render(<BillingPage />);
+
+ fireEvent.click(screen.getByText('billing.settings.topupCard.action'));
+ fireEvent.click((await screen.findByText('Configured top-up')).closest('button')!);
+ fireEvent.click((await screen.findByText('alipay')).closest('button')!);
+ fireEvent.change(screen.getByPlaceholderText('billing.amount.placeholder'), {
+ target: { value: '10' },
+ });
+ const pay = screen.getByText('billing.actions.pay');
+ fireEvent.click(pay);
+ fireEvent.click(pay);
+
+ expect(confirm).toHaveBeenCalledTimes(1);
+ expect(checkout.startTopup).not.toHaveBeenCalled();
+
+ acceptConfirmation!(true);
+ await waitFor(() => expect(checkout.startTopup).toHaveBeenCalledTimes(1));
+ });
+
it('does not confirm read-only refresh actions', async () => {
render(<BillingPage />);
await screen.findByText('billing.balance.title');