Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.0 KB
Raw Blame
import {t} from "@lingui/macro";

export interface DeviceDescription {
    label: string;
    isMobile: boolean;
}

const browsers: [RegExp, string][] = [
    [/Edg\//, 'Edge'],
    [/OPR\/|Opera/, 'Opera'],
    [/Firefox\//, 'Firefox'],
    [/Chrome\//, 'Chrome'],
    [/Safari\//, 'Safari'],
];

const operatingSystems: [RegExp, string][] = [
    [/iPhone|iPad|iPod/, 'iOS'],
    [/Android/, 'Android'],
    [/Mac OS X|Macintosh/, 'macOS'],
    [/Windows/, 'Windows'],
    [/CrOS/, 'ChromeOS'],
    [/Linux/, 'Linux'],
];

export const describeDevice = (userAgent: string | null): DeviceDescription => {
    if (!userAgent) {
        return {label: t`Unknown device`, isMobile: false};
    }

    const browser = browsers.find(([pattern]) => pattern.test(userAgent))?.[1];
    const os = operatingSystems.find(([pattern]) => pattern.test(userAgent))?.[1];
    const label = browser && os
        ? t`${browser} on ${os}`
        : browser ?? os ?? t`Unknown device`;

    return {
        label,
        isMobile: /Mobi|iPhone|iPad|Android/.test(userAgent),
    };
};