|
1 | 1 | import { Progress } from "@/components/ui/progress" |
2 | 2 | import { useEffect, useState } from "react"; |
3 | 3 | 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'; |
6 | 6 |
|
7 | 7 | export function UsageProgressBar() { |
8 | 8 | 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) |
9 | 12 | const supabase = createClient() |
10 | 13 |
|
11 | | - |
12 | 14 | useEffect(() => { |
13 | 15 | const fetchData = async () => { |
14 | 16 | const { data } = await supabase.from('usages').select().maybeSingle() |
15 | 17 | 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)) |
16 | 25 | } |
17 | 26 | fetchData() |
18 | | - }, [supabase]) // Added supabase to the dependency array |
19 | | - const percentage = Math.min((usages / MAX_REQUESTS_PER_MONTH) * 100, 100) |
| 27 | + }, []) |
20 | 28 |
|
21 | 29 | return ( |
22 | 30 | <div className="px-4 space-y-2"> |
23 | 31 | <div className="flex justify-between text-sm"> |
24 | 32 | <span>Requests this month</span> |
25 | 33 | <span> |
26 | | - {usages} / {MAX_REQUESTS_PER_MONTH} |
| 34 | + {usages} / {maxRequestsPerMonth} |
27 | 35 | </span> |
28 | 36 | </div> |
29 | 37 | <Progress value={percentage} className="w-full" /> |
| 38 | + {isPro && <Badge>Pro</Badge>} |
30 | 39 | </div> |
31 | 40 | ) |
32 | 41 | } |
|
0 commit comments