Files
echoes-of-the-ash/pwa/src/utils/assetPath.ts
2026-02-10 10:48:53 +01:00

41 lines
1.2 KiB
TypeScript

/**
* Asset Path Utility
*
* Resolves asset paths based on runtime environment:
* - Electron: Returns relative path (assets bundled with app, using ./)
* - Browser: Returns absolute path (served by PWA nginx)
*/
// Detect if running in Electron - check at runtime, not module load time
function checkIsElectron(): boolean {
return !!(window as any).electronAPI?.isElectron ||
window.location.protocol === 'file:'
}
/**
* Resolves an asset path for the current environment
* @param path - The asset path (e.g., "images/items/knife.webp" or "/images/items/knife.webp")
* @returns The resolved path for the current environment
*/
export function getAssetPath(path: string): string {
if (!path) return ''
// Normalize path (remove leading slash for Electron compatibility)
const cleanPath = path.startsWith('/') ? path.slice(1) : path
if (checkIsElectron()) {
// In Electron with base: './', use relative paths
return `./${cleanPath}`
}
// In browser, use absolute path - PWA nginx serves images at /images/
return `/${cleanPath}`
}
/**
* Check if we're running in Electron
*/
export function isElectronApp(): boolean {
return checkIsElectron()
}