"use client"; import { useQuery } from "@tanstack/react-query"; import { Label } from "@workspace/ui/components/label"; import { RadioGroup, RadioGroupItem, } from "@workspace/ui/components/radio-group"; import { cn } from "@workspace/ui/lib/utils"; import { getAvailablePaymentMethods } from "@workspace/ui/services/user/portal"; import type React from "react"; import { memo, useEffect } from "react"; import { useTranslation } from "react-i18next"; interface PaymentMethodsProps { value: number; onChange: (value: number) => void; balance?: boolean; } const PaymentMethods: React.FC = ({ value, onChange, balance = true, }) => { const { t } = useTranslation("subscribe"); const { data } = useQuery({ queryKey: ["getAvailablePaymentMethods", { balance }], queryFn: async () => { const { data } = await getAvailablePaymentMethods(); const list = data.data?.list || []; return balance ? list : list.filter((item) => item.id !== -1); }, }); // Only set a default when the current value is not a valid option. // This avoids resetting the user's selection on refetch (common on mobile). // Prefer non-balance methods when possible. useEffect(() => { if (!data || data.length === 0) return; const valid = data.some((m) => String(m.id) === String(value)); if (valid) return; const preferred = data.find((m) => m.id !== -1)?.id ?? data[0]!.id; onChange(preferred); }, [data, onChange, value]); return ( <>
{t("paymentMethod", "Payment Method")}
{ onChange(Number(val)); }} value={String(value)} > {data?.map((item) => (
))}
); }; export default memo(PaymentMethods);