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'],
|
|
|
|
|
['claude', 'anthropic'],
|
|
|
|
|
]);
|
|
|
|
|
|
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 {
|
|
|
|
|
const normalizedId = providerId?.toLowerCase() ?? null;
|
2026-02-04 01:13:23 -08:00
|
|
|
const resolvedId = normalizedId ? LOGO_ALIAS.get(normalizedId) ?? normalizedId : null;
|
|
|
|
|
const hasLocalLogo = resolvedId ? LOCAL_PROVIDER_LOGO_MAP.has(resolvedId) : false;
|
|
|
|
|
const localLogoSrc = resolvedId ? LOCAL_PROVIDER_LOGO_MAP.get(resolvedId) ?? 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-04 01:13:23 -08:00
|
|
|
}, [hasLocalLogo, resolvedId]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const handleError = useCallback(() => {
|
|
|
|
|
setSource((current) => (current === 'local' && hasLocalLogo ? 'remote' : 'none'));
|
|
|
|
|
}, [hasLocalLogo]);
|
|
|
|
|
|
2026-02-04 01:13:23 -08:00
|
|
|
if (!resolvedId) {
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (source === 'remote') {
|
|
|
|
|
return {
|
2026-02-04 01:13:23 -08:00
|
|
|
src: `https://models.dev/logos/${resolvedId}.svg`,
|
2025-12-07 19:32:53 +02:00
|
|
|
onError: handleError,
|
|
|
|
|
hasLogo: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { src: null, onError: handleError, hasLogo: false };
|
|
|
|
|
}
|