-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): add Platform API for OS detection
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type PlatformOSType = 'android' | 'ios' | 'web' | ||
|
||
export type PlataformSelectSpecifics<T> = { | ||
[platform in PlatformOSType | 'default']?: T | ||
} | ||
|
||
export interface PlatformSelectOptions { | ||
fallbackToWeb?: boolean | ||
} | ||
|
||
export type PlatformSelect<T> = ( | ||
specifics: PlataformSelectSpecifics<T>, | ||
options: PlatformSelectOptions, | ||
) => T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Credits: Bruno Lemos (https://github.com/devhubapp/devhub/blob/master/packages/components/src/libs/platform/index.ts) | ||
|
||
import { Platform as _Platform } from 'react-native' | ||
|
||
import { PlataformSelectSpecifics, PlatformSelectOptions } from './index.shared' | ||
|
||
export const Platform = { | ||
..._Platform, | ||
isElectron: false, | ||
isStandalone: true, | ||
realOS: _Platform.OS, | ||
selectUsingRealOS<T>( | ||
specifics: PlataformSelectSpecifics<T>, | ||
_options?: PlatformSelectOptions, | ||
) { | ||
return _Platform.select(specifics) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Platform as _Platform } from 'react-native' | ||
|
||
import { | ||
PlataformSelectSpecifics, | ||
PlatformOSType, | ||
PlatformSelectOptions, | ||
} from './index.shared' | ||
|
||
// From: https://github.com/cheton/is-electron | ||
function isElectron() { | ||
if ( | ||
!(typeof window !== 'undefined' && (window as any).bara === true && !!(window as any).ipc) | ||
) { | ||
return false | ||
} | ||
|
||
if ( | ||
typeof window !== 'undefined' && | ||
typeof (window as any).process === 'object' && | ||
(window as any).process.type === 'renderer' | ||
) { | ||
return true | ||
} | ||
|
||
if ( | ||
typeof process !== 'undefined' && | ||
typeof process.versions === 'object' && | ||
!!(process.versions as any).electron | ||
) { | ||
return true | ||
} | ||
|
||
if ( | ||
typeof navigator === 'object' && | ||
typeof navigator.userAgent === 'string' && | ||
navigator.userAgent.indexOf('Electron') >= 0 | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
function getOSName(): PlatformOSType { | ||
const userAgent = | ||
navigator.userAgent || navigator.vendor || (window as any).opera | ||
|
||
if (/android/i.test(userAgent)) return 'android' | ||
|
||
if (/iPad|iPhone|iPod/.test(userAgent) && !(window as any).MSStream) | ||
return 'ios' | ||
|
||
return 'web' | ||
} | ||
|
||
const realOS = getOSName() | ||
|
||
export const Platform = { | ||
realOS, | ||
..._Platform, | ||
isElectron: isElectron(), | ||
isStandalone: (window.navigator as any).standalone, | ||
selectUsingRealOS<T>( | ||
specifics: PlataformSelectSpecifics<T>, | ||
{ fallbackToWeb = false }: PlatformSelectOptions = {}, | ||
) { | ||
const result = | ||
Platform.realOS in specifics | ||
? specifics[realOS] | ||
: fallbackToWeb && 'web' in specifics | ||
? specifics.web | ||
: specifics.default | ||
|
||
return result | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './use-barn-state' | ||
export * from './barn-bridge' | ||
export * from './use-components-stream' | ||
|
||
export * from './Platform' |