feat: add Usage Limits Panel component with usage tracking and visual indicators for limits
style: implement custom color schemes and gradients for account section, enhancing visual hierarchy
This commit is contained in:
245
frontend/src/components/billing/UsageLimitsPanel.tsx
Normal file
245
frontend/src/components/billing/UsageLimitsPanel.tsx
Normal file
@@ -0,0 +1,245 @@
|
||||
/**
|
||||
* Usage Limits Panel Component
|
||||
* Displays hard and monthly plan limits with usage tracking
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Card } from '../ui/card';
|
||||
import { AlertCircle, TrendingUp, Users, Globe, Tag, FileText, Image, Zap } from 'lucide-react';
|
||||
import Badge from '../ui/badge/Badge';
|
||||
import { getUsageSummary, type UsageSummary, type LimitUsage } from '../../services/billing.api';
|
||||
import { useToast } from '../ui/toast/ToastContainer';
|
||||
|
||||
interface LimitCardProps {
|
||||
title: string;
|
||||
icon: React.ReactNode;
|
||||
usage: LimitUsage;
|
||||
type: 'hard' | 'monthly';
|
||||
daysUntilReset?: number;
|
||||
}
|
||||
|
||||
function LimitCard({ title, icon, usage, type, daysUntilReset }: LimitCardProps) {
|
||||
const percentage = usage.percentage_used;
|
||||
const isWarning = percentage >= 80;
|
||||
const isDanger = percentage >= 95;
|
||||
|
||||
let barColor = 'bg-blue-500';
|
||||
let badgeVariant: 'default' | 'warning' | 'danger' = 'default';
|
||||
|
||||
if (isDanger) {
|
||||
barColor = 'bg-red-500';
|
||||
badgeVariant = 'danger';
|
||||
} else if (isWarning) {
|
||||
barColor = 'bg-yellow-500';
|
||||
badgeVariant = 'warning';
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="p-4 hover:shadow-md transition-shadow">
|
||||
<div className="flex items-start justify-between mb-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="p-2 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-blue-600 dark:text-blue-400">
|
||||
{icon}
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="font-medium text-gray-900 dark:text-white">{title}</h3>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||
{type === 'monthly' && daysUntilReset !== undefined
|
||||
? `Resets in ${daysUntilReset} days`
|
||||
: 'Lifetime limit'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Badge variant={badgeVariant}>{percentage}%</Badge>
|
||||
</div>
|
||||
|
||||
{/* Progress Bar */}
|
||||
<div className="mb-3">
|
||||
<div className="h-2 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden">
|
||||
<div
|
||||
className={`h-full ${barColor} transition-all duration-300`}
|
||||
style={{ width: `${Math.min(percentage, 100)}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Usage Stats */}
|
||||
<div className="flex items-center justify-between text-sm">
|
||||
<span className="text-gray-600 dark:text-gray-400">
|
||||
{usage.current.toLocaleString()} / {usage.limit.toLocaleString()}
|
||||
</span>
|
||||
<span className="font-medium text-gray-900 dark:text-white">
|
||||
{usage.remaining.toLocaleString()} remaining
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Warning Message */}
|
||||
{isWarning && (
|
||||
<div className={`mt-3 flex items-start gap-2 text-xs ${
|
||||
isDanger ? 'text-red-600 dark:text-red-400' : 'text-yellow-600 dark:text-yellow-400'
|
||||
}`}>
|
||||
<AlertCircle className="w-4 h-4 mt-0.5 flex-shrink-0" />
|
||||
<span>
|
||||
{isDanger
|
||||
? 'Limit almost reached! Consider upgrading your plan.'
|
||||
: 'Approaching limit. Monitor your usage carefully.'}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export default function UsageLimitsPanel() {
|
||||
const toast = useToast();
|
||||
const [summary, setSummary] = useState<UsageSummary | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string>('');
|
||||
|
||||
useEffect(() => {
|
||||
loadUsageSummary();
|
||||
}, []);
|
||||
|
||||
const loadUsageSummary = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
const data = await getUsageSummary();
|
||||
setSummary(data);
|
||||
} catch (err: any) {
|
||||
const message = err?.message || 'Failed to load usage summary';
|
||||
setError(message);
|
||||
toast.error(message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="animate-pulse">
|
||||
<div className="h-32 bg-gray-200 dark:bg-gray-700 rounded-lg mb-4"></div>
|
||||
<div className="h-32 bg-gray-200 dark:bg-gray-700 rounded-lg mb-4"></div>
|
||||
<div className="h-32 bg-gray-200 dark:bg-gray-700 rounded-lg"></div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !summary) {
|
||||
return (
|
||||
<Card className="p-6 text-center">
|
||||
<AlertCircle className="w-12 h-12 text-red-500 mx-auto mb-4" />
|
||||
<h3 className="text-lg font-medium text-gray-900 dark:text-white mb-2">
|
||||
Failed to Load Usage Data
|
||||
</h3>
|
||||
<p className="text-gray-600 dark:text-gray-400 mb-4">{error || 'Unknown error'}</p>
|
||||
<button
|
||||
onClick={loadUsageSummary}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
|
||||
>
|
||||
Retry
|
||||
</button>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
const hardLimitIcons = {
|
||||
sites: <Globe className="w-5 h-5" />,
|
||||
users: <Users className="w-5 h-5" />,
|
||||
keywords: <Tag className="w-5 h-5" />,
|
||||
clusters: <TrendingUp className="w-5 h-5" />,
|
||||
};
|
||||
|
||||
const monthlyLimitIcons = {
|
||||
content_ideas: <FileText className="w-5 h-5" />,
|
||||
content_words: <FileText className="w-5 h-5" />,
|
||||
images_basic: <Image className="w-5 h-5" />,
|
||||
images_premium: <Zap className="w-5 h-5" />,
|
||||
image_prompts: <Image className="w-5 h-5" />,
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h2 className="text-xl font-bold text-gray-900 dark:text-white">Plan Limits & Usage</h2>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400 mt-1">
|
||||
Current Plan: <span className="font-medium">{summary.plan_name}</span>
|
||||
</p>
|
||||
</div>
|
||||
{summary.days_until_reset !== undefined && (
|
||||
<Badge variant="default">
|
||||
Resets in {summary.days_until_reset} days
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Hard Limits Section */}
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
|
||||
Account Limits
|
||||
</h3>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
{Object.entries(summary.hard_limits).map(([key, usage]) => (
|
||||
<LimitCard
|
||||
key={key}
|
||||
title={usage.display_name}
|
||||
icon={hardLimitIcons[key as keyof typeof hardLimitIcons]}
|
||||
usage={usage}
|
||||
type="hard"
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Monthly Limits Section */}
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
|
||||
Monthly Usage Limits
|
||||
</h3>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{Object.entries(summary.monthly_limits).map(([key, usage]) => (
|
||||
<LimitCard
|
||||
key={key}
|
||||
title={usage.display_name}
|
||||
icon={monthlyLimitIcons[key as keyof typeof monthlyLimitIcons]}
|
||||
usage={usage}
|
||||
type="monthly"
|
||||
daysUntilReset={summary.days_until_reset}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Upgrade CTA if approaching limits */}
|
||||
{(Object.values(summary.hard_limits).some(u => u.percentage_used >= 80) ||
|
||||
Object.values(summary.monthly_limits).some(u => u.percentage_used >= 80)) && (
|
||||
<Card className="p-6 bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-900/20 dark:to-indigo-900/20 border-blue-200 dark:border-blue-700">
|
||||
<div className="flex items-start gap-4">
|
||||
<div className="p-3 bg-blue-600 rounded-lg text-white">
|
||||
<TrendingUp className="w-6 h-6" />
|
||||
</div>
|
||||
<div className="flex-1">
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">
|
||||
Approaching Your Limits
|
||||
</h3>
|
||||
<p className="text-gray-600 dark:text-gray-400 mb-4">
|
||||
You're using a significant portion of your plan's resources. Upgrade to get higher limits
|
||||
and avoid interruptions.
|
||||
</p>
|
||||
<a
|
||||
href="/account/plans-and-billing"
|
||||
className="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
View Plans & Upgrade
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -18,6 +18,17 @@ export interface PricingPlan {
|
||||
features: string[];
|
||||
buttonText: string;
|
||||
highlighted?: boolean;
|
||||
disabled?: boolean;
|
||||
// Plan limits
|
||||
max_sites?: number;
|
||||
max_users?: number;
|
||||
max_keywords?: number;
|
||||
max_clusters?: number;
|
||||
max_content_ideas?: number;
|
||||
max_content_words?: number;
|
||||
max_images_basic?: number;
|
||||
max_images_premium?: number;
|
||||
included_credits?: number;
|
||||
}
|
||||
|
||||
interface PricingTableProps {
|
||||
@@ -124,12 +135,68 @@ export function PricingTable({ variant = '1', title, plans, showToggle = false,
|
||||
<span className="text-sm text-gray-700 dark:text-gray-300">{feature}</span>
|
||||
</li>
|
||||
))}
|
||||
|
||||
{/* Plan Limits Section */}
|
||||
{(plan.max_sites || plan.max_content_words || plan.included_credits) && (
|
||||
<div className="pt-3 mt-3 border-t border-gray-200 dark:border-gray-700">
|
||||
<div className="text-xs font-semibold text-gray-500 dark:text-gray-400 mb-2">LIMITS</div>
|
||||
{plan.max_sites && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{plan.max_sites === 99999 ? 'Unlimited' : plan.max_sites} Sites
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
{plan.max_users && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{plan.max_users === 99999 ? 'Unlimited' : plan.max_users} Team Members
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
{plan.max_content_words && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{(plan.max_content_words / 1000).toLocaleString()}K Words/month
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
{plan.max_content_ideas && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{plan.max_content_ideas} Ideas/month
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
{plan.max_images_basic && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{plan.max_images_basic} Images/month
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
{plan.included_credits && (
|
||||
<li className="flex items-start gap-2">
|
||||
<Check className="w-4 h-4 text-blue-500 flex-shrink-0 mt-0.5" />
|
||||
<span className="text-xs text-gray-600 dark:text-gray-400">
|
||||
{plan.included_credits.toLocaleString()} Credits/month
|
||||
</span>
|
||||
</li>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</ul>
|
||||
|
||||
<Button
|
||||
variant={plan.highlighted ? 'primary' : 'outline'}
|
||||
className="w-full"
|
||||
onClick={() => onPlanSelect?.(plan)}
|
||||
disabled={plan.disabled}
|
||||
>
|
||||
{plan.buttonText}
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user