Skip to content

Commit

Permalink
feat: Remove cordova stuff from AppLinker
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Dec 13, 2024
1 parent 3ddba0d commit 5eab7d8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 153 deletions.
13 changes: 0 additions & 13 deletions react/AppLinker/expiringMemoize.js

This file was deleted.

131 changes: 5 additions & 126 deletions react/AppLinker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -114,108 +78,38 @@ 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)
}

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
})
}
}

AppLinker.defaultProps = {
nativePath: '/'
}
AppLinker.propTypes = {
/** DEPRECATED: please use app.slug prop */
slug: PropTypes.string,
Expand All @@ -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 }
14 changes: 0 additions & 14 deletions react/AppLinker/native.config.js
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 5eab7d8

Please sign in to comment.