import { useState, useCallback } from 'react' import { Check, Copy, Loader2 } from 'lucide-react' import { useTranslation } from 'react-i18next' import { copyToClipboard } from '@/lib/copy-to-clipboard' import { Button } from '@/components/ui/button' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip' import { StatusBadge } from '@/components/status-badge' import { type ApiKey } from '../types' import { useApiKeys } from './api-keys-provider' export function ApiKeyCell({ apiKey }: { apiKey: ApiKey }) { const { t } = useTranslation() const { resolveRealKey, resolvedKeys, loadingKeys, copiedKeyId, markKeyCopied, } = useApiKeys() const [popoverOpen, setPopoverOpen] = useState(false) const isLoading = !!loadingKeys[apiKey.id] const resolvedFullKey = resolvedKeys[apiKey.id] const isCopied = copiedKeyId === apiKey.id const maskedKey = `sk-${apiKey.key}` const handlePopoverOpen = useCallback( (open: boolean) => { setPopoverOpen(open) if (open && !resolvedFullKey) { resolveRealKey(apiKey.id) } }, [resolvedFullKey, resolveRealKey, apiKey.id] ) const handleCopy = useCallback(async () => { const realKey = resolvedFullKey || (await resolveRealKey(apiKey.id)) if (realKey) { const ok = await copyToClipboard(realKey) if (ok) markKeyCopied(apiKey.id) } }, [resolvedFullKey, resolveRealKey, apiKey.id, markKeyCopied]) return (

{t('Full API Key')}

{isLoading ? (
{t('Loading...')}
) : ( e.target.select()} className='bg-muted/50 w-full min-w-[280px] rounded-md border px-3 py-2 font-mono text-xs outline-none' /> )}
{isLoading ? t('Loading...') : isCopied ? t('Copied!') : t('Copy API key')}
) } export function ModelLimitsCell({ apiKey }: { apiKey: ApiKey }) { const { t } = useTranslation() if (!apiKey.model_limits_enabled || !apiKey.model_limits) { return ( ) } const models = apiKey.model_limits.split(',').filter(Boolean) return (
{models.map((m) => (
{m}
))}
) } export function IpRestrictionsCell({ apiKey }: { apiKey: ApiKey }) { const { t } = useTranslation() const allowIps = apiKey.allow_ips?.trim() if (!allowIps) { return ( ) } const ips = allowIps .split('\n') .map((ip) => ip.trim()) .filter(Boolean) return (
{ips.map((ip) => (
{ip}
))}
) }