|
@@ -4,7 +4,7 @@ import { Key } from "@opencode/cloud-core/key.js"
|
|
|
import { json, query, action, useParams, useAction, createAsync, useSubmission } from "@solidjs/router"
|
|
import { json, query, action, useParams, useAction, createAsync, useSubmission } from "@solidjs/router"
|
|
|
import { createEffect, createMemo, createSignal, For, Show } from "solid-js"
|
|
import { createEffect, createMemo, createSignal, For, Show } from "solid-js"
|
|
|
import { withActor } from "~/context/auth.withActor"
|
|
import { withActor } from "~/context/auth.withActor"
|
|
|
-import { IconCopy, IconCheck } from "~/component/icon"
|
|
|
|
|
|
|
+import { IconCopy, IconCheck, IconCreditCard } from "~/component/icon"
|
|
|
import { createStore } from "solid-js/store"
|
|
import { createStore } from "solid-js/store"
|
|
|
|
|
|
|
|
function formatDateForTable(date: Date) {
|
|
function formatDateForTable(date: Date) {
|
|
@@ -73,36 +73,68 @@ const removeKey = action(async (form: FormData) => {
|
|
|
// Billing related queries and actions
|
|
// Billing related queries and actions
|
|
|
/////////////////////////////////////
|
|
/////////////////////////////////////
|
|
|
|
|
|
|
|
-const getBalanceInfo = query(async (workspaceID: string) => {
|
|
|
|
|
|
|
+const getBillingInfo = query(async (workspaceID: string) => {
|
|
|
"use server"
|
|
"use server"
|
|
|
return withActor(async () => {
|
|
return withActor(async () => {
|
|
|
return await Billing.get()
|
|
return await Billing.get()
|
|
|
}, workspaceID)
|
|
}, workspaceID)
|
|
|
-}, "balanceInfo")
|
|
|
|
|
|
|
+}, "billing.get")
|
|
|
|
|
|
|
|
const getUsageInfo = query(async (workspaceID: string) => {
|
|
const getUsageInfo = query(async (workspaceID: string) => {
|
|
|
"use server"
|
|
"use server"
|
|
|
return withActor(async () => {
|
|
return withActor(async () => {
|
|
|
return await Billing.usages()
|
|
return await Billing.usages()
|
|
|
}, workspaceID)
|
|
}, workspaceID)
|
|
|
-}, "usageInfo")
|
|
|
|
|
|
|
+}, "usage.list")
|
|
|
|
|
|
|
|
const getPaymentsInfo = query(async (workspaceID: string) => {
|
|
const getPaymentsInfo = query(async (workspaceID: string) => {
|
|
|
"use server"
|
|
"use server"
|
|
|
return withActor(async () => {
|
|
return withActor(async () => {
|
|
|
return await Billing.payments()
|
|
return await Billing.payments()
|
|
|
}, workspaceID)
|
|
}, workspaceID)
|
|
|
-}, "paymentsInfo")
|
|
|
|
|
|
|
+}, "payment.list")
|
|
|
|
|
+
|
|
|
|
|
+const setMonthlyLimit = action(async (form: FormData) => {
|
|
|
|
|
+ "use server"
|
|
|
|
|
+ const limit = form.get("limit")?.toString()
|
|
|
|
|
+ if (!limit) return { error: "Limit is required" }
|
|
|
|
|
+ const workspaceID = form.get("workspaceID")?.toString()
|
|
|
|
|
+ if (!workspaceID) return { error: "Workspace ID is required" }
|
|
|
|
|
+ return json(
|
|
|
|
|
+ await withActor(
|
|
|
|
|
+ () =>
|
|
|
|
|
+ Billing.setMonthlyLimit(parseInt(limit))
|
|
|
|
|
+ .then((data) => ({ error: undefined, data }))
|
|
|
|
|
+ .catch((e) => ({ error: e.message as string })),
|
|
|
|
|
+ workspaceID,
|
|
|
|
|
+ ),
|
|
|
|
|
+ { revalidate: getBillingInfo.key },
|
|
|
|
|
+ )
|
|
|
|
|
+}, "billing.setMonthlyLimit")
|
|
|
|
|
+
|
|
|
|
|
+const reload = action(async (form: FormData) => {
|
|
|
|
|
+ "use server"
|
|
|
|
|
+ const workspaceID = form.get("workspaceID")?.toString()
|
|
|
|
|
+ if (!workspaceID) return { error: "Workspace ID is required" }
|
|
|
|
|
+ return json(await withActor(() => Billing.reload(), workspaceID), { revalidate: getBillingInfo.key })
|
|
|
|
|
+}, "billing.reload")
|
|
|
|
|
+
|
|
|
|
|
+const disableReload = action(async (form: FormData) => {
|
|
|
|
|
+ "use server"
|
|
|
|
|
+ const workspaceID = form.get("workspaceID")?.toString()
|
|
|
|
|
+ if (!workspaceID) return { error: "Workspace ID is required" }
|
|
|
|
|
+ return json(await withActor(() => Billing.disableReload(), workspaceID), { revalidate: getBillingInfo.key })
|
|
|
|
|
+}, "billing.disableReload")
|
|
|
|
|
|
|
|
const createCheckoutUrl = action(async (workspaceID: string, successUrl: string, cancelUrl: string) => {
|
|
const createCheckoutUrl = action(async (workspaceID: string, successUrl: string, cancelUrl: string) => {
|
|
|
"use server"
|
|
"use server"
|
|
|
return withActor(() => Billing.generateCheckoutUrl({ successUrl, cancelUrl }), workspaceID)
|
|
return withActor(() => Billing.generateCheckoutUrl({ successUrl, cancelUrl }), workspaceID)
|
|
|
}, "checkoutUrl")
|
|
}, "checkoutUrl")
|
|
|
|
|
|
|
|
-// const createPortalUrl = action(async (workspaceID: string, returnUrl: string) => {
|
|
|
|
|
-// "use server"
|
|
|
|
|
-// return withActor(() => Billing.generatePortalUrl({ returnUrl }), workspaceID)
|
|
|
|
|
-// }, "portalUrl")
|
|
|
|
|
|
|
+const createSessionUrl = action(async (workspaceID: string, returnUrl: string) => {
|
|
|
|
|
+ "use server"
|
|
|
|
|
+ return withActor(() => Billing.generateSessionUrl({ returnUrl }), workspaceID)
|
|
|
|
|
+}, "sessionUrl")
|
|
|
|
|
|
|
|
function KeySection() {
|
|
function KeySection() {
|
|
|
const params = useParams()
|
|
const params = useParams()
|
|
@@ -248,9 +280,13 @@ function KeyCreateForm() {
|
|
|
|
|
|
|
|
function BalanceSection() {
|
|
function BalanceSection() {
|
|
|
const params = useParams()
|
|
const params = useParams()
|
|
|
- const balanceInfo = createAsync(() => getBalanceInfo(params.id))
|
|
|
|
|
|
|
+ const balanceInfo = createAsync(() => getBillingInfo(params.id))
|
|
|
const createCheckoutUrlAction = useAction(createCheckoutUrl)
|
|
const createCheckoutUrlAction = useAction(createCheckoutUrl)
|
|
|
const createCheckoutUrlSubmission = useSubmission(createCheckoutUrl)
|
|
const createCheckoutUrlSubmission = useSubmission(createCheckoutUrl)
|
|
|
|
|
+ const createSessionUrlAction = useAction(createSessionUrl)
|
|
|
|
|
+ const createSessionUrlSubmission = useSubmission(createSessionUrl)
|
|
|
|
|
+ const disableReloadSubmission = useSubmission(disableReload)
|
|
|
|
|
+ const reloadSubmission = useSubmission(reload)
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<section data-component="balance-section">
|
|
<section data-component="balance-section">
|
|
@@ -274,24 +310,176 @@ function BalanceSection() {
|
|
|
})()}
|
|
})()}
|
|
|
</span>
|
|
</span>
|
|
|
</div>
|
|
</div>
|
|
|
- <button
|
|
|
|
|
- data-color="primary"
|
|
|
|
|
- disabled={createCheckoutUrlSubmission.pending}
|
|
|
|
|
- onClick={async () => {
|
|
|
|
|
- const baseUrl = window.location.href
|
|
|
|
|
- const checkoutUrl = await createCheckoutUrlAction(params.id, baseUrl, baseUrl)
|
|
|
|
|
- if (checkoutUrl) {
|
|
|
|
|
- window.location.href = checkoutUrl
|
|
|
|
|
- }
|
|
|
|
|
- }}
|
|
|
|
|
|
|
+ <Show
|
|
|
|
|
+ when={balanceInfo()?.reload}
|
|
|
|
|
+ fallback={
|
|
|
|
|
+ <>
|
|
|
|
|
+ <button
|
|
|
|
|
+ data-color="primary"
|
|
|
|
|
+ disabled={createCheckoutUrlSubmission.pending}
|
|
|
|
|
+ onClick={async () => {
|
|
|
|
|
+ const baseUrl = window.location.href
|
|
|
|
|
+ const checkoutUrl = await createCheckoutUrlAction(params.id, baseUrl, baseUrl)
|
|
|
|
|
+ if (checkoutUrl) {
|
|
|
|
|
+ window.location.href = checkoutUrl
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ {createCheckoutUrlSubmission.pending ? "Loading..." : "Enable Billing"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <p>You can continue using the API with the remaining credits.</p>
|
|
|
|
|
+ </>
|
|
|
|
|
+ }
|
|
|
>
|
|
>
|
|
|
- {createCheckoutUrlSubmission.pending ? "Loading..." : "Buy Credits"}
|
|
|
|
|
- </button>
|
|
|
|
|
|
|
+ <>
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <p>
|
|
|
|
|
+ You will be automatically reloading <b>$20</b> (+$1.23 processing fee) when your balance reaches{" "}
|
|
|
|
|
+ <b>$5</b>.
|
|
|
|
|
+ </p>
|
|
|
|
|
+ <form action={disableReload} method="post" data-slot="create-form">
|
|
|
|
|
+ <input type="hidden" name="workspaceID" value={params.id} />
|
|
|
|
|
+ <button data-color="primary" type="submit" disabled={disableReloadSubmission.pending}>
|
|
|
|
|
+ {disableReloadSubmission.pending ? "Disabling..." : "Disable Billing"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ <p>You will be able to continue using the API with the remaining credits after disabling billing.</p>
|
|
|
|
|
+ <Show when={balanceInfo()?.reloadError}>
|
|
|
|
|
+ <>
|
|
|
|
|
+ <p>
|
|
|
|
|
+ Reload failed at{" "}
|
|
|
|
|
+ {balanceInfo()?.timeReloadError!.toLocaleString("en-US", {
|
|
|
|
|
+ month: "short",
|
|
|
|
|
+ day: "numeric",
|
|
|
|
|
+ hour: "numeric",
|
|
|
|
|
+ minute: "2-digit",
|
|
|
|
|
+ second: "2-digit",
|
|
|
|
|
+ })}{" "}
|
|
|
|
|
+ . Reason: {balanceInfo()?.reloadError?.replace(/\.$/, "")}. Please update your payment method and
|
|
|
|
|
+ try again.
|
|
|
|
|
+ </p>
|
|
|
|
|
+ <form action={reload} method="post" data-slot="create-form">
|
|
|
|
|
+ <input type="hidden" name="workspaceID" value={params.id} />
|
|
|
|
|
+ <button data-color="primary" type="submit" disabled={reloadSubmission.pending}>
|
|
|
|
|
+ {reloadSubmission.pending ? "Reloading..." : "Retry Reload"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ </>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <hr />
|
|
|
|
|
+ <div data-slot="amount">
|
|
|
|
|
+ <IconCreditCard style={{ width: "32px", height: "32px" }} />
|
|
|
|
|
+ <span data-slot="currency">••••</span>
|
|
|
|
|
+ <span data-slot="value">{balanceInfo()?.paymentMethodLast4}</span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <button
|
|
|
|
|
+ data-color="primary"
|
|
|
|
|
+ disabled={createSessionUrlSubmission.pending}
|
|
|
|
|
+ onClick={async () => {
|
|
|
|
|
+ const baseUrl = window.location.href
|
|
|
|
|
+ const sessionUrl = await createSessionUrlAction(params.id, baseUrl)
|
|
|
|
|
+ if (sessionUrl) {
|
|
|
|
|
+ window.location.href = sessionUrl
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ {createSessionUrlSubmission.pending ? "Loading..." : "Manage Payment Methods"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <hr />
|
|
|
|
|
+ <Show when={balanceInfo()?.monthlyLimit} fallback={<p>No spending limit set.</p>}>
|
|
|
|
|
+ <p>
|
|
|
|
|
+ Spending limit is ${balanceInfo()?.monthlyLimit ?? 0}. Current usage for the month of{" "}
|
|
|
|
|
+ {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)
|
|
|
|
|
+ })()}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ <BalanceLimitForm />
|
|
|
|
|
+ </>
|
|
|
|
|
+ </Show>
|
|
|
</div>
|
|
</div>
|
|
|
</section>
|
|
</section>
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function BalanceLimitForm() {
|
|
|
|
|
+ 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 (
|
|
|
|
|
+ <Show
|
|
|
|
|
+ when={store.show}
|
|
|
|
|
+ fallback={
|
|
|
|
|
+ <button data-color="primary" onClick={() => show()}>
|
|
|
|
|
+ {balanceInfo()?.monthlyLimit ? "Edit Spending Limit" : "Set Spending Limit"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ }
|
|
|
|
|
+ >
|
|
|
|
|
+ <form action={setMonthlyLimit} method="post" data-slot="create-form">
|
|
|
|
|
+ <div data-slot="input-container">
|
|
|
|
|
+ <input ref={(r) => (input = r)} data-component="input" name="limit" type="number" placeholder="Enter limit" />
|
|
|
|
|
+ <Show when={submission.result && submission.result.error}>
|
|
|
|
|
+ {(err) => <div data-slot="form-error">{err()}</div>}
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <input type="hidden" name="workspaceID" value={params.id} />
|
|
|
|
|
+ <div data-slot="form-actions">
|
|
|
|
|
+ <button type="reset" data-color="ghost" onClick={() => hide()}>
|
|
|
|
|
+ Cancel
|
|
|
|
|
+ </button>
|
|
|
|
|
+ <button type="submit" data-color="primary" disabled={submission.pending}>
|
|
|
|
|
+ {submission.pending ? "Setting..." : "Set"}
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </form>
|
|
|
|
|
+ </Show>
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function UsageSection() {
|
|
function UsageSection() {
|
|
|
const params = useParams()
|
|
const params = useParams()
|
|
|
const usage = createAsync(() => getUsageInfo(params.id))
|
|
const usage = createAsync(() => getUsageInfo(params.id))
|
|
@@ -349,6 +537,7 @@ function UsageSection() {
|
|
|
function PaymentSection() {
|
|
function PaymentSection() {
|
|
|
const params = useParams()
|
|
const params = useParams()
|
|
|
const payments = createAsync(() => getPaymentsInfo(params.id))
|
|
const payments = createAsync(() => getPaymentsInfo(params.id))
|
|
|
|
|
+ console.log("!#!@", payments())
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
payments() &&
|
|
payments() &&
|