fix(subscription): preselect pay channel before navigation

This commit is contained in:
2026-07-02 11:38:47 +08:00
parent 35c8de917d
commit cd25fa35f2
15 changed files with 121 additions and 56 deletions
+5 -28
View File
@@ -1,10 +1,10 @@
"use client";
import { useEffect, useMemo, useRef } from "react";
import { useEffect, useMemo } from "react";
import { BackButton } from "@/app/_components";
import { Checkbox, MobileShell } from "@/app/_components/core";
import { AppConstants } from "@/core/app_constants";
import { useUserState } from "@/stores/user/user-context";
import type { PayChannel } from "@/data/dto/payment";
import {
SubscriptionCheckoutButton,
@@ -16,7 +16,6 @@ import {
import styles from "./components/subscription-screen.module.css";
import {
findSelectedSubscriptionPlan,
getDefaultPayChannelForCountryCode,
getDefaultSubscriptionPlanId,
toCoinsOfferPlanViews,
toVipOfferPlanViews,
@@ -30,12 +29,14 @@ export interface SubscriptionScreenProps {
subscriptionType?: SubscriptionType;
shouldResumePendingOrder?: boolean;
returnTo?: "chat" | null;
initialPayChannel?: PayChannel;
}
export function SubscriptionScreen({
subscriptionType = "vip",
shouldResumePendingOrder = false,
returnTo = null,
initialPayChannel = "stripe",
}: SubscriptionScreenProps) {
const {
payment,
@@ -47,9 +48,8 @@ export function SubscriptionScreen({
subscriptionType,
shouldResumePendingOrder,
returnTo,
initialPayChannel,
});
const user = useUserState();
const paymentMethodManuallySelectedRef = useRef(false);
const canSubscribeVip = subscriptionType === "vip";
const vipOfferPlans = useMemo(
@@ -100,28 +100,6 @@ export function SubscriptionScreen({
vipOfferPlans,
]);
useEffect(() => {
if (payment.status !== "ready") return;
if (paymentMethodManuallySelectedRef.current) return;
const countryCode = user.currentUser?.countryCode ?? "";
if (countryCode.trim().length === 0) return;
const defaultPayChannel =
getDefaultPayChannelForCountryCode(countryCode);
if (payment.payChannel === defaultPayChannel) return;
paymentDispatch({
type: "PaymentPayChannelChanged",
payChannel: defaultPayChannel,
});
}, [
payment.payChannel,
payment.status,
paymentDispatch,
user.currentUser?.countryCode,
]);
return (
<MobileShell>
<div className={styles.shell}>
@@ -163,7 +141,6 @@ export function SubscriptionScreen({
value={payment.payChannel}
disabled={isPaymentBusy}
onChange={(payChannel) => {
paymentMethodManuallySelectedRef.current = true;
paymentDispatch({
type: "PaymentPayChannelChanged",
payChannel,