import React, { useEffect, useState } from 'react'; import { Cpu, Loader2 } from 'lucide-react'; import { getAiModelStatus, type AiRuntimeStatus } from '../lib/api'; import { cn } from '../lib/utils'; import { useStore } from '../store/useStore'; interface ModelStatusBadgeProps { compact?: boolean; } export function ModelStatusBadge({ compact = false }: ModelStatusBadgeProps) { const aiModel = useStore((state) => state.aiModel); const [status, setStatus] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { let cancelled = false; setIsLoading(true); getAiModelStatus(aiModel) .then((data) => { if (!cancelled) setStatus(data); }) .catch(() => { if (!cancelled) setStatus(null); }) .finally(() => { if (!cancelled) setIsLoading(false); }); return () => { cancelled = true; }; }, [aiModel]); const model = status?.models.find((item) => item.id === aiModel); const ready = Boolean(model?.available); const gpuReady = Boolean(status?.gpu.available); const label = compact ? (gpuReady ? 'GPU' : 'CPU') : `${model?.label || aiModel.toUpperCase()} ${ready ? '可用' : '不可用'}`; return (
{isLoading ? : } {label}
); }