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

feat: Add logs to ease debugging #844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 48 additions & 21 deletions src/components/webviews/CozyProxyWebView.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Minilog from '@cozy/minilog'
import React, { useState, useEffect } from 'react'
import { Platform, View } from 'react-native'

Expand All @@ -10,6 +11,8 @@ import { updateCozyAppBundleInBackground } from '/libs/cozyAppBundle/cozyAppBund
import { useHttpServerContext } from '/libs/httpserver/httpServerProvider'
import { IndexInjectionWebviewComponent } from '/components/webviews/webViewComponents/IndexInjectionWebviewComponent'

const log = Minilog('CozyProxyWebView')

const NO_INJECTED_HTML = 'NO_INJECTED_HTML'

const getHttpUnsecureUrl = uri => {
Expand Down Expand Up @@ -64,6 +67,31 @@ const getPlaformSpecificConfig = (uri, html) => {
}
}

const initHtmlContent = async ({
httpServerContext,
slug,
href,
client,
dispatch
}) => {
log.debug('Call to initHtmlContent()')
const htmlContent = await httpServerContext.getIndexHtmlForSlug(slug, client)

const { source: sourceActual, nativeConfig: nativeConfigActual } =
getPlaformSpecificConfig(href, htmlContent || NO_INJECTED_HTML)

dispatch({
html: htmlContent,
nativeConfig: nativeConfigActual,
source: sourceActual
})

updateCozyAppBundleInBackground({
slug,
client
})
}

export const CozyProxyWebView = ({ slug, href, style, ...props }) => {
const client = useClient()
const httpServerContext = useHttpServerContext()
Expand All @@ -73,31 +101,29 @@ export const CozyProxyWebView = ({ slug, href, style, ...props }) => {
nativeConfig: undefined
})

const reload = () => {
dispatch({
source: undefined
})
initHtmlContent({
httpServerContext,
slug,
href,
client,
dispatch
})
}

