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

fix: Stabilize opening connectors on the HomeView #82

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}) => {
Ldoppea marked this conversation as resolved.
Show resolved Hide resolved
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) => {
acezard marked this conversation as resolved.
Show resolved Hide resolved
const param = get(route, `params.${paramName}`)

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

return param
}
64 changes: 64 additions & 0 deletions src/libs/functions/routeHelpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {consumeRouteParameter} from './routeHelpers'

describe('consumeRouteParameter', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('should return parameter and clear it from navigation when parameter exist', async () => {
const route = {
params: {
foo: 'bar',
},
}

const navigation = {
setParams: jest.fn(),
}

const param = consumeRouteParameter('foo', route, navigation)

expect(param).toBe('bar')
expect(navigation.setParams).toHaveBeenCalled()
expect(navigation.setParams.mock.calls[0][0]).toStrictEqual({
foo: undefined,
})
})

it('should return undefined and should not try to clear it from navigation when parameter does not exist', async () => {
const route = {
params: {
foo: 'bar',
},
}

const navigation = {
setParams: jest.fn(),
}

const param = consumeRouteParameter('unexistingParam', route, navigation)

expect(param).toBe(undefined)
expect(navigation.setParams).not.toHaveBeenCalled()
})

it('should return parameter and clear it from navigation when parameter exist but has falsy value', async () => {
const route = {
params: {
foo: 0,
},
}

const navigation = {
setParams: jest.fn(),
}

const param = consumeRouteParameter('foo', route, navigation)

expect(param).toBe(0)
expect(navigation.setParams).toHaveBeenCalled()
expect(navigation.setParams.mock.calls[0][0]).toStrictEqual({
foo: undefined,
})
})
})
45 changes: 40 additions & 5 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('')
acezard marked this conversation as resolved.
Show resolved Hide resolved
const nativeIntent = useNativeIntent()
const session = useSession()

React.useEffect(() => {
acezard marked this conversation as resolved.
Show resolved Hide resolved
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,16 @@ 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
const handleTrackWebviewInnerUri = webviewInneruri => {
if (webviewInneruri !== trackedWebviewInnerUri) {
setTrackedWebviewInnerUri(webviewInneruri)
}
}

return targetUri ? (
return uri ? (
<CozyWebView
source={{uri: targetUri}}
source={{uri: uri}}
trackWebviewInnerUri={handleTrackWebviewInnerUri}
navigation={navigation}
logId="HomeView"
onMessage={async m => {
Expand Down