mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-01 06:12:31 -08:00
30 lines
880 B
TypeScript
30 lines
880 B
TypeScript
import { UAParser } from 'ua-parser-js';
|
|
|
|
const result = new UAParser(window.navigator.userAgent).getResult();
|
|
|
|
const isMobileOrTablet = (() => {
|
|
const { os, device } = result;
|
|
if (device.type === 'mobile' || device.type === 'tablet') return true;
|
|
if (os.name === 'Android' || os.name === 'iOS') return true;
|
|
return false;
|
|
})();
|
|
|
|
const normalizeMacName = (os?: string) => {
|
|
if (!os) return os;
|
|
if (os === 'Mac OS') return 'macOS';
|
|
return os;
|
|
};
|
|
|
|
const isMac = result.os.name === 'Mac OS' || result.os.name === 'macOS';
|
|
|
|
export const ua = () => result;
|
|
export const isMacOS = () => isMac;
|
|
export const mobileOrTablet = () => isMobileOrTablet;
|
|
|
|
export const deviceDisplayName = (): string => {
|
|
const browser = result.browser.name;
|
|
const os = normalizeMacName(result.os.name);
|
|
if (!browser || !os) return 'Sable Web';
|
|
return `Sable on ${browser} for ${os}`;
|
|
};
|