AeThex-OS/client/src/lib/platform.ts
sirpiglr 89c576aac7 Prepare codebase for future cross-platform deployment
Implement platform abstraction layer for API requests and storage, and document multi-platform strategy.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 279f1558-c0e3-40e4-8217-be7e9f4c6eca
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Event-Id: 4ad7a49d-0f69-4e30-a6ae-edccda64bd96
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/b984cb14-1d19-4944-922b-bc79e821ed35/279f1558-c0e3-40e4-8217-be7e9f4c6eca/jIK7HfC
Replit-Helium-Checkpoint-Created: true
2025-12-17 04:59:41 +00:00

92 lines
2.1 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;
function detectPlatform(): PlatformType {
if (cachedPlatform !== null) return cachedPlatform;
if (typeof window === 'undefined') {
cachedPlatform = 'web';
return cachedPlatform;
}
if (window.__TAURI__ !== undefined) {
cachedPlatform = 'desktop';
return cachedPlatform;
}
if (window.flutter_inappwebview !== undefined || window.Capacitor !== undefined) {
cachedPlatform = 'mobile';
return cachedPlatform;
}
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.includes('electron')) {
cachedPlatform = 'desktop';
return cachedPlatform;
}
if (userAgent.includes('cordova')) {
cachedPlatform = 'mobile';
return cachedPlatform;
}
cachedPlatform = 'web';
return cachedPlatform;
}
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();