Skip to content

Commit

Permalink
fix: Stabilize opening connectors on the HomeView
Browse files Browse the repository at this point in the history
#74 introduced the ability to open a connector from the cozy-store
webview

This implementation had a side effect as the requested connector was
kept in the application state which would trigger the connector display
after each re-render

For example if the user opens a connector from the store, closes it and
the navigate to cozy-drive, then when navigating back to the Home, the
connector would be displayed again

To prevent this we now keep track of the webview's url in real time and
then use the navigation's `focus` and `blur` events to fix the rendered
url on the native context

We also clear the navigation `konnector` param when consumed. Which
would prevent an unexpected re-render of the connector after closing it

This new implementation does not need to inject a javascript chunk to
handle the navigation, so we deleted `interceptHashAndNavigate` method
  • Loading branch information
Ldoppea committed Feb 10, 2022
1 parent c55e42d commit 44c1b3d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
9 changes: 6 additions & 3 deletions src/components/webviews/CozyWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {useSession} from '../../hooks/useSession.js'

import {jsCozyGlobal} from './jsInteractions/jsCozyInjection'
import {jsLogInterception, tryConsole} from './jsInteractions/jsLogInterception'
import {interceptHashAndNavigate} from './jsInteractions/jsNavigation'

const log = Minilog('CozyWebView')

Expand All @@ -18,6 +17,7 @@ const CozyWebView = ({
onMessage: parentOnMessage,
logId = '',
source,
trackWebviewInnerUri,
...rest
}) => {
const [ref, setRef] = useState('')
Expand Down Expand Up @@ -60,8 +60,6 @@ const CozyWebView = ({
})();
`

interceptHashAndNavigate(source.uri, ref, log, logId)

return uri ? (
<WebView
{...rest}
Expand All @@ -85,6 +83,11 @@ const CozyWebView = ({
return true
}
}}
onLoad={({nativeEvent}) => {
if (trackWebviewInnerUri) {
trackWebviewInnerUri(nativeEvent.url)
}
}}
onMessage={async m => {
tryConsole(m, log, logId)

Expand Down
13 changes: 0 additions & 13 deletions src/components/webviews/jsInteractions/jsNavigation.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/libs/functions/routeHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {get} from 'lodash'

/**
* Retrieve the specified route parameter and remove it from the navigation state
* @param {string} paramName - Name of the parameter to retrieve
* @param {*} route - Application's route
* @param {*} navigation - Application's navigation
* @returns the route parameter's value
*/
export const consumeRouteParameter = (paramName, route, navigation) => {
const param = get(route, `params.${paramName}`)

if (param) {
navigation.setParams({[paramName]: undefined})
}

return param
}
45 changes: 39 additions & 6 deletions src/screens/home/components/HomeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,45 @@ import {useNativeIntent} from 'cozy-intent'

import {useSession} from '../../../hooks/useSession'
import CozyWebView from '../../../components/webviews/CozyWebView'
import {consumeRouteParameter} from '../../../libs/functions/routeHelpers'

const HomeView = ({route, navigation, setLauncherContext}) => {
const client = useClient()
const [uri, setUri] = useState('')
const [trackedWebviewInnerUri, setTrackedWebviewInnerUri] = useState('')
const nativeIntent = useNativeIntent()
const session = useSession()

React.useEffect(() => {
const unsubscribe = navigation.addListener('blur', () => {
setUri(trackedWebviewInnerUri)
})

return unsubscribe
}, [navigation, uri, trackedWebviewInnerUri])

React.useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
if (uri) {
const konnectorParam = consumeRouteParameter(
'konnector',
route,
navigation,
)

if (konnectorParam) {
const url = new URL(uri)
url.hash = `/connected/${konnectorParam}`

const targetUri = url.toString()
setUri(targetUri)
}
}
})

return unsubscribe
}, [navigation, route, uri])

useEffect(() => {
const {shouldCreateSession, handleCreateSession, consumeSessionToken} =
session
Expand Down Expand Up @@ -39,13 +71,14 @@ const HomeView = ({route, navigation, setLauncherContext}) => {
}
}, [uri, client, route, nativeIntent, navigation, session])

const konnectorParam = get(route, 'params.konnector')
const targetUri =
uri && konnectorParam ? `${uri}connected/${konnectorParam}` : uri

return targetUri ? (
return uri ? (
<CozyWebView
source={{uri: targetUri}}
source={{uri: uri}}
trackWebviewInnerUri={webviewInneruri => {
if (webviewInneruri !== trackedWebviewInnerUri) {
setTrackedWebviewInnerUri(webviewInneruri)
}
}}
navigation={navigation}
logId="HomeView"
onMessage={async m => {
Expand Down

0 comments on commit 44c1b3d

Please sign in to comment.