-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathPlatform.ts
More file actions
115 lines (100 loc) · 2.91 KB
/
Platform.ts
File metadata and controls
115 lines (100 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export const Platform = (() => {
const isBrowser =
typeof window !== "undefined" && typeof navigator !== "undefined";
const normalizePlatform = (platform: string): string => {
const normalized = platform.toLowerCase();
if (normalized.includes("windows")) return "Windows";
if (
normalized.includes("iphone") ||
normalized.includes("ipad") ||
normalized.includes("ipod") ||
normalized.includes("ios")
) {
return "iOS";
}
if (
normalized.includes("mac") ||
normalized.includes("macintosh") ||
normalized.includes("macos")
) {
return "macOS";
}
if (normalized.includes("android")) return "Android";
if (normalized.includes("chrome os")) return "Linux";
if (normalized.includes("linux")) return "Linux";
return "Unknown";
};
// OS Extraction
const extractOS = (): string => {
if (!isBrowser) return "Unknown";
const uaData = (navigator as any).userAgentData;
if (uaData?.platform) {
return normalizePlatform(uaData.platform);
}
const ua = navigator.userAgent;
if (/windows nt/i.test(ua)) return "Windows";
if (/iphone|ipad|ipod/i.test(ua)) return "iOS";
if (
/mac os x/i.test(ua) &&
((navigator.maxTouchPoints ?? 0) > 1 || /ipad/i.test(ua))
) {
return "iOS";
}
if (/mac os x/i.test(ua)) return "macOS";
if (/android/i.test(ua)) return "Android";
if (/linux/i.test(ua)) return "Linux";
return "Unknown";
};
const isFirefox = /Firefox/i.test(navigator.userAgent);
const currentOS = extractOS();
// Environment Extraction
const performElectronCheck = (): boolean => {
// Renderer process
if (
typeof window !== "undefined" &&
typeof (window as any).process === "object" &&
(window as any).process.type === "renderer"
) {
return true;
}
// Main process
if (
typeof process !== "undefined" &&
typeof process.versions === "object" &&
!!process.versions.electron
) {
return true;
}
// Detect the user agent when the `nodeIntegration` option is set to false
if (
isBrowser &&
typeof navigator.userAgent === "string" &&
navigator.userAgent.indexOf("Electron") >= 0
) {
return true;
}
return false;
};
const isMac = currentOS === "macOS";
return {
os: currentOS,
isMac,
isWindows: currentOS === "Windows",
isIOS: currentOS === "iOS",
isAndroid: currentOS === "Android",
isLinux: currentOS === "Linux",
isElectron: performElectronCheck(),
isFirefox,
get isMobileWidth(): boolean {
return isBrowser ? window.innerWidth < 768 : false;
},
get isTabletWidth(): boolean {
return isBrowser
? window.innerWidth >= 768 && window.innerWidth < 1024
: false;
},
get isDesktopWidth(): boolean {
return isBrowser ? window.innerWidth >= 1024 : false;
},
};
})();