"use client"; import { useQuery as useZeroQuery } from "@rocicorp/zero/react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { Minus, Plus } from "lucide-react"; import { useParams } from "next/navigation"; import { useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Progress } from "@/components/ui/progress"; import { Spinner } from "@/components/ui/spinner"; import { stripeApiService } from "@/lib/apis/stripe-api.service"; import { AppError } from "@/lib/error"; import { cn } from "@/lib/utils"; import { queries } from "@/zero/queries"; // One pack = $1.00 of credit, stored as 1_000_000 micro-USD on the // backend. Premium turns are debited at the actual provider cost // reported by LiteLLM, so $1 of credit always buys $1 of provider // usage at cost. const CREDIT_PER_PACK_MICROS = 1_000_000; const PRICE_PER_PACK_USD = 1; const PRESET_MULTIPLIERS = [1, 2, 5, 10, 25, 50] as const; const formatUsd = (micros: number, options?: { compact?: boolean }) => { const dollars = micros / 1_000_000; if (options?.compact && dollars >= 1) return `$${dollars.toFixed(2)}`; if (dollars >= 100) return `$${dollars.toFixed(0)}`; if (dollars >= 1) return `$${dollars.toFixed(2)}`; if (dollars > 0) return `$${dollars.toFixed(3)}`; return "$0"; }; export function BuyTokensContent() { const params = useParams(); const searchSpaceId = Number(params?.search_space_id); const [quantity, setQuantity] = useState(1); // Server config flag: stays on REST, not per-user. const { data: tokenStatus } = useQuery({ queryKey: ["token-status"], queryFn: () => stripeApiService.getTokenStatus(), }); // Live per-user balance via Zero. const [me] = useZeroQuery(queries.user.me({})); const purchaseMutation = useMutation({ mutationFn: stripeApiService.createTokenCheckoutSession, onSuccess: (response) => { window.location.assign(response.checkout_url); }, onError: (error) => { if (error instanceof AppError && error.message) { toast.error(error.message); return; } toast.error("Failed to start checkout. Please try again."); }, }); const totalCreditMicros = quantity * CREDIT_PER_PACK_MICROS; const totalPrice = quantity * PRICE_PER_PACK_USD; if (tokenStatus && !tokenStatus.token_buying_enabled) { return (

Buy Premium Credit

Credit purchases are temporarily unavailable.

); } const used = me?.premiumCreditMicrosUsed ?? 0; const limit = me?.premiumCreditMicrosLimit ?? 0; // Mirrors the backend formula in stripe_routes.py (max(0, limit - used)). const remaining = Math.max(0, limit - used); const usagePercentage = me ? Math.min((used / Math.max(limit, 1)) * 100, 100) : 0; return (

Buy Premium Credit

$1 buys $1 of credit, billed at provider cost

{me && (
{formatUsd(used)} / {formatUsd(limit)} of credit {usagePercentage.toFixed(0)}%

{formatUsd(remaining)} of credit remaining

)}
${(totalCreditMicros / 1_000_000).toFixed(0)} of credit
{PRESET_MULTIPLIERS.map((m) => ( ))}
${(totalCreditMicros / 1_000_000).toFixed(0)} of credit ${totalPrice}

Secure checkout via Stripe

); }