Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ux): deeplink broken with share contact
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
- Loading branch information
Showing
7 changed files
with
46 additions
and
88 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
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,83 +1,53 @@ | ||
import { Linking } from 'react-native' | ||
import React from 'react' | ||
import hoistNonReactStatics from 'hoist-non-react-statics' | ||
|
||
import { BASE_WEBSITE_URL } from '../constants' | ||
import { parse as parseUrl } from './url' | ||
|
||
export const withDeepLinkHandler = Component => { | ||
class DeepLinkHandler extends Component { | ||
initialState = { | ||
routeName: 'switch/main', | ||
params: {}, | ||
} | ||
|
||
state = this.initialState | ||
|
||
handleOpenURL = event => { | ||
let url = parseUrl( | ||
event.url.replace('berty://berty.chat/', `${BASE_WEBSITE_URL}/`) | ||
) | ||
switch (url.pathname) { | ||
case '/chats/detail': | ||
if (url.hashParts['id']) { | ||
// TODO: remove that part when we'll have get rid of graphql/relay | ||
url.hashParts['id'] = btoa(`conversation:${url.hashParts['id']}`) | ||
import { withHOC } from './views' | ||
import { withNavigation } from 'react-navigation' | ||
|
||
export const withDeepLinkHandler = Component => | ||
withHOC( | ||
withNavigation( | ||
class WithDeepLinkHandler extends React.PureComponent { | ||
handleOpenURL = event => { | ||
let url = parseUrl( | ||
event.url.replace('berty://berty.chat/', `${BASE_WEBSITE_URL}/`) | ||
) | ||
switch (url.pathname) { | ||
case '/chats/detail': | ||
this.props.navigation.navigate('chats/detail', url.hashParts) | ||
break | ||
case '/id': | ||
this.props.navigation.navigate('modal/contacts/card', { | ||
id: url.hashParts.key, | ||
displayName: url.hashParts.name, | ||
}) | ||
break | ||
default: | ||
console.warn(`Unhandled deep link, URL: ${event.url}`) | ||
break | ||
} | ||
this.setState({ | ||
routeName: 'chats/detail', | ||
params: url.hashParts, | ||
}) | ||
break | ||
case '/id': | ||
this.setState({ | ||
routeName: 'modal/contacts/card', | ||
params: url.hashParts, | ||
}) | ||
break | ||
default: | ||
console.warn(`Unhandled deep link, URL: ${event.url}`) | ||
break | ||
} | ||
} | ||
|
||
clearDeepLink = () => { | ||
this.setState(this.initialState) | ||
} | ||
} | ||
|
||
setDeepLink = deepLink => this.setState(deepLink) | ||
componentDidMount() { | ||
Linking.addEventListener('url', this.handleOpenURL) | ||
|
||
componentDidMount() { | ||
Linking.addEventListener('url', this.handleOpenURL) | ||
Linking.getInitialURL() | ||
.then(url => url != null && this.handleOpenURL({ url })) | ||
.catch(console.error) | ||
} | ||
|
||
Linking.getInitialURL() | ||
.then(url => { | ||
if (url !== null) { | ||
this.handleOpenURL({ url }) | ||
} | ||
}) | ||
.catch(() => {}) | ||
} | ||
|
||
componentWillUnmount() { | ||
Linking.removeEventListener('url', this.handleOpenURL) | ||
} | ||
componentWillUnmount() { | ||
Linking.removeEventListener('url', this.handleOpenURL) | ||
} | ||
|
||
render() { | ||
return ( | ||
<Component | ||
{...this.props} | ||
deepLinkHandler={{ | ||
deepLink: this.state, | ||
setDeepLink: deepLink => this.setDeepLink(deepLink), | ||
clearDeepLink: () => this.clearDeepLink(), | ||
}} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
return hoistNonReactStatics(DeepLinkHandler, Component) | ||
} | ||
render() { | ||
return <Component {...this.props} /> | ||
} | ||
} | ||
) | ||
)(Component) | ||
|
||
export default withDeepLinkHandler |
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
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
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
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
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