From 2235acc72f33caa67a67650cdb9a7a4512fda50f Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Fri, 2 Aug 2024 12:50:42 +0000
Subject: [PATCH] feat: improve app load time

---
 src/components/Splash/SplashRoot.tsx          | 41 ++++++++++---------
 .../__snapshots__/SplashRoot.spec.tsx.snap    |  6 +--
 .../__snapshots__/SplashScreen.spec.tsx.snap  |  4 +-
 src/models/initialisationSteps.model.ts       |  6 +--
 src/services/initialization.service.ts        |  1 +
 5 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/src/components/Splash/SplashRoot.tsx b/src/components/Splash/SplashRoot.tsx
index af2214bb0..3b4e51866 100644
--- a/src/components/Splash/SplashRoot.tsx
+++ b/src/components/Splash/SplashRoot.tsx
@@ -19,7 +19,6 @@ import {
   PartnersInfo,
   Profile,
   ProfileType,
-  UserChallenge,
 } from 'models'
 import React, { ReactNode, useCallback, useEffect, useState } from 'react'
 import ActionService from 'services/action.service'
@@ -195,19 +194,24 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => {
       const transaction = Sentry.startTransaction({ name: 'Initialize app' })
       try {
         console.groupCollapsed('Initialization logs')
-        // init Terms
-        const termsStatus = await initializationService.initConsent()
+        // Run init steps in parallel
+        const [
+          termsStatus,
+          ,
+          profile,
+          profileType,
+          profileEcogesture,
+          fluidStatus,
+        ] = await Promise.all([
+          initializationService.initConsent(),
+          initializationService.initFluidPrices(),
+          initializationService.initProfile(),
+          initializationService.initProfileType(),
+          initializationService.initProfileEcogesture(),
+          initializationService.initFluidStatus(),
+        ])
         if (subscribed) dispatch(updateTermsStatus(termsStatus))
 
-        // Init fluidPrices
-        await initializationService.initFluidPrices()
-
-        // Init profile and update ecogestures, challenges, analysis
-        const profile = await initializationService.initProfile()
-        const profileType = await initializationService.initProfileType()
-        const profileEcogesture =
-          await initializationService.initProfileEcogesture()
-
         const migrationsResult = await ms.runMigrations(migrations)
         // Init last release notes when they exist
         dispatch(
@@ -250,11 +254,11 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => {
           }
           dispatch(toggleAnalysisNotification(!profile.haveSeenLastAnalysis))
         }
-        // Init Fluid status && lastDate for the chart
-        const fluidStatus = await initializationService.initFluidStatus()
+
+        // Process fluids status
         if (subscribed) {
           dispatch(setFluidStatus(fluidStatus))
-          let lastDataDate: DateTime | null = DateTime.fromISO('0001-01-01')
+          let lastDataDate = DateTime.fromISO('0001-01-01')
           for (const fluid of fluidStatus) {
             if (fluid.lastDataDate && fluid.lastDataDate > lastDataDate) {
               lastDataDate = fluid.lastDataDate
@@ -282,10 +286,9 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => {
             UserActionState.ONGOING
           ) {
             const actionService = new ActionService(client)
-            const updatedUserChallenge: UserChallenge | null =
-              await actionService.isActionDone(
-                filteredCurrentOngoingChallenge[0]
-              )
+            const updatedUserChallenge = await actionService.isActionDone(
+              filteredCurrentOngoingChallenge[0]
+            )
             if (updatedUserChallenge) {
               dispatch(updateUserChallengeList(updatedUserChallenge))
             }
diff --git a/src/components/Splash/__snapshots__/SplashRoot.spec.tsx.snap b/src/components/Splash/__snapshots__/SplashRoot.spec.tsx.snap
index 1cc016b6b..17d478907 100644
--- a/src/components/Splash/__snapshots__/SplashRoot.spec.tsx.snap
+++ b/src/components/Splash/__snapshots__/SplashRoot.spec.tsx.snap
@@ -27,7 +27,7 @@ exports[`SplashRoot component should be rendered correctly 1`] = `
           >
             <div
               class="splash-progress-bar-content"
-              style="left: -73%;"
+              style="left: -40%;"
             />
           </div>
         </div>
@@ -35,7 +35,7 @@ exports[`SplashRoot component should be rendered correctly 1`] = `
       <div
         class="step-label text-18-normal"
       >
-        splashscreen.step.1
+        splashscreen.step.3
       </div>
       <div
         class="splash-logos-container"
@@ -83,7 +83,7 @@ exports[`SplashRoot component should render the error screen 1`] = `
         <div
           class="splash-error-text text-18-normal"
         >
-          splashscreen.consent_error
+          splashscreen.consos_error
         </div>
       </div>
     </div>
diff --git a/src/components/Splash/__snapshots__/SplashScreen.spec.tsx.snap b/src/components/Splash/__snapshots__/SplashScreen.spec.tsx.snap
index fee381e60..41988b376 100644
--- a/src/components/Splash/__snapshots__/SplashScreen.spec.tsx.snap
+++ b/src/components/Splash/__snapshots__/SplashScreen.spec.tsx.snap
@@ -23,7 +23,7 @@ exports[`SplashScreen component should be rendered correctly 1`] = `
         >
           <div
             class="splash-progress-bar-content"
-            style="left: -73%;"
+            style="left: -80%;"
           />
         </div>
       </div>
@@ -31,7 +31,7 @@ exports[`SplashScreen component should be rendered correctly 1`] = `
     <div
       class="step-label text-18-normal"
     >
-      splashscreen.step.1
+      splashscreen.step.0
     </div>
     <div
       class="splash-logos-container"
diff --git a/src/models/initialisationSteps.model.ts b/src/models/initialisationSteps.model.ts
index fd9cb97b4..9ccccf4f4 100644
--- a/src/models/initialisationSteps.model.ts
+++ b/src/models/initialisationSteps.model.ts
@@ -1,10 +1,10 @@
 export enum InitSteps {
-  MIGRATION,
   CONSENT,
+  PRICES,
   PROFILE,
-  CHALLENGES,
-  PRICES, // never used
   CONSOS,
+  MIGRATION,
+  CHALLENGES,
 }
 export enum InitStepsErrors {
   MIGRATION_ERROR = 'migration_error',
diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts
index b30bb683a..e01b9dd78 100644
--- a/src/services/initialization.service.ts
+++ b/src/services/initialization.service.ts
@@ -160,6 +160,7 @@ export default class InitializationService {
 
   public async initFluidPrices(): Promise<void> {
     const startTime = performance.now()
+    this._setInitStep(InitSteps.PRICES)
     logDuration('[Initialization] Launching fluidPrices service', startTime)
     const triggerQuery: QueryDefinition = Q(TRIGGERS_DOCTYPE).where({
       'message.name': 'fluidsPrices',
-- 
GitLab