11/* global __SIMULATE_FLAGSHIP__ */
2- import React , { useEffect , useState } from 'react'
2+ import React , { useState } from 'react'
33import { Navigate , Route } from 'react-router-dom'
44
5- import flag , { enable as enableFlags } from 'cozy-flags'
5+ import flag from 'cozy-flags'
66import minilog from 'cozy-minilog'
77import { useQuery } from 'cozy-client'
88import { useWebviewIntent } from 'cozy-intent'
@@ -25,12 +25,10 @@ import IntentRedirect from 'components/IntentRedirect'
2525import MoveModal from 'components/MoveModal'
2626import StoreRedirection from 'components/StoreRedirection'
2727import BackupNotification from 'components/BackupNotification/BackupNotification'
28- import appEntryPoint from 'components/appEntryPoint'
2928import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'
3029import { BackgroundContainer } from 'components/BackgroundContainer'
3130import { FLAG_FAB_BUTTON_ENABLED } from 'components/AddButton/helpers'
3231import { MainView } from 'components/MainView'
33- import { toFlagNames } from './toFlagNames'
3432import { Konnector } from 'components/Konnector'
3533import DefaultRedirectionSnackbar from 'components/DefaultRedirectionSnackbar/DefaultRedirectionSnackbar'
3634import ReloadFocus from './ReloadFocus'
@@ -39,27 +37,22 @@ import { formatShortcuts } from 'components/Shortcuts/utils'
3937import {
4038 mkHomeMagicFolderConn ,
4139 mkHomeCustomShorcutsConn ,
42- mkHomeCustomShorcutsDirConn ,
43- contextQuery
40+ mkHomeCustomShorcutsDirConn
4441} from 'queries'
42+ import { useFetchInitialData } from 'hooks/useFetchInitialData'
4543import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
4644import SectionDialog from 'components/Sections/SectionDialog'
4745import { SentryRoutes } from 'lib/sentry'
46+ import '../flags'
4847
4948window . flag = window . flag || flag
5049window . minilog = minilog
5150
52- const App = ( { accounts , konnectors , triggers } ) => {
51+ const App = ( ) => {
5352 const { isMobile } = useBreakpoints ( )
5453 const [ contentWrapper , setContentWrapper ] = useState ( undefined )
55- const [ isFetching , setIsFetching ] = useState (
56- [ accounts , konnectors , triggers ] . some ( collection =>
57- [ 'pending' , 'loading' ] . includes ( collection . fetchStatus )
58- )
59- )
60- const [ hasError , setHasError ] = useState ( false )
61- const [ isReady , setIsReady ] = useState ( false )
62- const [ appsReady , setAppsReady ] = useState ( false )
54+
55+ const [ didInit , setDidInit ] = useState ( false )
6356 const webviewIntent = useWebviewIntent ( )
6457 const theme = useCozyTheme ( )
6558
@@ -93,48 +86,28 @@ const App = ({ accounts, konnectors, triggers }) => {
9386 const shortcutsDirectories = canHaveShortcuts
9487 ? formatShortcuts ( folders , customHomeShortcuts )
9588 : null
96- const context = useQuery ( contextQuery . definition , contextQuery . options )
89+
90+ const { isFetching, hasError } = useFetchInitialData ( )
9791
9892 const showAssistantForMobile = isFlagshipApp ( )
9993 ? flag ( 'cozy.searchbar.enabled-for-flagship' )
10094 : flag ( 'cozy.searchbar.enabled' ) && isMobile
10195
102- useEffect ( ( ) => {
103- setIsFetching (
104- [ accounts , konnectors , triggers , context ] . some ( collection =>
105- [ 'pending' , 'loading' ] . includes ( collection . fetchStatus )
106- )
107- )
108- setHasError (
109- [ accounts , konnectors , triggers , context ] . find (
110- collection => collection . fetchStatus === 'failed'
111- )
112- )
113- } , [ accounts , konnectors , triggers , context ] )
114-
115- if ( context ?. attributes ?. features ) {
116- const flags = toFlagNames ( context . attributes . features )
117- enableFlags ( flags )
118- }
119-
120- useEffect ( ( ) => {
121- setIsReady (
122- appsReady &&
123- ! hasError &&
124- ! isFetching &&
125- shortcutsDirectories !== undefined
126- )
127- } , [ appsReady , hasError , isFetching , shortcutsDirectories ] )
128-
129- useEffect ( ( ) => {
130- if ( isReady && webviewIntent ) {
96+ if (
97+ ! didInit &&
98+ ! hasError &&
99+ ! isFetching &&
100+ shortcutsDirectories !== undefined
101+ ) {
102+ if ( webviewIntent ) {
131103 webviewIntent . call ( 'setTheme' , theme . variant )
132104 webviewIntent . call ( 'hideSplashScreen' )
133105 }
134- if ( isReady && ! webviewIntent && __SIMULATE_FLAGSHIP__ ) {
106+ if ( ! webviewIntent && __SIMULATE_FLAGSHIP__ ) {
135107 document . getElementById ( 'splashscreen' ) . style . display = 'none'
136108 }
137- } , [ isReady , theme , webviewIntent ] )
109+ setDidInit ( true )
110+ }
138111
139112 return (
140113 < >
@@ -145,7 +118,7 @@ const App = ({ accounts, konnectors, triggers }) => {
145118 < Corner />
146119 < div
147120 className = "u-flex u-flex-column u-flex-content-start u-flex-content-stretch u-w-100 u-m-auto u-pos-relative"
148- ref = { isReady ? div => setContentWrapper ( div ) : null }
121+ ref = { didInit ? div => setContentWrapper ( div ) : null }
149122 >
150123 < MoveModal />
151124 < HeroHeader />
@@ -167,7 +140,6 @@ const App = ({ accounts, konnectors, triggers }) => {
167140 element = {
168141 < Home
169142 wrapper = { contentWrapper }
170- setAppsReady = { ( ) => setAppsReady ( true ) }
171143 shortcutsDirectories = { shortcutsDirectories }
172144 />
173145 }
@@ -206,4 +178,4 @@ const App = ({ accounts, konnectors, triggers }) => {
206178 )
207179}
208180
209- export default appEntryPoint ( App )
181+ export default App
0 commit comments