Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'App.js' lib to TypeScript #29661

Merged
merged 14 commits into from
Nov 9, 2023
Merged
25 changes: 9 additions & 16 deletions src/libs/Browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
function getBrowser() {
return '';
}
import type {GetBrowser, IsMobile, IsMobileChrome, IsMobileSafari, IsSafari, OpenRouteInDesktopApp} from './types';

function isMobile() {
return false;
}
const getBrowser: GetBrowser = () => '';

function isMobileSafari() {
return false;
}
function isMobileChrome() {
return false;
}
const isMobile: IsMobile = () => false;

function isSafari() {
return false;
}
const isMobileSafari: IsMobileSafari = () => false;

function openRouteInDesktopApp() {}
const isMobileChrome: IsMobileChrome = () => false;

const isSafari: IsSafari = () => false;

const openRouteInDesktopApp: OpenRouteInDesktopApp = () => {};

export {getBrowser, isMobile, isMobileSafari, isSafari, isMobileChrome, openRouteInDesktopApp};
25 changes: 11 additions & 14 deletions src/libs/Browser/index.web.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {GetBrowser, IsMobile, IsMobileChrome, IsMobileSafari, IsSafari, OpenRouteInDesktopApp} from './types';

/**
* Fetch browser name from UA string
*
*/
function getBrowser(): string {
const getBrowser: GetBrowser = () => {
const {userAgent} = window.navigator;
const match = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))/i) ?? [];
let temp: RegExpMatchArray | null;
Expand All @@ -30,43 +31,39 @@ function getBrowser(): string {

browserName = match[1] ?? navigator.appName;
return browserName ? browserName.toLowerCase() : CONST.BROWSER.OTHER;
}
};

/**
* Whether the platform is a mobile browser.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
*
*/
function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Silk|Opera Mini/i.test(navigator.userAgent);
}
const isMobile: IsMobile = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Silk|Opera Mini/i.test(navigator.userAgent);

/**
* Checks if requesting user agent is Safari browser on a mobile device
*
*/
function isMobileSafari() {
const isMobileSafari: IsMobileSafari = () => {
const userAgent = navigator.userAgent;
return /iP(ad|od|hone)/i.test(userAgent) && /WebKit/i.test(userAgent) && !/(CriOS|FxiOS|OPiOS|mercury)/i.test(userAgent);
}
};

/**
* Checks if requesting user agent is Chrome browser on a mobile device
*
*/
function isMobileChrome() {
const isMobileChrome: IsMobileChrome = () => {
const userAgent = navigator.userAgent;
return /Android/i.test(userAgent) && /chrome|chromium|crios/i.test(userAgent);
}
};

function isSafari() {
return getBrowser() === 'safari' || isMobileSafari();
}
const isSafari: IsSafari = () => getBrowser() === 'safari' || isMobileSafari();

/**
* The session information needs to be passed to the Desktop app, and the only way to do that is by using query params. There is no other way to transfer the data.
*/
function openRouteInDesktopApp(shortLivedAuthToken = '', email = '') {
const openRouteInDesktopApp: OpenRouteInDesktopApp = (shortLivedAuthToken = '', email = '') => {
const params = new URLSearchParams();
// If the user is opening the desktop app through a third party signin flow, we need to manually add the exitTo param
// so that the desktop app redirects to the correct home route after signin is complete.
Expand Down Expand Up @@ -102,6 +99,6 @@ function openRouteInDesktopApp(shortLivedAuthToken = '', email = '') {
} else {
window.location.href = expensifyDeeplinkUrl;
}
}
};

export {getBrowser, isMobile, isMobileSafari, isSafari, isMobileChrome, openRouteInDesktopApp};
13 changes: 13 additions & 0 deletions src/libs/Browser/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type GetBrowser = () => string;

type IsMobile = () => boolean;

type IsMobileSafari = () => boolean;

type IsMobileChrome = () => boolean;

type IsSafari = () => boolean;
VickyStash marked this conversation as resolved.
Show resolved Hide resolved

type OpenRouteInDesktopApp = (shortLivedAuthToken?: string, email?: string) => void;

export type {GetBrowser, IsMobile, IsMobileSafari, IsMobileChrome, IsSafari, OpenRouteInDesktopApp};
Loading
Loading