import { Button } from "../../ui/button" import { useApi } from "../components/context-api" import { createEffect, createSignal, createResource, For } from "solid-js" import { useWorkspace } from "../components/context-workspace" import style from "./billing.module.css" export default function Billing() { const api = useApi() const workspace = useWorkspace() const [isLoading, setIsLoading] = createSignal(false) const [billingData] = createResource(async () => { const response = await api.billing.info.$get() return response.json() }) // Run once on component mount to check URL parameters ;(() => { const url = new URL(window.location.href) const result = url.hash console.log("STRIPE RESULT", result) if (url.hash === "#success") { setIsLoading(true) // Remove the hash from the URL window.history.replaceState(null, "", window.location.pathname + window.location.search) } })() createEffect((old?: number) => { if (old && old !== billingData()?.billing?.balance) { setIsLoading(false) } return billingData()?.billing?.balance }) const handleBuyCredits = async () => { try { setIsLoading(true) const baseUrl = window.location.href const successUrl = new URL(baseUrl) successUrl.hash = "success" const response = await api.billing.checkout .$post({ json: { success_url: successUrl.toString(), cancel_url: baseUrl, }, }) .then((r) => r.json() as any) window.location.href = response.url } catch (error) { console.error("Failed to get checkout URL:", error) setIsLoading(false) } } return ( <>
Manage your billing and add credits to your account.
{(() => { const balanceStr = ((billingData()?.billing?.balance ?? 0) / 100000000).toFixed(2) return `$${balanceStr === "-0.00" ? "0.00" : balanceStr}` })()}
Your recent payment transactions.
Your recent API usage and costs.