import React, { useMemo } from "react"; import { buySubscription } from "@/api/addition.ts"; import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; import { useToast } from "@/components/ui/use-toast.ts"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog.tsx"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select.tsx"; import { Badge } from "@/components/ui/badge.tsx"; import { DialogClose } from "@radix-ui/react-dialog"; import { Button } from "@/components/ui/button.tsx"; import { expiredSelector, refreshSubscription } from "@/store/subscription.ts"; import { Plus } from "lucide-react"; import { subscriptionPrize } from "@/conf.ts"; import { ToastAction } from "@/components/ui/toast.tsx"; import { deeptrainEndpoint } from "@/utils/env.ts"; function countPrize(base: number, month: number): number { const prize = subscriptionPrize[base] * month; if (month >= 36) { return prize * 0.7; } else if (month >= 12) { return prize * 0.8; } else if (month >= 6) { return prize * 0.9; } return prize; } function countUpgradePrize( level: number, target: number, days: number, ): number { const bias = subscriptionPrize[target] - subscriptionPrize[level]; return (bias / 30) * days; } type UpgradeProps = { base: number; level: number; }; async function callBuyAction( t: any, toast: any, month: number, level: number, ): Promise { const res = await buySubscription(month, level); if (res.status) { toast({ title: t("sub.success"), description: t("sub.success-prompt", { month, }), }); } else { toast({ title: t("sub.failed"), description: t("sub.failed-prompt"), action: ( (location.href = `${deeptrainEndpoint}/home/wallet`)} > {t("buy.go")} ), }); setTimeout(() => { window.open(`${deeptrainEndpoint}/home/wallet`); }, 2000); } return res.status; } async function callMigrateAction( t: any, toast: any, level: number, ): Promise { const res = await buySubscription(1, level); if (res.status) { toast({ title: t("sub.migrate-success"), description: t("sub.migrate-success-prompt"), }); } else { toast({ title: t("sub.migrate-failed"), description: t("sub.migrate-failed-prompt"), }); } return res.status; } export function Upgrade({ base, level }: UpgradeProps) { const { t } = useTranslation(); const expired = useSelector(expiredSelector); const [open, setOpen] = React.useState(false); const [month, setMonth] = React.useState(1); const dispatch = useDispatch(); const { toast } = useToast(); const isCurrent = useMemo(() => level === base, [level, base]); const isUpgrade = useMemo(() => level < base, [level, base]); return level === 0 || level === base ? ( {t("sub.select-time")}

{t("sub.price", { price: countPrize(base, month).toFixed(2) })}

) : ( {t("sub.migrate-plan")}
{t("sub.migrate-plan-desc")} {isUpgrade && (

{t("sub.upgrade-price", { price: countUpgradePrize(level, base, expired).toFixed(2), })}

)}
); }