mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Badge } from '@/components/ui/badge';
|
|
import { AIProvider } from '@/contexts/AISettingsContext';
|
|
import { useAICredits } from '@/hooks/useAICredits';
|
|
|
|
interface CreditsBadgeProps {
|
|
provider: AIProvider;
|
|
className?: string;
|
|
onOpenDialog?: () => void;
|
|
}
|
|
|
|
/**
|
|
* Displays a clickable credits badge showing the remaining credits for an AI provider
|
|
*/
|
|
export function CreditsBadge({ provider, className, onOpenDialog }: CreditsBadgeProps) {
|
|
const { data: credits, isLoading, error } = useAICredits(provider);
|
|
|
|
// Don't render anything if there's an error (provider doesn't support credits endpoint)
|
|
if (error || isLoading) {
|
|
return null;
|
|
}
|
|
|
|
// Don't render if no credits data is available
|
|
if (!credits) {
|
|
return null;
|
|
}
|
|
|
|
// Format the amount as currency
|
|
const formattedAmount = new Intl.NumberFormat('en-US', {
|
|
style: 'currency',
|
|
currency: 'USD',
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2,
|
|
}).format(credits.amount);
|
|
|
|
return (
|
|
<Badge
|
|
variant="secondary"
|
|
className={`cursor-pointer hover:bg-secondary/80 transition-colors ${className}`}
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onOpenDialog?.();
|
|
}}
|
|
>
|
|
{formattedAmount}
|
|
</Badge>
|
|
);
|
|
} |