2025-12-07 19:32:53 +02:00
|
|
|
import { useState, useCallback, useEffect } from 'react';
|
|
|
|
|
|
|
|
|
|
type LogoSource = 'local' | 'remote' | 'none';
|
|
|
|
|
|
|
|
|
|
interface UseProviderLogoReturn {
|
|
|
|
|
src: string | null;
|
|
|
|
|
onError: () => void;
|
|
|
|
|
hasLogo: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const localLogoModules = import.meta.glob<string>('../assets/provider-logos/*.svg', {
|
|
|
|
|
eager: true,
|
|
|
|
|
import: 'default',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const LOCAL_PROVIDER_LOGO_MAP = new Map<string, string>();
|
|
|
|
|
|
2026-02-04 01:13:23 -08:00
|
|
|
const LOGO_ALIAS = new Map<string, string>([
|
|
|
|
|
['codex', 'openai'],
|
2026-02-24 03:28:30 +02:00
|
|
|
['chatgpt', 'openai'],
|
2026-02-04 01:13:23 -08:00
|
|
|
['claude', 'anthropic'],
|
2026-02-24 03:28:30 +02:00
|
|
|
['gemini', 'google'],
|
|
|
|
|
['evroc-ai', 'evroc'],
|
|
|
|
|
['evrocai', 'evroc'],
|
2026-02-27 15:38:28 -08:00
|
|
|
['ollama-cloud', 'ollama'],
|
2026-02-04 01:13:23 -08:00
|
|
|
]);
|
|
|
|
|
|
2026-02-24 03:28:30 +02:00
|
|
|
const normalizeProviderId = (providerId: string | null | undefined) => {
|
|
|
|
|
return (providerId ?? '')
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.trim()
|
|
|
|
|
.replace(/^models\./, '')
|
|
|
|
|
.replace(/^provider\./, '')
|
|
|
|
|
.replace(/\s+/g, '-');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const buildLogoCandidates = (providerId: string | null | undefined) => {
|
|
|
|
|
const normalized = normalizeProviderId(providerId);
|
|
|
|
|
if (!normalized) {
|
|
|
|
|
return [] as string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const compact = normalized.replace(/[^a-z0-9_\-./:]/g, '');
|
|
|
|
|
const primary = compact.split(/[/:]/)[0] || compact;
|
2026-03-20 01:01:03 +02:00
|
|
|
const candidates = [LOGO_ALIAS.get(compact), LOGO_ALIAS.get(primary), compact, primary]
|
2026-02-24 03:28:30 +02:00
|
|
|
.filter((value): value is string => Boolean(value && value.length > 0));
|
|
|
|
|
|
|
|
|
|
return [...new Set(candidates)];
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
for (const [path, url] of Object.entries(localLogoModules)) {
|
|
|
|
|
const match = path.match(/provider-logos\/([^/]+)\.svg$/i);
|
|
|
|
|
if (match?.[1] && url) {
|
|
|
|
|
LOCAL_PROVIDER_LOGO_MAP.set(match[1].toLowerCase(), url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useProviderLogo(providerId: string | null | undefined): UseProviderLogoReturn {
|
2026-02-24 03:28:30 +02:00
|
|
|
const candidates = buildLogoCandidates(providerId);
|
|
|
|
|
const localResolvedId = candidates.find((candidate) => LOCAL_PROVIDER_LOGO_MAP.has(candidate)) ?? null;
|
|
|
|
|
const remoteResolvedId = candidates[0] ?? null;
|
|
|
|
|
const hasLocalLogo = Boolean(localResolvedId);
|
|
|
|
|
const localLogoSrc = localResolvedId ? LOCAL_PROVIDER_LOGO_MAP.get(localResolvedId) ?? null : null;
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const [source, setSource] = useState<LogoSource>(hasLocalLogo ? 'local' : 'remote');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSource(hasLocalLogo ? 'local' : 'remote');
|
2026-02-24 03:28:30 +02:00
|
|
|
}, [hasLocalLogo, localResolvedId, remoteResolvedId]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const handleError = useCallback(() => {
|
|
|
|
|
setSource((current) => (current === 'local' && hasLocalLogo ? 'remote' : 'none'));
|
|
|
|
|
}, [hasLocalLogo]);
|
|
|
|
|
|
2026-02-24 03:28:30 +02:00
|
|
|
if (!localResolvedId && !remoteResolvedId) {
|
2025-12-07 19:32:53 +02:00
|
|
|
return { src: null, onError: handleError, hasLogo: false };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (source === 'local' && localLogoSrc) {
|
|
|
|
|
return {
|
|
|
|
|
src: localLogoSrc,
|
|
|
|
|
onError: handleError,
|
|
|
|
|
hasLogo: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 03:28:30 +02:00
|
|
|
if (source === 'remote' && remoteResolvedId) {
|
2025-12-07 19:32:53 +02:00
|
|
|
return {
|
2026-02-24 03:28:30 +02:00
|
|
|
src: `https://models.dev/logos/${remoteResolvedId}.svg`,
|
2025-12-07 19:32:53 +02:00
|
|
|
onError: handleError,
|
|
|
|
|
hasLogo: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { src: null, onError: handleError, hasLogo: false };
|
|
|
|
|
}
|