Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fetch usage data

Vasek Mlejnsky84a43bb4740c
diff --git a/apps/docs/src/app/usage/Credits.tsx b/apps/docs/src/app/usage/Credits.tsx
index c73d00094..cf46098dc 100644
--- a/apps/docs/src/app/usage/Credits.tsx
+++ b/apps/docs/src/app/usage/Credits.tsx
@@ -6,6 +6,7 @@ import {
} from 'lucide-react'
import Link from 'next/link'
+import SpinnerIcon from '@/components/Spinner'
import ManageBilling from '@/components/ManageBilling'
import { useUsage } from '@/utils/useUsage'
import { useUser } from '@/utils/useUser'
@@ -16,7 +17,7 @@ function formatCurrency(value: number) {
function Credits() {
const { user } = useUser()
- const { credits, costs } = useUsage()
+ const { credits, usage, isLoading } = useUsage()
if (!user) {
return (
@@ -39,25 +40,28 @@ function Credits() {
<span className="font-medium text-white">Credits left</span>
</div>
<span className="text-sm">Will be used when generating invoice</span>
- <span className="text-sm font-mono text-green-300/80">${credits}</span>
+ {isLoading && <SpinnerIcon />}
+ {!isLoading && (
+ <span className="text-sm font-mono text-green-300/80">${credits}</span>
+ )}
</div>
<div className="flex flex-col justify-start items-start gap-1">
- {costs.map((cost, index) => (
+ {usage.map((u, index) => (
<React.Fragment key={index}>
<h3 className="text-xl">
- {new Date(cost.year, cost.month - 1).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
+ {new Date(u.year, u.month - 1).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
</h3>
<div className="flex flex-col justify-start items-start gap-4">
<div className="flex flex-col items-start justify-start gap-1">
<span className="font-bold">Total costs</span>
- <span className="text-sm font-mono">${formatCurrency(cost.total_costs)}</span>
+ <span className="text-sm font-mono">${formatCurrency(u.total_cost)}</span>
</div>
<div className="flex flex-col items-start justify-start gap-1">
- <span className="font-bold">{new Date().getMonth() + 1 === cost.month ? 'To pay' : 'Paid'}</span>
- <span className="text-sm font-mono">${formatCurrency(cost.unpaid_costs)}</span>
+ <span className="font-bold">{new Date().getMonth() + 1 === u.month ? 'To pay' : 'Paid'}</span>
+ <span className="text-sm font-mono">${formatCurrency(u.unpaid_cost)}</span>
</div>
</div>
</React.Fragment>
diff --git a/apps/docs/src/utils/useUsage.tsx b/apps/docs/src/utils/useUsage.tsx
index eaf430e18..1d5a2eaa5 100644
--- a/apps/docs/src/utils/useUsage.tsx
+++ b/apps/docs/src/utils/useUsage.tsx
@@ -6,20 +6,25 @@ import {
useCallback,
} from 'react'
-import { useUser } from '@/utils/useUser'
+import { useApiKey, useUser } from '@/utils/useUser'
import { createPagesBrowserClient } from '@supabase/auth-helpers-nextjs'
export interface MonthlyCosts {
month: number
year: number
- total_costs: number
- unpaid_costs: number
+ total_cost: number
+ unpaid_cost: number
}
+const usageUrl = `${process.env.NEXT_PUBLIC_BILLING_API_URL}/usage`
+const creditsUrl = `${process.env.NEXT_PUBLIC_BILLING_API_URL}/credits`
+
export function useUsage() {
const { user } = useUser()
+ const apiKey = useApiKey()
const [credits, setCredits] = useState(null)
- const [costs, setCosts] = useState<MonthlyCosts[]>([])
+ const [usage, setUsage] = useState<MonthlyCosts[]>([])
+ const [isLoading, setIsLoading] = useState(true)
const supabase = createPagesBrowserClient()
const fetchCredits = useCallback(async function fetchCredits() {
@@ -27,31 +32,48 @@ export function useUsage() {
const { teams } = user
if (!teams || !supabase) return
if (teams.length === 0) return
+ if (!apiKey) return
- // const teamID = teams[0].id
+ const [
+ { credits: c },
+ u,
+ ] = await Promise.all([
+ fetch(creditsUrl, {
+ headers: {
+ 'X-Team-API-Key': apiKey,
+ },
+ }).then(res => res.json()),
+ fetch(usageUrl, {
+ headers: {
+ 'X-Team-API-Key': apiKey,
+ },
+ }).then(res => res.json()),
+ ])
// TODO: Fetch from API server
- setCredits(100)
- setCosts([
- {
- month: 2,
- year: 2024,
- total_costs: 23.85,
- unpaid_costs: 23.85,
- },
- {
- month: 1,
- year: 2024,
- total_costs: 232,
- unpaid_costs: 0,
- },
- {
- month: 12,
- year: 2023,
- total_costs: 32.221,
- unpaid_costs: 0,
- },
- ])
+ setCredits(c)
+ setUsage(u)
+ setIsLoading(false)
+ // setCosts([
+ // {
+ // month: 2,
+ // year: 2024,
+ // total_costs: 23.85,
+ // unpaid_costs: 23.85,
+ // },
+ // {
+ // month: 1,
+ // year: 2024,
+ // total_costs: 232,
+ // unpaid_costs: 0,
+ // },
+ // {
+ // month: 12,
+ // year: 2023,
+ // total_costs: 32.221,
+ // unpaid_costs: 0,
+ // },
+ // ])
}, [user, supabase])
@@ -61,6 +83,7 @@ export function useUsage() {
return {
credits,
- costs,
+ usage,
+ isLoading,
}
}
\ No newline at end of file