Foundation for device filtering

This commit is contained in:
Manoj Vivek 2019-09-02 19:49:20 +05:30
parent 0e98d36fc6
commit 67d23d405d

View file

@ -1,6 +1,12 @@
// @flow
import chromeEmulatedDevices from './chromeEmulatedDevices';
type OS = 'iOS' | 'Android' | 'Windows Phone' | 'PC';
type DeviceType = 'phone' | 'tablet' | 'notebook';
type Capability = 'mobile' | 'touch';
export type Device = {
id: number,
added: boolean,
@ -8,10 +14,44 @@ export type Device = {
height: number,
name: string,
useragent: string,
capabilities: Array<string>,
capabilities: Array<Capability>,
os: OS,
type: DeviceType,
};
let idGen = 1;
export const OS: {[key: string]: OS} = {
ios: 'iOS',
android: 'Android',
windowsPhone: 'Windows Phone',
pc: 'PC',
};
export const DEVICE_TYPE: {[key: string]: DeviceType} = {
phone: 'phone',
tablet: 'tablet',
desktop: 'notebook',
};
export const CAPABILITIES: {[key: string]: Capability} = {
mobile: 'mobile',
touch: 'touch',
};
function getOS(device) {
if (device.capabilities.indexOf('mobile') > -1) {
const useragent = device['user-agent'];
if (useragent.indexOf('like Mac OS X') > -1) {
return 'iOS';
}
if (useragent.indexOf('Lumia') > -1) {
return 'Windows Phone';
}
return 'Android';
}
return 'PC';
}
let idGen = 1;
export default chromeEmulatedDevices.extensions
.sort((a, b) => a.order - b.order)
.map(({order, device}) => {
@ -19,6 +59,7 @@ export default chromeEmulatedDevices.extensions
device.type === 'notebook'
? device.screen.horizontal
: device.screen.vertical;
return {
id: idGen++,
name: device.title,
@ -27,5 +68,7 @@ export default chromeEmulatedDevices.extensions
useragent: device['user-agent'],
capabilities: device.capabilities,
added: device['show-by-default'],
os: getOS(device),
type: device.type,
};
});