Skip to content

Commit d264be3

Browse files
committed
Update usage bar.
1 parent 73cf219 commit d264be3

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

packages/dashboard/src/app/(layout)/components/UsageProgressBar.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
import { Progress } from "@/components/ui/progress"
22
import { useEffect, useState } from "react";
33
import { createClient } from "@/utils/supabase/client";
4-
5-
const MAX_REQUESTS_PER_MONTH = 1000
4+
import { PLANS } from '@/utils/constants';
5+
import { Badge } from '@/components/ui/badge';
66

77
export function UsageProgressBar() {
88
const [usages, setUsages] = useState<number>(0)
9+
const [percentage, setPercentage] = useState<number>(0)
10+
const [maxRequestsPerMonth, setMaxRequestsPerMonth] = useState<number>(0)
11+
const [isPro, setIsPro] = useState<boolean>(false)
912
const supabase = createClient()
1013

11-
1214
useEffect(() => {
1315
const fetchData = async () => {
1416
const { data } = await supabase.from('usages').select().maybeSingle()
1517
setUsages(data?.calls_count ?? 0)
18+
19+
const user = await supabase.auth.getUser()
20+
const { data: proSubscriptionExists } = await supabase.rpc('check_subscription', { p_user_id: user.data.user?.id })
21+
22+
setIsPro(proSubscriptionExists)
23+
setMaxRequestsPerMonth(proSubscriptionExists ? PLANS.PRO.REQUESTS_PER_MONTH : PLANS.BASIC.REQUESTS_PER_MONTH)
24+
setPercentage(Math.min((usages / maxRequestsPerMonth) * 100, 100))
1625
}
1726
fetchData()
18-
}, [supabase]) // Added supabase to the dependency array
19-
const percentage = Math.min((usages / MAX_REQUESTS_PER_MONTH) * 100, 100)
27+
}, [])
2028

2129
return (
2230
<div className="px-4 space-y-2">
2331
<div className="flex justify-between text-sm">
2432
<span>Requests this month</span>
2533
<span>
26-
{usages} / {MAX_REQUESTS_PER_MONTH}
34+
{usages} / {maxRequestsPerMonth}
2735
</span>
2836
</div>
2937
<Progress value={percentage} className="w-full" />
38+
{isPro && <Badge>Pro</Badge>}
3039
</div>
3140
)
3241
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

22
export const DAY_MS = 24 * 60 * 60 * 1000;
33

4-
export const EXTERNAL_ENDPOINTS_SOURCES = ['openapi', 'postman'];
4+
export const EXTERNAL_ENDPOINTS_SOURCES = ['openapi', 'postman'];
5+
6+
export const PLANS = {
7+
BASIC: {
8+
REQUESTS_PER_MONTH: 100
9+
},
10+
PRO: {
11+
REQUESTS_PER_MONTH: 10000
12+
}
13+
} as const;

0 commit comments

Comments
 (0)