-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Simplify and speed up Home initialization
We get rid of the complicated state management to determine whether or not the Home is initialized, to rely on a dedicated hook in charge of fetching all the required data. Note the Home is relying on this mechanism to later fetch data directly from the store. In the future, we might have the same logic, but relying on the dataproxy directly
- Loading branch information
1 parent
7c1b67e
commit c9f8817
Showing
4 changed files
with
106 additions
and
63 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useQuery } from 'cozy-client' | ||
import { | ||
makeAccountsQuery, | ||
makeAppsQuery, | ||
makeKonnectorsQuery, | ||
makeTriggersQuery | ||
} from 'queries' | ||
import log from 'cozy-logger' | ||
|
||
export const useFetchInitialData = () => { | ||
const accountsQuery = useQuery( | ||
makeAccountsQuery.definition, | ||
makeAccountsQuery.options | ||
) | ||
const konnectorsQuery = useQuery( | ||
makeKonnectorsQuery.definition, | ||
makeKonnectorsQuery.options | ||
) | ||
const appsQuery = useQuery(makeAppsQuery.definition, makeAppsQuery.options) | ||
const triggersQuery = useQuery( | ||
makeTriggersQuery.definition, | ||
makeTriggersQuery.options | ||
) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const allQueries = [accountsQuery, konnectorsQuery, appsQuery, triggersQuery] | ||
|
||
const startTime = useRef(null) | ||
const loggedTime = useRef(false) | ||
|
||
useEffect(() => { | ||
if (startTime.current === null) { | ||
startTime.current = performance.now() | ||
} | ||
|
||
const allFetched = allQueries.every(query => query.fetchStatus === 'loaded') | ||
|
||
if (allFetched && !loggedTime.current) { | ||
const endTime = performance.now() | ||
log('info', `Required data fetched in ${endTime - startTime.current} ms`) | ||
loggedTime.current = true | ||
} | ||
}, [allQueries]) | ||
|
||
const isFetching = allQueries.some( | ||
query => query.fetchStatus === 'loading' || query.fetchStatus === 'pending' | ||
) | ||
const hasError = allQueries.some(query => query.fetchStatus === 'failed') | ||
|
||
return { | ||
isFetching, | ||
hasError, | ||
data: { | ||
accounts: accountsQuery.data, | ||
konnectors: konnectorsQuery.data, | ||
apps: appsQuery.data, | ||
triggers: triggersQuery.data | ||
} | ||
} | ||
} |
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