Files
opsxdb/src/driverIconUrls.ts
T

32 lines
1.0 KiB
TypeScript

/** Vite-bundled driver icons — works in Tauri/web without depending on /public path. */
const modules = import.meta.glob("../assets/databases/*.{svg,png}", {
eager: true,
query: "?url",
import: "default",
}) as Record<string, string>;
function fileKey(path: string): string {
const base = path.split("/").pop() ?? path;
return base.replace(/\.(svg|png)$/i, "").toLowerCase();
}
/** id → bundled asset URL */
export const DRIVER_ICON_URLS: Record<string, string> = {};
for (const [path, url] of Object.entries(modules)) {
DRIVER_ICON_URLS[fileKey(path)] = url;
}
/** Resolve catalog icon path or id to a loadable URL. */
export function resolveDriverIconUrl(id: string, icon?: string): string | undefined {
const byId = DRIVER_ICON_URLS[id.toLowerCase()];
if (byId) return byId;
if (!icon) return undefined;
const m = icon.match(/\/([^/]+)\.(svg|png)$/i);
if (m) {
const byFile = DRIVER_ICON_URLS[m[1].toLowerCase()];
if (byFile) return byFile;
}
// last resort: absolute/public path (web dev)
return icon;
}