"use client"; import { useState, useEffect } from "react"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { CheckIcon, Loader2 } from "lucide-react"; import { Dictionary } from "@/lib/IDict"; import { pb } from "@/lib/pb"; interface PBPricingPlan { id: string; name: string; level: string; tagline: string; description: string; price1m: number; price6m: number; price1y: number; features: string; popular: boolean; cta: string; lang: string; order: number; } type Period = "1m" | "6m" | "1y"; interface PeriodOption { id: Period; label: string; badge?: string; } export function Pricing({ dict, lang, }: { dict: Dictionary["pricing"]; lang: string; }) { const [period, setPeriod] = useState("1y"); const [plans, setPlans] = useState([]); // Инициализируем загрузку только если есть язык, иначе сразу false const [loading, setLoading] = useState(!!lang); useEffect(() => { if (!lang) return; async function fetchPlans() { try { setLoading(true); const records = await pb .collection("pricing_plans") .getFullList({ filter: `lang = "${lang}"`, sort: "order", requestKey: null, }); setPlans(records); } catch (error) { console.error("Ошибка загрузки тарифов:", error); } finally { setLoading(false); } } fetchPlans(); }, [lang]); const isRu = lang === "ru"; const currencySign = isRu ? "₽" : "$"; // Исправлено: теперь это массив PeriodOption[] const periods: PeriodOption[] = [ { id: "1m", label: isRu ? "1 месяц" : "1 month" }, { id: "6m", label: isRu ? "6 месяцев" : "6 months" }, { id: "1y", label: isRu ? "1 год" : "1 year", badge: isRu ? "Выгодно" : "Best Value", }, ]; return (
{dict.badge}

{dict.title}

{periods.map((p: PeriodOption) => ( ))}
{loading ? (
Syncing_Nodes...
) : plans.length === 0 ? (

0 uplinks found.

) : (
{plans.map((plan) => { const months = period === "1m" ? 1 : period === "6m" ? 6 : 12; const totalPrice = period === "1m" ? plan.price1m : period === "6m" ? plan.price6m : plan.price1y; const originalTotalPrice = plan.price1m * months; const isDiscounted = totalPrice < originalTotalPrice; const monthlyEquivalent = Math.round(totalPrice / months); const discountPercent = isDiscounted ? Math.round((1 - totalPrice / originalTotalPrice) * 100) : 0; const formatPrice = (price: number) => isRu ? price.toLocaleString("ru-RU") : price.toString(); return ( {plan.popular && (
{isRu ? "Рекомендуем" : "Recommended"}
)}
{plan.level}
{plan.name}
{plan.tagline}
{plan.description}
{isDiscounted && ( <> {formatPrice(originalTotalPrice)} {currencySign} {isRu ? `Выгода ${discountPercent}%` : `Save ${discountPercent}%`} )}
{isRu ? "" : currencySign} {formatPrice(monthlyEquivalent)} {isRu ? ` ${currencySign}` : ""} {dict.perMonth}
{period !== "1m" && (
{isRu ? "Итого к оплате: " : "Total billed: "} {isRu ? "" : currencySign} {formatPrice(totalPrice)} {isRu ? ` ${currencySign}` : ""} {" "} {isRu ? `за ${months} мес.` : `per ${months} months`}
)}
    {plan.features .split("\n") .filter((f) => f.trim() !== "") .map((feature, i) => (
  • {feature.trim()}
  • ))}
); })}
)}
); }