AeThex-OS/client/src/lib/platform.ts
2026-02-12 20:38:28 -07:00

125 lines
3.7 KiB
TypeScript

export type PlatformType = 'web' | 'desktop' | 'mobile';
declare global {
interface Window {
__TAURI__?: unknown;
flutter_inappwebview?: unknown;
Capacitor?: unknown;
}
}
interface PlatformConfig {
platform: PlatformType;
apiBaseUrl: string;
isSecureContext: boolean;
supportsNotifications: boolean;
supportsFileSystem: boolean;
}
let cachedPlatform: PlatformType | null = null;
export function detectPlatform(): PlatformType {
// Always re-check unless we've confirmed a native environment
if (cachedPlatform === 'mobile' || cachedPlatform === 'desktop') {
return cachedPlatform;
}
if (typeof window === 'undefined') {
console.log('[Platform] Detected: web (no window)');
return 'web';
}
// Check for specific native bridges
if (window.__TAURI__ !== undefined) {
console.log('[Platform] Detected: desktop (Tauri)');
cachedPlatform = 'desktop';
return cachedPlatform;
}
// Capacitor check - only detect mobile on actual native platforms
if (window.Capacitor !== undefined) {
try {
// Capacitor has getPlatform() that returns 'android', 'ios', or 'web'
const capacitorPlatform = (window.Capacitor as any).getPlatform?.();
if (capacitorPlatform === 'android' || capacitorPlatform === 'ios') {
console.log('[Platform] Detected: mobile (Capacitor on ' + capacitorPlatform + ')');
cachedPlatform = 'mobile';
return cachedPlatform;
}
console.log('[Platform] Capacitor running on web platform - ignoring');
} catch (e) {
console.log('[Platform] Capacitor check failed - treating as web');
}
}
// Flutter check
if (window.flutter_inappwebview !== undefined) {
console.log('[Platform] Detected: mobile (Flutter)');
cachedPlatform = 'mobile';
return cachedPlatform;
}
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.includes('electron')) {
console.log('[Platform] Detected: desktop (Electron)');
cachedPlatform = 'desktop';
return cachedPlatform;
}
if (userAgent.includes('cordova')) {
console.log('[Platform] Detected: mobile (Cordova)');
cachedPlatform = 'mobile';
return cachedPlatform;
}
// Only treat as mobile if we're actually in a native app context // Don't rely on user agent alone since users browsing from desktop should see desktop UI
// Uncomment the lines below ONLY if you need mobile web browser detection:
// if (userAgent.includes('android') || userAgent.includes('iphone') || userAgent.includes('ipad')) {
// console.log('[Platform] Detected: mobile (UA)', userAgent);
// return 'mobile';
// }
// Default to web, but do NOT cache it so we can re-check later when Capacitor might be ready
console.log('[Platform] Detected: web (default - desktop browser or no native bridge)');
return 'web';
}
function getApiBaseUrl(): string {
const platform = detectPlatform();
if (platform === 'web') {
return '';
}
const envUrl = import.meta.env.VITE_API_BASE_URL;
if (envUrl) return envUrl;
return 'https://aethex.network';
}
export function getPlatformConfig(): PlatformConfig {
const platform = detectPlatform();
return {
platform,
apiBaseUrl: getApiBaseUrl(),
isSecureContext: typeof window !== 'undefined' && window.isSecureContext,
supportsNotifications: typeof Notification !== 'undefined',
supportsFileSystem: typeof window !== 'undefined' && 'showOpenFilePicker' in window,
};
}
export function isDesktop(): boolean {
return detectPlatform() === 'desktop';
}
export function isMobile(): boolean {
return detectPlatform() === 'mobile';
}
export function isWeb(): boolean {
return detectPlatform() === 'web';
}
export const platformConfig = getPlatformConfig();