From 6627444036045259d2a3a71488faaa1839be9e96 Mon Sep 17 00:00:00 2001 From: tzhangchi Date: Sun, 18 Dec 2022 13:08:36 +0800 Subject: [PATCH] feat: use useragent method to alternative of isMac Method --- .../app/src/components/quick-search/index.tsx | 3 +- packages/app/src/utils/useragent.ts | 136 ++++++++++-------- 2 files changed, 78 insertions(+), 61 deletions(-) diff --git a/packages/app/src/components/quick-search/index.tsx b/packages/app/src/components/quick-search/index.tsx index 1ba2c212b8..2d9f011d6e 100644 --- a/packages/app/src/components/quick-search/index.tsx +++ b/packages/app/src/components/quick-search/index.tsx @@ -12,12 +12,13 @@ import { Footer } from './footer'; import { Command } from 'cmdk'; import { useEffect, useState } from 'react'; import { useModal } from '@/providers/global-modal-provider'; +import { getUaHelper } from '@/utils'; type TransitionsModalProps = { open: boolean; onClose: () => void; }; const isMac = () => { - return /macintosh|mac os x/i.test(navigator.userAgent); + return getUaHelper().isMacOs; }; export const QuickSearch = ({ open, onClose }: TransitionsModalProps) => { const [query, setQuery] = useState(''); diff --git a/packages/app/src/utils/useragent.ts b/packages/app/src/utils/useragent.ts index 1ed1304980..20b8593ef1 100644 --- a/packages/app/src/utils/useragent.ts +++ b/packages/app/src/utils/useragent.ts @@ -1,66 +1,82 @@ -const getUa = () => { - const ua = navigator.userAgent; - const uas = ua.toLowerCase(); - const mobile = - /mobile|tablet|ip(ad|hone|od)|android/i.test(ua) || - (uas.indexOf('safari') > -1 && /Mac OS/i.test(ua) && /Macintosh/i.test(ua)); - const android = - (mobile && (uas.indexOf('android') > -1 || uas.indexOf('linux') > -1)) || - uas.indexOf('adr') > -1; - const ios = mobile && !android && /Mac OS/i.test(ua); - const iphone = ios && uas.indexOf('iphone') > -1; - const ipad = ios && !iphone; - const wx = /MicroMessenger/i.test(ua); - const chrome = /CriOS/i.test(ua) || /Chrome/i.test(ua); - const tiktok = mobile && /aweme/i.test(ua); - const weibo = mobile && /Weibo/i.test(ua); - const safari = - ios && !chrome && !wx && !weibo && !tiktok && /Safari|Macintosh/i.test(ua); - const firefox = /Firefox/.test(ua); - const win = /windows|win32|win64|wow32|wow64/.test(uas); - const linux = /linux/.test(uas); - return { - ua, - mobile, - android, - ios, - wx, - chrome, - iphone, - ipad, - safari, - tiktok, - weibo, - win, - linux, - firefox, - }; -}; +function getUaHelper() { + let uaHelper = null; + (function (navigator) { + const getUa = () => { + const ua = navigator.userAgent; + const uas = ua.toLowerCase(); + const mobile = + /mobile|tablet|ip(ad|hone|od)|android/i.test(ua) || + (uas.indexOf('safari') > -1 && + /Mac OS/i.test(ua) && + /Macintosh/i.test(ua)); + const android = + (mobile && + (uas.indexOf('android') > -1 || uas.indexOf('linux') > -1)) || + uas.indexOf('adr') > -1; + const ios = mobile && !android && /Mac OS/i.test(ua); + const iphone = ios && uas.indexOf('iphone') > -1; + const ipad = ios && !iphone; + const wx = /MicroMessenger/i.test(ua); + const chrome = /CriOS/i.test(ua) || /Chrome/i.test(ua); + const tiktok = mobile && /aweme/i.test(ua); + const weibo = mobile && /Weibo/i.test(ua); + const safari = + ios && + !chrome && + !wx && + !weibo && + !tiktok && + /Safari|Macintosh/i.test(ua); + const firefox = /Firefox/.test(ua); + const win = /windows|win32|win64|wow32|wow64/.test(uas); + const linux = /linux/.test(uas); + return { + ua, + mobile, + android, + ios, + wx, + chrome, + iphone, + ipad, + safari, + tiktok, + weibo, + win, + linux, + firefox, + }; + }; -class UaHelper { - private uaMap; - public isLinux = false; - public isMacOs = false; - public isSafari = false; - public isWindows = false; - public isFireFox = false; + class UaHelper { + private uaMap; + public isLinux = false; + public isMacOs = false; + public isSafari = false; + public isWindows = false; + public isFireFox = false; - constructor() { - this.uaMap = getUa(); - this.initUaFlags(); - } + constructor() { + this.uaMap = getUa(); + this.initUaFlags(); + } - public checkUseragent(isUseragent: keyof ReturnType) { - return Boolean(this.uaMap[isUseragent]); - } + public checkUseragent(isUseragent: keyof ReturnType) { + return Boolean(this.uaMap[isUseragent]); + } - private initUaFlags() { - this.isLinux = this.checkUseragent('linux'); - this.isMacOs = this.checkUseragent('ios'); - this.isSafari = this.checkUseragent('safari'); - this.isWindows = this.checkUseragent('win'); - this.isFireFox = this.checkUseragent('firefox'); - } + private initUaFlags() { + this.isLinux = this.checkUseragent('linux'); + this.isMacOs = this.checkUseragent('ios'); + this.isSafari = this.checkUseragent('safari'); + this.isWindows = this.checkUseragent('win'); + this.isFireFox = this.checkUseragent('firefox'); + } + } + uaHelper = new UaHelper(); + })(navigator); + + return uaHelper; } -export const uaHelper = new UaHelper(); +export { getUaHelper };