useEffect(() => {
if (httpServerContext.isRunning()) {
const initHtmlContent = async () => {
const htmlContent = await httpServerContext.getIndexHtmlForSlug(
slug,
client
)

const { source: sourceActual, nativeConfig: nativeConfigActual } =
getPlaformSpecificConfig(href, htmlContent || NO_INJECTED_HTML)

dispatch({
html: htmlContent,
nativeConfig: nativeConfigActual,
source: sourceActual
})

updateCozyAppBundleInBackground({
if (slug) {
initHtmlContent({
httpServerContext,
slug,
client
href,
client,
dispatch
})
}

if (slug) {
initHtmlContent()
} else {
dispatch({
html: undefined,
Expand All @@ -115,6 +141,7 @@ export const CozyProxyWebView = ({ slug, href, style, ...props }) => {
source={state.source}
nativeConfig={state.nativeConfig}
injectedIndex={state.html}
reloadProxyWebView={reload}
{...props}
/>
) : null}
Expand Down
8 changes: 7 additions & 1 deletion src/components/webviews/ReloadInterceptorWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ const ReloadInterceptorWebView = React.forwardRef((props, ref) => {
navigation,
onShouldStartLoadWithRequest,
interceptReload: true,
onReloadInterception: () => setTimestamp(Date.now()),
onReloadInterception: () => {
if (props.reloadProxyWebView) {
props.reloadProxyWebView()
} else {
setTimestamp(Date.now())
}
},
isFirstCall,
client,
setDownloadProgress
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useAppBootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '/libs/defaultRedirection/defaultRedirection'
import { useHomeStateContext } from '/screens/home/HomeStateProvider'

const log = Minilog('useAppBootstrap')
const log = Minilog('📦 useAppBootstrap')

export const useAppBootstrap = client => {
const [initialRoute, setInitialRoute] = useState('fetching')
Expand Down
11 changes: 10 additions & 1 deletion src/libs/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ export {
} from '/libs/clientHelpers/initClient'
export { call2FAInitClient } from '/libs/clientHelpers/twoFactorAuthentication'

const log = Minilog('LoginScreen')
const log = Minilog('⛅ Client.js')

/**
* Get a cozy client instance, initialized with authentication information from mobile storage
*
* @returns {CozyClient}
*/
export const getClient = async () => {
log.debug('Call to getClient()')
const val = await AsyncStorage.getItem(strings.OAUTH_STORAGE_KEY)
if (!val) {
return false
Expand Down Expand Up @@ -79,6 +80,7 @@ export const getClient = async () => {
* @returns {CozyPublicData}
*/
export const fetchPublicData = async client => {
log.debug('Call to fetchPublicData()')
const stackClient = client.getStackClient()

const { KdfIterations: kdfIterations, name } = await stackClient.fetchJSON(
Expand All @@ -103,6 +105,7 @@ export const fetchPublicData = async client => {
*/

export const fetchCozyDataForSlug = async (slug, client, cookie) => {
log.debug(`Call to fetchCozyDataForSlug() with slug ${slug}`)
const stackClient = client.getStackClient()

const options = cookie
Expand Down Expand Up @@ -165,6 +168,7 @@ export const getInstanceAndFqdnFromClient = client => {
* @returns {Promise<string>} - The version of the cozy-app
*/
export const fetchCozyAppVersion = async (slug, client, type = 'apps') => {
log.debug(`Call to fetchCozyAppVersion() with slug ${slug} and type ${type}`)
const stackClient = client.getStackClient()

const result = await stackClient.fetchJSON('GET', `/${type}/${slug}`)
Expand Down Expand Up @@ -192,6 +196,9 @@ export const fetchCozyAppArchiveInfoForVersion = async (
version,
client
) => {
log.debug(
`Call to fetchCozyAppArchiveInfoForVersion() with slug ${slug} and version ${version}`
)
const stackClient = client.getStackClient()

try {
Expand Down Expand Up @@ -219,6 +226,7 @@ export const fetchCozyAppArchiveInfoForVersion = async (
}

export const saveNotificationDeviceToken = async (client, token) => {
log.debug('Call to saveNotificationDeviceToken()')
const oauthOptions = client.getStackClient().oauthOptions

if (oauthOptions?.notification_device_token === token) return
Expand All @@ -232,6 +240,7 @@ export const saveNotificationDeviceToken = async (client, token) => {
}

export const removeNotificationDeviceToken = async client => {
log.debug('Call to removeNotificationDeviceToken()')
const oauthOptions = client.getStackClient().oauthOptions

if (oauthOptions?.notification_device_token === '') return
Expand Down
7 changes: 7 additions & 0 deletions src/libs/httpserver/httpCookieManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Minilog from '@cozy/minilog'
import AsyncStorage from '@react-native-async-storage/async-storage'
import CookieManager, { Cookie, Cookies } from '@react-native-cookies/cookies'

Expand All @@ -7,6 +8,8 @@ import { isSecureProtocol } from '../functions/isSecureProtocol'

import strings from '/constants/strings.json'

const log = Minilog('🍪 HttpCookieManager')

type CookieRecord = Record<string, string>

const isCookieName = (key: string): boolean => {
Expand Down Expand Up @@ -89,6 +92,7 @@ export const setCookie = async (
cookieString: string,
client: CozyClient
): Promise<boolean> => {
log.debug('Call to setCookie()')
const cookie = parseCookie(cookieString)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
Expand Down Expand Up @@ -118,6 +122,7 @@ export const setCookie = async (
export const getCookie = async (
client: CozyClient
): Promise<Cookie | undefined> => {
log.debug('Call to getCookie()')
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const appUrl: string = client.getStackClient().uri

Expand All @@ -133,6 +138,7 @@ export const getCookie = async (
* @returns {Promise<boolean>}
*/
export const resyncCookies = async (client: CozyClient): Promise<void> => {
log.debug('Call to resyncCookies()')
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const appUrl: string = client.getStackClient().uri

Expand All @@ -147,6 +153,7 @@ export const resyncCookies = async (client: CozyClient): Promise<void> => {
}

export const clearCookies = async (): Promise<void> => {
log.debug('Call to clearCookies()')
await AsyncStorage.removeItem(strings.COOKIE_STORAGE_KEY)

await CookieManager.clearAll()
Expand Down
1 change: 1 addition & 0 deletions src/libs/httpserver/httpServerProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const HttpServerProvider = props => {
const isRunning = () => serverInstance?.isRunning()

const getIndexHtmlForSlug = async (slug, client) => {
log.debug(`Call to getIndexHtmlForSlug() with slug ${slug}`)
try {
const rootURL = client.getStackClient().uri

Expand Down
5 changes: 5 additions & 0 deletions src/libs/httpserver/indexDataFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Minilog from '@cozy/minilog'

import CozyClient from 'cozy-client'

import { AppData, AppAttributes } from '/libs/httpserver/models'
Expand All @@ -6,6 +8,8 @@ import { getCookie } from '/libs/httpserver/httpCookieManager'
import { logToSentry } from '/libs/monitoring/Sentry'
import { replaceAll } from '/libs/functions/stringHelpers'

const log = Minilog('👉 IndexDataFetcher')

interface FetchAppDataForSlugResult {
cookie: AppAttributes['Cookie']
templateValues: Omit<AppAttributes, 'Cookie'> & { CozyData: string }
Expand All @@ -17,6 +21,7 @@ export const fetchAppDataForSlug = async (
slug: string,
client: CozyClient
): Promise<FetchAppDataForSlugResult | undefined> => {
log.debug(`Call to fetchAppDataForSlug() with slug ${slug}`)
try {
const storedCookie = await getCookie(client)
const cozyDataResult = await fetchCozyDataForSlug<{ data: AppData }>(
Expand Down
11 changes: 11 additions & 0 deletions src/screens/cozy-app/CozyAppScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Minilog from '@cozy/minilog'
import React, { useCallback, useEffect, useMemo, useState } from 'react'
import { StatusBar, View } from 'react-native'

Expand All @@ -11,6 +12,8 @@ import { firstHalfUI, handleError } from './CozyAppScreen.functions'
import { styles } from './CozyAppScreen.styles'
import { CozyAppScreenProps } from './CozyAppScreen.types'

const log = Minilog('📐 CozyAppScreen')

export const CozyAppScreen = ({
route,
navigation
Expand All @@ -28,6 +31,14 @@ export const CozyAppScreen = ({
const [shouldExitAnimation, setShouldExitAnimation] = useState(false)
const { setShouldWaitCozyApp } = useHomeStateContext()

useEffect(() => {
log.debug('CozyAppScreen mount')

return () => {
log.debug('CozyAppScreen unmount')
}
}, [])

useEffect(() => {
flagshipUI.on('change', (state: NormalisedFlagshipUI) => {
setUIState({ ...UIState, ...state })
Expand Down
11 changes: 11 additions & 0 deletions src/screens/home/components/HomeView.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Minilog from '@cozy/minilog'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { get } from 'lodash'
import { useFocusEffect } from '@react-navigation/native'
Expand All @@ -23,6 +24,8 @@ import { launcherEvent } from '/libs/ReactNativeLauncher'
import { determineSecurityFlow } from '/app/domain/authorization/services/SecurityService'
import { devlog } from '/core/tools/env'

const log = Minilog('🏠 HomeView')

const unzoomHomeView = webviewRef => {
webviewRef?.injectJavaScript(
'window.dispatchEvent(new Event("closeApp"));true;'
Expand Down Expand Up @@ -61,6 +64,14 @@ const HomeView = ({ route, navigation, setLauncherContext, setBarStyle }) => {
)
const hasRenderedOnce = useRef(false)

useEffect(() => {
log.debug('HomeView mount')

return () => {
log.debug('HomeView unmount')
}
}, [])

useEffect(() => {
const subscription = AppState.addEventListener(
'change',
Expand Down