diff --git a/react/AppLinker/expiringMemoize.js b/react/AppLinker/expiringMemoize.js deleted file mode 100644 index 91cf50b9e2..0000000000 --- a/react/AppLinker/expiringMemoize.js +++ /dev/null @@ -1,13 +0,0 @@ -export default function (fn, duration, keyFn) { - const memo = {} - return arg => { - const key = keyFn(arg) - const memoInfo = memo[key] - const uptodate = - memoInfo && memoInfo.result && memoInfo.date - Date.now() < duration - if (!uptodate) { - memo[key] = { result: fn(arg), date: Date.now() } - } - return memo[key].result - } -} diff --git a/react/AppLinker/index.jsx b/react/AppLinker/index.jsx index d63aaf9f67..63f8057394 100644 --- a/react/AppLinker/index.jsx +++ b/react/AppLinker/index.jsx @@ -2,38 +2,20 @@ import PropTypes from 'prop-types' import React from 'react' import { withClient } from 'cozy-client' -import { - checkApp, - startApp, - isMobileApp, - isMobile, - openDeeplinkOrRedirect, - isAndroid, - isFlagshipApp -} from 'cozy-device-helper' +import { isFlagshipApp } from 'cozy-device-helper' import { WebviewContext } from 'cozy-intent' import logger from 'cozy-logger' -import expiringMemoize from './expiringMemoize' import { generateUniversalLink, generateWebLink, getUniversalLinkDomain } from './native' -import { NATIVE_APP_INFOS } from './native.config' - -const expirationDelay = 10 * 1000 -const memoizedCheckApp = expiringMemoize( - appInfo => checkApp(appInfo).catch(() => false), - expirationDelay, - appInfo => appInfo.appId -) export class AppLinker extends React.Component { static contextType = WebviewContext state = { - nativeAppIsAvailable: null, isFetchingAppInfo: false } @@ -48,21 +30,6 @@ export class AppLinker extends React.Component { this.setState({ imgRef: this.imgRef }) } - componentDidMount() { - if (isMobileApp()) { - this.checkAppAvailability() - } - } - - async checkAppAvailability() { - const slug = AppLinker.getSlug(this.props) - const appInfo = NATIVE_APP_INFOS[slug] - if (appInfo) { - const nativeAppIsAvailable = Boolean(await memoizedCheckApp(appInfo)) - this.setState({ nativeAppIsAvailable }) - } - } - static getSlug(props) { if (props.app && props.app.slug) { return props.app.slug @@ -79,13 +46,10 @@ export class AppLinker extends React.Component { } } - static getOnClickHref(props, nativeAppIsAvailable, context, imgRef) { - const { app, client, nativePath } = props - const slug = AppLinker.getSlug(props) + static getOnClickHref(props, context, imgRef) { + const { app, client } = props let href = props.href let onClick = null - const usingNativeApp = isMobileApp() - const appInfo = NATIVE_APP_INFOS[slug] if (isFlagshipApp()) { const { app: currentApp } = client @@ -114,79 +78,17 @@ export class AppLinker extends React.Component { } } - if (usingNativeApp) { - if (nativeAppIsAvailable) { - // If we are on the native app and the other native app is available, - // we open the native app - onClick = AppLinker.openNativeFromNative.bind(this, props) - href = '#' - } else { - // If we are on a native app, but the other native app is not available - // we open the web link, this is done by the href prop. We still - // have to call the prop callback - onClick = AppLinker.openWeb.bind(this, props) - } - } else if (isMobile() && appInfo) { - // If we are on the "mobile web version", we try to open the native app - // if it exists with an universal links. If it fails, we redirect to the web - // version of the requested app - // Only on iOS ATM - if (isAndroid()) { - onClick = AppLinker.openNativeFromWeb.bind(this, props) - } else { - // Since generateUniversalLink can rise an error, let's catch it to not crash - // all the page. - try { - href = generateUniversalLink({ slug, nativePath, fallbackUrl: href }) - } catch (err) { - console.error(err) - href = '#' - } - } - } - return { href, onClick } } - static openNativeFromWeb(props, ev) { - const { href, nativePath, onAppSwitch } = props - const slug = AppLinker.getSlug(props) - const appInfo = NATIVE_APP_INFOS[slug] - - if (ev) { - ev.preventDefault() - } - - AppLinker.onAppSwitch(onAppSwitch) - openDeeplinkOrRedirect( - appInfo.uri + (nativePath === '/' ? '' : nativePath), - function () { - window.location.href = href - } - ) - } - static onAppSwitch(onAppSwitchFn) { if (typeof onAppSwitchFn === 'function') { onAppSwitchFn() } } - static openNativeFromNative(props, ev) { - const { onAppSwitch } = props - const slug = AppLinker.getSlug(props) - if (ev) { - ev.preventDefault() - } - const appInfo = NATIVE_APP_INFOS[slug] - AppLinker.onAppSwitch(onAppSwitch) - startApp(appInfo).catch(err => { - console.error('AppLinker: Could not open native app', err) - }) - } - static openWeb(props) { AppLinker.onAppSwitch(props.onAppSwitch) } @@ -194,18 +96,13 @@ export class AppLinker extends React.Component { render() { const { children } = this.props AppLinker.deprecateSlug(this.props) - const slug = AppLinker.getSlug(this.props) - const { nativeAppIsAvailable } = this.state - const appInfo = NATIVE_APP_INFOS[slug] const { href, onClick } = AppLinker.getOnClickHref( this.props, - nativeAppIsAvailable, this.context, this.state.imgRef ) return children({ - ...appInfo, iconRef: this.setImgRef, onClick: onClick, href @@ -213,9 +110,6 @@ export class AppLinker extends React.Component { } } -AppLinker.defaultProps = { - nativePath: '/' -} AppLinker.propTypes = { /** DEPRECATED: please use app.slug prop */ slug: PropTypes.string, @@ -224,27 +118,12 @@ AppLinker.propTypes = { Used as a fallback_uri on mobile web */ href: PropTypes.string, - /* - Path used for "native link" - */ - nativePath: PropTypes.string, onAppSwitch: PropTypes.func, app: PropTypes.shape({ // Slug of the app : drive / banks ... - slug: PropTypes.string.isRequired, - // Information about mobile native app - mobile: PropTypes.shape({ - schema: PropTypes.string, - id_playstore: PropTypes.string, - id_appstore: PropTypes.string - }) + slug: PropTypes.string.isRequired }).isRequired } export default withClient(AppLinker) -export { - NATIVE_APP_INFOS, - getUniversalLinkDomain, - generateWebLink, - generateUniversalLink -} +export { getUniversalLinkDomain, generateWebLink, generateUniversalLink } diff --git a/react/AppLinker/native.config.js b/react/AppLinker/native.config.js index c5ba033a8a..720efa6b15 100644 --- a/react/AppLinker/native.config.js +++ b/react/AppLinker/native.config.js @@ -1,15 +1 @@ -import { isAndroidApp } from 'cozy-device-helper' - -export const NATIVE_APP_INFOS = { - drive: { - appId: 'io.cozy.drive.mobile', - uri: 'cozydrive://', - name: 'Cozy Drive' - }, - banks: { - appId: isAndroidApp() ? 'io.cozy.banks.mobile' : 'io.cozy.banks', - uri: 'cozybanks://', - name: 'Cozy Banks' - } -} export const UNIVERSAL_LINK_URL = 'https://links.mycozy.cloud'