subscription-status.tsx193 lines, 6.2 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { Progress } from "@/components/ui/progress"; |
| 5 | import { useUserSubscription } from "@/lib/hooks/queries/subscriptions"; |
| 6 | import { cn } from "@/lib/utils"; |
| 7 | import { ZapIcon, CheckCircleIcon } from "lucide-react"; |
| 8 | import Link from "next/link"; |
| 9 | |
| 10 | export default function SubscriptionStatus() { |
| 11 | const { data, isLoading } = useUserSubscription(); |
| 12 | |
| 13 | if (isLoading) { |
| 14 | return ( |
| 15 | <div className="px-4 py-3 bg-muted/50 rounded-md animate-pulse"> |
| 16 | <div className="h-4 w-3/4 bg-muted mb-2 rounded"></div> |
| 17 | <div className="h-3 w-1/2 bg-muted rounded"></div> |
| 18 | </div> |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | const isSubscribed = data?.isSubscribed; |
| 23 | const subscription = data?.subscription; |
| 24 | const plan = subscription?.plan; |
| 25 | |
| 26 | // Format usage data |
| 27 | const videosUsed = subscription?.videosUsed || 0; |
| 28 | const videoLimit = plan?.videoLimit || 3; // Default to free tier |
| 29 | const minutesUsed = subscription?.minutesUsed || 0; |
| 30 | const minutesLimit = plan?.minutesLimit || 30; // Default to free tier |
| 31 | |
| 32 | // Calculate percentages for progress bars |
| 33 | const videoPercentage = |
| 34 | videoLimit === -1 ? 0 : Math.min(100, (videosUsed / videoLimit) * 100); |
| 35 | const minutesPercentage = |
| 36 | minutesLimit === -1 ? 0 : Math.min(100, (minutesUsed / minutesLimit) * 100); |
| 37 | |
| 38 | // Check if limits are close to being reached |
| 39 | const isVideoLimitCritical = |
| 40 | videoLimit !== -1 && videosUsed >= videoLimit * 0.8; |
| 41 | const isMinutesLimitCritical = |
| 42 | minutesLimit !== -1 && minutesUsed >= minutesLimit * 0.8; |
| 43 | |
| 44 | if (!isSubscribed) { |
| 45 | return ( |
| 46 | <div className="px-4 py-3 bg-muted/30 rounded-md"> |
| 47 | <div className="flex items-center justify-between mb-2"> |
| 48 | <span className="text-sm font-medium">Free Tier</span> |
| 49 | <Link href="/subscriptions/manage"> |
| 50 | <Button variant="outline" size="sm" className="h-7 px-2"> |
| 51 | <ZapIcon className="size-3.5 mr-1" /> |
| 52 | Upgrade |
| 53 | </Button> |
| 54 | </Link> |
| 55 | </div> |
| 56 | |
| 57 | <div className="space-y-2"> |
| 58 | <div> |
| 59 | <div className="flex justify-between text-xs mb-1"> |
| 60 | <span |
| 61 | className={cn( |
| 62 | isVideoLimitCritical |
| 63 | ? "text-destructive font-medium" |
| 64 | : "text-muted-foreground" |
| 65 | )} |
| 66 | > |
| 67 | Videos: {videosUsed}/{videoLimit} |
| 68 | </span> |
| 69 | </div> |
| 70 | <Progress |
| 71 | value={videoPercentage} |
| 72 | className={cn( |
| 73 | "h-1.5", |
| 74 | isVideoLimitCritical ? "bg-destructive/20" : "bg-muted" |
| 75 | )} |
| 76 | indicatorClassName={ |
| 77 | isVideoLimitCritical ? "bg-destructive" : undefined |
| 78 | } |
| 79 | /> |
| 80 | </div> |
| 81 | |
| 82 | <div> |
| 83 | <div className="flex justify-between text-xs mb-1"> |
| 84 | <span |
| 85 | className={cn( |
| 86 | isMinutesLimitCritical |
| 87 | ? "text-destructive font-medium" |
| 88 | : "text-muted-foreground" |
| 89 | )} |
| 90 | > |
| 91 | Minutes: {minutesUsed}/{minutesLimit} |
| 92 | </span> |
| 93 | </div> |
| 94 | <Progress |
| 95 | value={minutesPercentage} |
| 96 | className={cn( |
| 97 | "h-1.5", |
| 98 | isMinutesLimitCritical ? "bg-destructive/20" : "bg-muted" |
| 99 | )} |
| 100 | indicatorClassName={ |
| 101 | isMinutesLimitCritical ? "bg-destructive" : undefined |
| 102 | } |
| 103 | /> |
| 104 | </div> |
| 105 | </div> |
| 106 | </div> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return ( |
| 111 | <div className="px-4 py-3 bg-muted/30 rounded-md"> |
| 112 | <div className="flex items-center justify-between mb-2"> |
| 113 | <div className="flex items-center"> |
| 114 | <CheckCircleIcon className="size-3.5 mr-1 text-primary" /> |
| 115 | <span className="text-sm font-medium">{plan?.name}</span> |
| 116 | </div> |
| 117 | <Link href="/subscriptions/manage"> |
| 118 | <Button variant="ghost" size="sm" className="h-6 px-2"> |
| 119 | Manage |
| 120 | </Button> |
| 121 | </Link> |
| 122 | </div> |
| 123 | |
| 124 | <div className="space-y-2"> |
| 125 | {videoLimit === -1 ? ( |
| 126 | <div className="flex items-center text-xs text-muted-foreground mb-1"> |
| 127 | <span className="flex items-center"> |
| 128 | <ZapIcon className="size-3 mr-1 text-primary" /> |
| 129 | Unlimited videos |
| 130 | </span> |
| 131 | </div> |
| 132 | ) : ( |
| 133 | <div> |
| 134 | <div className="flex justify-between text-xs mb-1"> |
| 135 | <span |
| 136 | className={cn( |
| 137 | isVideoLimitCritical |
| 138 | ? "text-destructive font-medium" |
| 139 | : "text-muted-foreground" |
| 140 | )} |
| 141 | > |
| 142 | Videos: {videosUsed}/{videoLimit} |
| 143 | </span> |
| 144 | </div> |
| 145 | <Progress |
| 146 | value={videoPercentage} |
| 147 | className={cn( |
| 148 | "h-1.5", |
| 149 | isVideoLimitCritical ? "bg-destructive/20" : "bg-muted" |
| 150 | )} |
| 151 | indicatorClassName={ |
| 152 | isVideoLimitCritical ? "bg-destructive" : undefined |
| 153 | } |
| 154 | /> |
| 155 | </div> |
| 156 | )} |
| 157 | |
| 158 | {minutesLimit === -1 ? ( |
| 159 | <div className="flex items-center text-xs text-muted-foreground"> |
| 160 | <span className="flex items-center"> |
| 161 | <ZapIcon className="size-3 mr-1 text-primary" /> |
| 162 | Unlimited minutes |
| 163 | </span> |
| 164 | </div> |
| 165 | ) : ( |
| 166 | <div> |
| 167 | <div className="flex justify-between text-xs mb-1"> |
| 168 | <span |
| 169 | className={cn( |
| 170 | isMinutesLimitCritical |
| 171 | ? "text-destructive font-medium" |
| 172 | : "text-muted-foreground" |
| 173 | )} |
| 174 | > |
| 175 | Minutes: {minutesUsed}/{minutesLimit} |
| 176 | </span> |
| 177 | </div> |
| 178 | <Progress |
| 179 | value={minutesPercentage} |
| 180 | className={cn( |
| 181 | "h-1.5", |
| 182 | isMinutesLimitCritical ? "bg-destructive/20" : "bg-muted" |
| 183 | )} |
| 184 | indicatorClassName={ |
| 185 | isMinutesLimitCritical ? "bg-destructive" : undefined |
| 186 | } |
| 187 | /> |
| 188 | </div> |
| 189 | )} |
| 190 | </div> |
| 191 | </div> |
| 192 | ); |
| 193 | } |