mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-17 14:49:39 +02:00
- userAgent utils: parseUserAgent, getDeviceType, formatUserAgent (17 tests) - guestWelcome utils: shouldShow, markSeen, reset (8 tests) - jwtUtils: decodeToken, isTokenValid, getUserFromToken, B2B (27 tests) - mana-apps: hasAppAccess, getTierLevel, getAccessibleManaApps (16 tests) Also fixes iOS detection bug in userAgent parser (iPhone UA contains "Mac OS X" — mobile check must come before desktop OS check). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
/**
|
|
* Parse a user agent string to extract browser and OS information.
|
|
*/
|
|
export function parseUserAgent(ua: string | null): { browser: string; os: string } {
|
|
if (!ua) return { browser: '', os: '' };
|
|
|
|
let browser = '';
|
|
let os = '';
|
|
|
|
if (ua.includes('Firefox/')) browser = 'Firefox';
|
|
else if (ua.includes('Edg/')) browser = 'Edge';
|
|
else if (ua.includes('Chrome/') && !ua.includes('Edg/')) browser = 'Chrome';
|
|
else if (ua.includes('Safari/') && !ua.includes('Chrome/')) browser = 'Safari';
|
|
else if (ua.includes('Opera/') || ua.includes('OPR/')) browser = 'Opera';
|
|
|
|
if (ua.includes('iPhone') || ua.includes('iPad')) os = 'iOS';
|
|
else if (ua.includes('Android')) os = 'Android';
|
|
else if (ua.includes('Windows')) os = 'Windows';
|
|
else if (ua.includes('Mac OS X') || ua.includes('Macintosh')) os = 'macOS';
|
|
else if (ua.includes('Linux')) os = 'Linux';
|
|
|
|
return { browser, os };
|
|
}
|
|
|
|
/**
|
|
* Determine the device type from a user agent string.
|
|
*/
|
|
export function getDeviceType(ua: string | null): 'mobile' | 'desktop' | 'tablet' {
|
|
if (!ua) return 'desktop';
|
|
if (ua.includes('iPhone') || (ua.includes('Android') && !ua.includes('Tablet'))) return 'mobile';
|
|
if (ua.includes('iPad') || ua.includes('Tablet')) return 'tablet';
|
|
return 'desktop';
|
|
}
|
|
|
|
/**
|
|
* Format a user agent string as a display label (e.g. "Chrome · macOS").
|
|
*/
|
|
export function formatUserAgent(ua: string | null): string {
|
|
const { browser, os } = parseUserAgent(ua);
|
|
const parts = [browser, os].filter(Boolean);
|
|
return parts.length > 0 ? parts.join(' \u00b7 ') : '';
|
|
}
|