* chore: remove dead/unreferenced files across ui, vscode Remove 59 unused source files (components, hooks, lib utils, stores, barrels, and orphaned vscode github modules) that are not imported by any entry-reachable code. Also drop a stale test mock for the removed execCommands module. Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com> * refactor: remove unused exported symbols (types, functions, consts, hooks) Remove exported symbols whose identifier is referenced nowhere in the repository (verified via repo-wide search), across ui types/contracts, lib utilities, sync layer, stores, and components. Also drop the few imports/private helpers orphaned by these removals. Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com> * refactor: remove more unused exports (desktop, shortcuts, worktree, vscode) Continue removing repo-wide unreferenced exported functions, consts and types across lib/desktop, shortcuts, worktreeSessionCreator, sync, and vscode gitService, with cascading orphaned helpers/imports cleaned up. Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com> * chore: add dead-code cleanup tooling * refactor: checkpoint dead-code cleanup * refactor: remove dead-code suppressions --------- Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com> Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
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>();
|
|
const PRELOADED_LOGO_SRCS = new Set<string>();
|
|
|
|
const LOGO_ALIAS = new Map<string, string>([
|
|
['codex', 'openai'],
|
|
['chatgpt', 'openai'],
|
|
['claude', 'anthropic'],
|
|
['gemini', 'google'],
|
|
['evroc-ai', 'evroc'],
|
|
['evrocai', 'evroc'],
|
|
['ollama-cloud', 'ollama'],
|
|
['wafer-ai', 'wafer.ai'],
|
|
['wafer', 'wafer.ai'],
|
|
]);
|
|
|
|
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;
|
|
const candidates = [LOGO_ALIAS.get(compact), LOGO_ALIAS.get(primary), compact, primary]
|
|
.filter((value): value is string => Boolean(value && value.length > 0));
|
|
|
|
return [...new Set(candidates)];
|
|
};
|
|
|
|
const resolveProviderLogoSrc = (providerId: string | null | undefined): string | null => {
|
|
const candidates = buildLogoCandidates(providerId);
|
|
const localResolvedId = candidates.find((candidate) => LOCAL_PROVIDER_LOGO_MAP.has(candidate)) ?? null;
|
|
const localLogoSrc = localResolvedId ? LOCAL_PROVIDER_LOGO_MAP.get(localResolvedId) ?? null : null;
|
|
if (localLogoSrc) {
|
|
return localLogoSrc;
|
|
}
|
|
|
|
const remoteResolvedId = candidates[0] ?? null;
|
|
return remoteResolvedId ? `https://models.dev/logos/${remoteResolvedId}.svg` : null;
|
|
};
|
|
|
|
const preloadProviderLogo = (providerId: string | null | undefined): void => {
|
|
if (typeof Image === 'undefined') return;
|
|
const src = resolveProviderLogoSrc(providerId);
|
|
if (!src || PRELOADED_LOGO_SRCS.has(src)) return;
|
|
|
|
PRELOADED_LOGO_SRCS.add(src);
|
|
const image = new Image();
|
|
image.decoding = 'async';
|
|
image.onerror = () => {
|
|
PRELOADED_LOGO_SRCS.delete(src);
|
|
};
|
|
image.src = src;
|
|
void image.decode?.().catch(() => undefined);
|
|
};
|
|
|
|
export const preloadProviderLogos = (providerIds: readonly (string | null | undefined)[]): void => {
|
|
for (const providerId of providerIds) {
|
|
preloadProviderLogo(providerId);
|
|
}
|
|
};
|
|
|
|
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 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;
|
|
|
|
const [source, setSource] = useState<LogoSource>(hasLocalLogo ? 'local' : 'remote');
|
|
|
|
useEffect(() => {
|
|
setSource(hasLocalLogo ? 'local' : 'remote');
|
|
}, [hasLocalLogo, localResolvedId, remoteResolvedId]);
|
|
|
|
const handleError = useCallback(() => {
|
|
setSource((current) => (current === 'local' && hasLocalLogo ? 'remote' : 'none'));
|
|
}, [hasLocalLogo]);
|
|
|
|
if (!localResolvedId && !remoteResolvedId) {
|
|
return { src: null, onError: handleError, hasLogo: false };
|
|
}
|
|
|
|
if (source === 'local' && localLogoSrc) {
|
|
return {
|
|
src: localLogoSrc,
|
|
onError: handleError,
|
|
hasLogo: true,
|
|
};
|
|
}
|
|
|
|
if (source === 'remote' && remoteResolvedId) {
|
|
return {
|
|
src: `https://models.dev/logos/${remoteResolvedId}.svg`,
|
|
onError: handleError,
|
|
hasLogo: true,
|
|
};
|
|
}
|
|
|
|
return { src: null, onError: handleError, hasLogo: false };
|
|
}
|