import { json, query, action, useParams, createAsync, useSubmission } from "@solidjs/router" import { createEffect, Show } from "solid-js" import { createStore } from "solid-js/store" import { withActor } from "~/context/auth.withActor" import { Billing } from "@opencode/console-core/billing.js" import styles from "./monthly-limit-section.module.css" const getBillingInfo = query(async (workspaceID: string) => { "use server" return withActor(async () => { return await Billing.get() }, workspaceID) }, "billing.get") const setMonthlyLimit = action(async (form: FormData) => { "use server" const limit = form.get("limit")?.toString() if (!limit) return { error: "Limit is required." } const numericLimit = parseInt(limit) if (numericLimit < 0) return { error: "Set a valid monthly limit." } const workspaceID = form.get("workspaceID")?.toString() if (!workspaceID) return { error: "Workspace ID is required." } return json( await withActor( () => Billing.setMonthlyLimit(numericLimit) .then((data) => ({ error: undefined, data })) .catch((e) => ({ error: e.message as string })), workspaceID, ), { revalidate: getBillingInfo.key }, ) }, "billing.setMonthlyLimit") export function MonthlyLimitSection() { const params = useParams() const submission = useSubmission(setMonthlyLimit) const [store, setStore] = createStore({ show: false }) const balanceInfo = createAsync(() => getBillingInfo(params.id)) let input: HTMLInputElement createEffect(() => { if (!submission.pending && submission.result && !submission.result.error) { hide() } }) function show() { // submission.clear() does not clear the result in some cases, ie. // 1. Create key with empty name => error shows // 2. Put in a key name and creates the key => form hides // 3. Click add key button again => form shows with the same error if // submission.clear() is called only once while (true) { submission.clear() if (!submission.result) break } setStore("show", true) input.focus() } function hide() { setStore("show", false) } return (

Monthly Limit

Set a monthly spending limit for your account.

{balanceInfo()?.monthlyLimit ? $ : null} {balanceInfo()?.monthlyLimit ?? "-"}
(input = r)} data-component="input" name="limit" type="number" placeholder="50" /> {(err) =>
{err()}
}
} >
No spending limit set.

}>

Current usage for {new Date().toLocaleDateString("en-US", { month: "long", timeZone: "UTC" })} is $ {(() => { const dateLastUsed = balanceInfo()?.timeMonthlyUsageUpdated if (!dateLastUsed) return "0" const current = new Date().toLocaleDateString("en-US", { year: "numeric", month: "long", timeZone: "UTC", }) const lastUsed = dateLastUsed.toLocaleDateString("en-US", { year: "numeric", month: "long", timeZone: "UTC", }) if (current !== lastUsed) return "0" return ((balanceInfo()?.monthlyUsage ?? 0) / 100000000).toFixed(2) })()} .

) }