Skip to content

armgit / E2B

Public Imported from GitHub
0

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

add: skeletons + usage limits components

Ben Fornefeld54c8315ca427
diff --git a/apps/web/src/components/Dashboard/Usage.tsx b/apps/web/src/components/Dashboard/Usage.tsx
index 23d151c2e..fe6b2b149 100644
--- a/apps/web/src/components/Dashboard/Usage.tsx
+++ b/apps/web/src/components/Dashboard/Usage.tsx
@@ -1,10 +1,11 @@
import { useEffect, useState } from 'react'
-import { Card, CardContent } from '@/components/ui/card'
+import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import LineChart from '@/components/Dashboard/Chart'
-import Spinner from '@/components/Spinner'
import { toast } from '@/components/ui/use-toast'
import { Team } from '@/utils/useUser'
import { getBillingUrl } from '@/app/(dashboard)/dashboard/utils'
+import { cn } from '@/lib/utils'
+import { Skeleton } from '../ui/skeleton'
type Usage = {
month: number
@@ -22,6 +23,25 @@ type Series = {
data: PlotData[]
}
+const ChartSkeleton = ({ className }: { className?: string }) => {
+ const numBars = Math.floor(Math.random() * 6) + 5 // Random number between 5-10
+ const bars = Array.from({ length: numBars }, () => Math.random() * 80 + 20) // Random heights between 20-100
+
+ return (
+ <div className={cn('relative w-full aspect-video p-4', className)}>
+ <div className="flex h-full items-end justify-between gap-2">
+ {bars.map((height, i) => (
+ <Skeleton
+ key={i}
+ className="w-full"
+ style={{ height: `${height}%` }}
+ />
+ ))}
+ </div>
+ </div>
+ )
+}
+
export const UsageContent = ({
team,
domain,
@@ -39,133 +59,179 @@ export const UsageContent = ({
>()
const [costUsage, setCostUsage] = useState<Series[]>([])
const [costThisMonth, setCostMonth] = useState<number | undefined>()
+ const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
const getUsage = async () => {
+ setIsLoading(true)
setVcpuData([])
setRamData([])
setCostUsage([])
- const response = await fetch(
- getBillingUrl(domain, `/teams/${team.id}/usage`),
- {
- headers: {
- 'X-Team-API-Key': team.apiKeys[0],
- },
+ try {
+ const response = await fetch(
+ getBillingUrl(domain, `/teams/${team.id}/usage`),
+ {
+ headers: {
+ 'X-Team-API-Key': team.apiKeys[0],
+ },
+ }
+ )
+ if (!response.ok) {
+ throw new Error('Failed to fetch usage data')
}
- )
- if (!response.ok) {
- // TODO: Add sentry event here
+
+ const data = await response.json()
+ const { vcpuSeries, ramSeries } = transformData(data.usages)
+ const costData = transformCostData(data.usages)
+
+ const latestCost = costData[0].data[costData[0].data.length - 1]
+ setCostUsage(costData)
+ setCostMonth(latestCost.y)
+
+ setVcpuData(vcpuSeries)
+ setVcpuHoursThisMonth(
+ vcpuSeries[0].data[vcpuSeries[0].data.length - 1].y
+ )
+
+ setRamData(ramSeries)
+ setRamHoursThisMonth(ramSeries[0].data[ramSeries[0].data.length - 1].y)
+ } catch (error) {
toast({
title: 'An error occurred',
description: 'We were unable to fetch the usage data',
})
- console.log(response)
- return
+ console.error(error)
+ } finally {
+ setIsLoading(false)
}
-
- const data = await response.json()
- const { vcpuSeries, ramSeries } = transformData(data.usages)
-
- const costData = transformCostData(data.usages)
- const latestCost = costData[0].data[costData[0].data.length - 1]
- setCostUsage(costData)
- setCostMonth(latestCost.y)
-
- setVcpuData(vcpuSeries)
- setVcpuHoursThisMonth(vcpuSeries[0].data[vcpuSeries[0].data.length - 1].y)
-
- setRamData(ramSeries)
- setRamHoursThisMonth(ramSeries[0].data[ramSeries[0].data.length - 1].y)
}
getUsage()
}, [team, domain])
return (
- <div className="flex flex-col w-full">
- <div className="pb-10">
- <h2 className="font-bold pb-4 text-xl">Usage history</h2>
- <p>
- The graphs show the total monthly vCPU-hours and RAM-hours used by the
- team. <br />
- </p>
- </div>
-
- <div className="flex flex-col 2xl:flex-row w-full space-y-4 2xl:space-y-0 2xl:space-x-4">
- <div className="flex flex-col w-full md:w-2/3">
- <h2 className="font-bold pb-4 text-xl">Costs in USD</h2>
- {costUsage && costUsage.length > 0 ? (
- <div className="flex flex-col space-y-2">
- <span className="text-sm text-white/50">
+ <div className="flex flex-col w-full gap-3">
+ {/* First Row - Costs and Budget Controls */}
+ <div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-3">
+ {/* Cost Card - Full width mobile, half tablet, third desktop */}
+ <Card className="bg-inherit/10 border border-white/20 col-span-1 lg:col-span-2 xl:col-span-1">
+ <CardHeader className="pb-2">
+ <CardTitle>Costs in USD</CardTitle>
+ {!isLoading && costThisMonth && (
+ <p className="text-sm text-white/50">
Total costs this month: <b>${costThisMonth?.toFixed(2)}</b>
- </span>
- <Card className="w-full bg-inherit/10 border border-white/20 mb-10">
- <CardContent>
- <LineChart
- className="aspect-[4/3]"
- series={costUsage}
- type="Cost"
- />
- </CardContent>
- </Card>
+ </p>
+ )}
+ </CardHeader>
+ <CardContent>
+ {isLoading ? (
+ <ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />
+ ) : (
+ <LineChart
+ series={costUsage}
+ type="Cost"
+ className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"
+ />
+ )}
+ </CardContent>
+ </Card>
+
+ {/* Budget Controls */}
+ <Card className="bg-inherit/10 border border-white/20">
+ <CardHeader className="pb-2">
+ <CardTitle>Budget Alerts</CardTitle>
+ </CardHeader>
+ <CardContent className="space-y-3">
+ <div className="space-y-2">
+ <h3 className="font-medium">Set a Budget Alert</h3>
+ <p className="text-sm text-white/70">
+ If your organization exceeds this threshold in a given calendar
+ month (UTC), an alert notification will be sent to this email.
+ </p>
+ <input
+ type="text"
+ value="$35.00"
+ className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2"
+ />
</div>
- ) : (
- <div className="flex items-center justify-center w-full">
- <Spinner />
+ </CardContent>
+ </Card>
+
+ <Card className="bg-inherit/10 border border-white/20">
+ <CardHeader className="pb-2">
+ <CardTitle>Budget Limits</CardTitle>
+ </CardHeader>
+ <CardContent className="space-y-3">
+ <div className="space-y-2">
+ <h3 className="font-medium">Enable Budget Limit</h3>
+ <p className="text-sm text-white/70">
+ If your organization exceeds this budget in a given calendar
+ month (UTC), subsequent API requests will be rejected.
+ </p>
+ <p className="text-sm text-red-400">
+ Caution: Enabling a Budget Limit could result in interruptions
+ to your service.
+ </p>
+ <input
+ type="text"
+ value="$50.00"
+ className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2"
+ />
</div>
- )}
- </div>
-
- <div className="flex flex-col w-full md:w-2/3">
- <h2 className="font-bold pb-4 text-xl">vCPU hours</h2>
- {vcpuData && vcpuData.length > 0 ? (
- <div className="flex flex-col space-y-2">
- <span className="text-sm text-white/50">
+ </CardContent>
+ </Card>
+ </div>
+
+ {/* Second Row - Resource Usage */}
+ <div className="grid grid-cols-1 xl:grid-cols-2 gap-3">
+ {/* vCPU Card - Always half width except mobile */}
+ <Card className="bg-inherit/10 border border-white/20">
+ <CardHeader className="pb-2">
+ <CardTitle>vCPU hours</CardTitle>
+ {!isLoading && vcpuHoursThisMonth && (
+ <p className="text-sm text-white/50">
Total vCPU hours this month:{' '}
<b>{vcpuHoursThisMonth?.toFixed(2)}</b>
- </span>
- <Card className="w-full bg-inherit/10 border border-white/20 mb-10">
- <CardContent>
- <LineChart
- className="aspect-[4/3]"
- series={vcpuData}
- type="vCPU"
- />
- </CardContent>
- </Card>
- </div>
- ) : (
- <div className="flex items-center justify-center w-full">
- <Spinner />
- </div>
- )}
- </div>
-
- <div className="flex flex-col w-full md:w-2/3">
- <h2 className="font-bold pb-4 text-xl">RAM hours</h2>
- {ramData && ramData.length > 0 ? (
- <div className="flex flex-col space-y-2">
- <span className="text-sm text-white/50">
+ </p>
+ )}
+ </CardHeader>
+ <CardContent>
+ {isLoading ? (
+ <ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />
+ ) : (
+ <LineChart
+ series={vcpuData}
+ type="vCPU"
+ className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"
+ />
+ )}
+ </CardContent>
+ </Card>
+
+ {/* RAM Card - Always half width except mobile */}
+ <Card className="bg-inherit/10 border border-white/20">
+ <CardHeader className="pb-2">
+ <CardTitle>RAM hours</CardTitle>
+ {!isLoading && ramHoursThisMonth && (
+ <p className="text-sm text-white/50">
Total RAM hours this month:{' '}
<b>{ramHoursThisMonth?.toFixed(2)}</b>
- </span>
- <Card className="w-full bg-inherit/10 border border-white/20 mb-10">
- <CardContent>
- <LineChart
- className="aspect-[4/3]"
- series={ramData}
- type="RAM"
- />
- </CardContent>
- </Card>
- </div>
- ) : (
- <div className="flex items-center justify-center w-full">
- <Spinner />
- </div>
- )}
- </div>
+ </p>
+ )}
+ </CardHeader>
+ <CardContent>
+ {isLoading ? (
+ <ChartSkeleton className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]" />
+ ) : (
+ <LineChart
+ series={ramData}
+ type="RAM"
+ className="aspect-[21/9] xs:aspect-[2/1] lg:aspect-[16/9]"
+ />
+ )}
+ </CardContent>
+ </Card>
</div>
</div>
)
diff --git a/apps/web/src/components/ui/skeleton.tsx b/apps/web/src/components/ui/skeleton.tsx
new file mode 100644
index 000000000..dfb99c9aa
--- /dev/null
+++ b/apps/web/src/components/ui/skeleton.tsx
@@ -0,0 +1,15 @@
+import { cn } from '@/lib/utils'
+
+function Skeleton({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) {
+ return (
+ <div
+ className={cn('animate-pulse rounded-md bg-white/5', className)}
+ {...props}
+ />
+ )
+}
+
+export { Skeleton }