WellNuo/web/lib/browserCheck.ts
Sergei cc626d6b67 Implement browser compatibility check for Web Bluetooth
Add comprehensive browser detection module that checks:
- Web Bluetooth API availability
- Browser name and version detection (Chrome, Edge, Opera, Safari, Firefox)
- Platform/OS detection (Windows, macOS, Linux, iOS, Android)
- Version requirement validation (Chrome 70+, Edge 79+, Opera 57+)
- User-friendly error messages for unsupported browsers

Features:
- isBrowserSupported(): Returns boolean for compatibility
- getBrowserInfo(): Detailed browser and platform information
- getRecommendedBrowsers(): List of supported browsers with download links
- getUnsupportedMessage(): Context-specific error messages

All functions include comprehensive unit tests with 100% coverage.

Related to PRD-WEB.md Phase 1 tasks.
2026-01-31 17:09:08 -08:00

198 lines
5.1 KiB
TypeScript

/**
* Browser Compatibility Check Module
* Detects browser support for Web Bluetooth API and provides compatibility information
*/
export interface BrowserInfo {
name: string;
version: string;
isSupported: boolean;
hasWebBluetooth: boolean;
platform: string;
}
export interface RecommendedBrowser {
name: string;
minVersion: string;
downloadUrl: string;
platforms: string[];
}
/**
* Detects the current browser name and version from user agent
*/
function detectBrowser(): { name: string; version: string } {
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
return { name: 'Unknown', version: '0' };
}
const userAgent = navigator.userAgent;
let name = 'Unknown';
let version = '0';
// Chrome (must check before Safari as it contains both, exclude Edge and Opera)
if (/Chrome\/(\d+)/.test(userAgent) && !/Edg/.test(userAgent) && !/OPR/.test(userAgent)) {
name = 'Chrome';
const match = userAgent.match(/Chrome\/(\d+)/);
version = match ? match[1] : '0';
}
// Edge (Chromium-based)
else if (/Edg\/(\d+)/.test(userAgent)) {
name = 'Edge';
const match = userAgent.match(/Edg\/(\d+)/);
version = match ? match[1] : '0';
}
// Opera
else if (/OPR\/(\d+)/.test(userAgent)) {
name = 'Opera';
const match = userAgent.match(/OPR\/(\d+)/);
version = match ? match[1] : '0';
}
// Safari (check after Chrome)
else if (/Safari\/(\d+)/.test(userAgent) && !/Chrome/.test(userAgent)) {
name = 'Safari';
const match = userAgent.match(/Version\/(\d+)/);
version = match ? match[1] : '0';
}
// Firefox
else if (/Firefox\/(\d+)/.test(userAgent)) {
name = 'Firefox';
const match = userAgent.match(/Firefox\/(\d+)/);
version = match ? match[1] : '0';
}
return { name, version };
}
/**
* Detects the platform/OS
*/
function detectPlatform(): string {
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
return 'Unknown';
}
const userAgent = navigator.userAgent;
const platform = navigator.platform;
if (/Win/.test(platform)) return 'Windows';
if (/Mac/.test(platform)) return 'macOS';
if (/Linux/.test(platform)) return 'Linux';
if (/iPhone|iPad|iPod/.test(userAgent)) return 'iOS';
if (/Android/.test(userAgent)) return 'Android';
return 'Unknown';
}
/**
* Checks if Web Bluetooth API is available
*/
function hasWebBluetoothAPI(): boolean {
if (typeof navigator === 'undefined') {
return false;
}
return 'bluetooth' in navigator;
}
/**
* Checks if the current browser is supported for WellNuo Web
* Supported browsers: Chrome 70+, Edge 79+, Opera 57+ (Windows 10+, macOS)
*/
export function isBrowserSupported(): boolean {
const { name, version } = detectBrowser();
const versionNum = parseInt(version, 10);
const platform = detectPlatform();
// iOS is not supported (system limitations)
if (platform === 'iOS') {
return false;
}
// Safari and Firefox don't support Web Bluetooth
if (name === 'Safari' || name === 'Firefox') {
return false;
}
// Check version requirements
switch (name) {
case 'Chrome':
return versionNum >= 70;
case 'Edge':
return versionNum >= 79;
case 'Opera':
return versionNum >= 57;
default:
return false;
}
}
/**
* Gets detailed information about the current browser
*/
export function getBrowserInfo(): BrowserInfo {
const { name, version } = detectBrowser();
const platform = detectPlatform();
const hasWebBluetooth = hasWebBluetoothAPI();
const isSupported = isBrowserSupported();
return {
name,
version,
isSupported,
hasWebBluetooth,
platform,
};
}
/**
* Returns list of recommended browsers with download links
*/
export function getRecommendedBrowsers(): RecommendedBrowser[] {
return [
{
name: 'Google Chrome',
minVersion: '70',
downloadUrl: 'https://www.google.com/chrome/',
platforms: ['Windows 10+', 'macOS', 'Linux'],
},
{
name: 'Microsoft Edge',
minVersion: '79',
downloadUrl: 'https://www.microsoft.com/edge',
platforms: ['Windows 10+', 'macOS'],
},
{
name: 'Opera',
minVersion: '57',
downloadUrl: 'https://www.opera.com/',
platforms: ['Windows', 'macOS', 'Linux'],
},
];
}
/**
* Gets a user-friendly message explaining why the browser is not supported
*/
export function getUnsupportedMessage(browserInfo: BrowserInfo): string {
const { name, platform } = browserInfo;
if (platform === 'iOS') {
return 'Web Bluetooth is not supported on iOS due to system limitations. Please use a desktop browser or download our mobile app.';
}
if (name === 'Safari') {
return 'Safari does not support Web Bluetooth API. Please use Chrome, Edge, or Opera instead.';
}
if (name === 'Firefox') {
return 'Firefox does not support Web Bluetooth API due to privacy concerns. Please use Chrome, Edge, or Opera instead.';
}
if (!browserInfo.hasWebBluetooth) {
return 'Your browser does not support Web Bluetooth API. Please update to a newer version or try Chrome, Edge, or Opera.';
}
return 'Your browser is not supported. Please use Chrome 70+, Edge 79+, or Opera 57+.';
}