import { eq } from "drizzle-orm"; import { redirect } from "next/navigation"; import { getTranslations } from "next-intl/server"; import { ContentCard, StatBlock } from "@/components/public/ui"; import { auth } from "@/lib/auth"; import { db, User } from "@/lib/db"; import { creditsPerUnit, isPayPalConfigured, PAYPAL_CURRENCY, } from "@/lib/services/paypal"; import TopUpForm from "./TopUpForm"; export default async function TopUpPage({ searchParams, }: { searchParams: Promise<{ status?: string }>; }) { const session = await auth(); if (!session?.user?.id) redirect("/login"); const t = await getTranslations("pages.topup"); const sp = await searchParams; const id = Number(session.user.id); // Current credits balance for context (fail soft). let user: { username: string; credits: number } | null = null; try { const [row] = await db .select({ username: User.username, credits: User.credits }) .from(User) .where(eq(User.id, id)) .limit(1); user = row ?? null; } catch { user = null; } if (!user) redirect("/login"); const configured = isPayPalConfigured(); const rate = creditsPerUnit(); return (
{sp.status === "cancel" ? (

{t("cancelled")}

) : null}
{configured ? ( ) : (

{t("notConfiguredPre")} PAYPAL_CLIENT_ID{" "} {t("notConfiguredAnd")} PAYPAL_SECRET{" "} {t("notConfiguredOptional")} PAYPAL_API{" "} {t("notConfiguredPost")}

)}

{t.rich("rateLine", { amount: rate.toLocaleString(), currency: PAYPAL_CURRENCY, strong: (chunks) => {chunks}, })}

); }