1- import { useEffect } from 'react' ;
1+ import { useEffect , useMemo , useRef } from 'react' ;
22
33import { differenceInDays , fromUnixTime } from 'date-fns' ;
44
@@ -16,27 +16,30 @@ import { useMailGlobalModals } from '../containers/globalModals/GlobalModalProvi
1616import { ModalType } from '../containers/globalModals/inteface' ;
1717
1818const useShowBYOESpotlightModal = ( ) => {
19+ const notifiedRef = useRef ( false ) ;
1920 const [ user ] = useUser ( ) ;
2021 const [ addresses = [ ] ] = useAddresses ( ) ;
2122 const allSyncs = useEasySwitchSelector ( getAllSync ) ;
2223 const syncListLoaded = useEasySwitchSelector ( selectSyncListLoadingState ) ;
23- const { feature, update } = useFeature ( FeatureCode . BYOESpotlightModal ) ;
24+ const { feature, update, loading : loadingFeature } = useFeature ( FeatureCode . BYOESpotlightModal ) ;
2425 const { notify } = useMailGlobalModals ( ) ;
2526
2627 const [ hasAccessToBYOE , loadingBYOEFeatureStatus ] = useBYOEFeatureStatus ( false ) ;
2728
28- const emailsFromAddresses = new Set ( addresses . map ( ( address ) => address . Email ) ) ;
29- const { forwardingSyncs, byoeSyncs } = allSyncs . reduce < { forwardingSyncs : Sync [ ] ; byoeSyncs : Sync [ ] } > (
30- ( acc , sync ) => {
31- if ( emailsFromAddresses . has ( sync . account ) ) {
32- acc . byoeSyncs . push ( sync ) ;
33- } else {
34- acc . forwardingSyncs . push ( sync ) ;
35- }
36- return acc ;
37- } ,
38- { forwardingSyncs : [ ] , byoeSyncs : [ ] }
39- ) ;
29+ const { forwardingSyncs, byoeSyncs } = useMemo ( ( ) => {
30+ const emailsFromAddresses = new Set ( addresses . map ( ( address ) => address . Email ) ) ;
31+ return allSyncs . reduce < { forwardingSyncs : Sync [ ] ; byoeSyncs : Sync [ ] } > (
32+ ( acc , sync ) => {
33+ if ( emailsFromAddresses . has ( sync . account ) ) {
34+ acc . byoeSyncs . push ( sync ) ;
35+ } else {
36+ acc . forwardingSyncs . push ( sync ) ;
37+ }
38+ return acc ;
39+ } ,
40+ { forwardingSyncs : [ ] , byoeSyncs : [ ] }
41+ ) ;
42+ } , [ addresses , allSyncs ] ) ;
4043
4144 // Show the modal if:
4245 // - User has access to BYOE (feature flag ON && not b2b sub user)
@@ -45,17 +48,20 @@ const useShowBYOESpotlightModal = () => {
4548 // - User never seen the modal before (old feature flag system)
4649 // - User is not busy
4750 const canShowBYOESpotlightModal =
51+ ! notifiedRef . current &&
4852 ! loadingBYOEFeatureStatus &&
4953 hasAccessToBYOE &&
5054 ! getIsBYOEOnlyAccount ( addresses ) &&
5155 byoeSyncs . length === 0 &&
5256 syncListLoaded === 'success' &&
5357 differenceInDays ( new Date ( ) , fromUnixTime ( user . CreateTime ) ) >= 30 &&
58+ ! loadingFeature &&
5459 ! ! feature ?. Value &&
5560 ! domIsBusy ( ) ;
5661
5762 useEffect ( ( ) => {
5863 if ( canShowBYOESpotlightModal ) {
64+ notifiedRef . current = true ; // We only want to notify once
5965 notify ( {
6066 type : ModalType . BYOESpotlight ,
6167 value : {
0 commit comments