import { Component , Injector } from "@angular/core" ; import type { OnInit } from "@angular/core" ; import { Subject , combineLatest , from , lastValueFrom , race } from "rxjs" ; import { distinctUntilChanged , filter , map , pairwise , take , takeUntil , withLatestFrom } from "rxjs/operators" ; import { equals , pick } from "remeda" ; import { ACCOUNTS_ACTIONS } from "src/web/browser-window/app/store/actions" ; import { AccountConfig } from "src/shared/model/account" ; import { AccountViewAbstractComponent } from "src/web/browser-window/app/_accounts/account-view-abstract.component" ; import { AccountsService } from "src/web/browser-window/app/_accounts/accounts.service" ; import { getWebLogger } from "src/web/browser-window/util" ; import { testProtonMailAppPage } from "src/shared/util" ; @ Component ( { selector : "electron-mail-account-view-primary" , template : "" , } ) export class AccountViewPrimaryComponent extends AccountViewAbstractComponent implements OnInit { private readonly logger = getWebLogger ( __filename , nameof ( AccountViewPrimaryComponent ) ) ; private readonly accountsService : AccountsService ; constructor ( injector ) { super ( "primary" , injector ) ; this . accountsService = injector . get ( AccountsService ) ; } ngOnInit ( ) { this . addSubscription ( combineLatest ( [ this . filterDomReadyEvent ( ) . pipe ( filter ( ( { webView : { src : url } } ) => testProtonMailAppPage ( { url , logger : this . logger } ) . projectType === "proton-mail" ) ) , this . account$ . pipe ( pairwise ( ) , filter ( ( [ prev , curr ] ) => { return ( ( curr . notifications . pageType . type !== "unknown" && prev . notifications . pageType . type !== curr . notifications . pageType . type ) || ! equals ( this . pickCredentialFields ( prev . accountConfig ) , this . pickCredentialFields ( curr . accountConfig ) ) || ! equals ( this . pickLoginDelayFields ( prev . accountConfig ) , this . pickLoginDelayFields ( curr . accountConfig ) ) ) ; } ) ) ] ) . pipe ( withLatestFrom ( this . account$ ) ) . subscribe ( ( [ [ { webView } ] , account ] ) => { if ( account . notifications . pageType . type === "unknown" ) { return ; } this . log ( "info" , [ `dispatch "TryToLogin"` ] ) ; this . action ( ACCOUNTS_ACTIONS . TryToLogin ( { account , webView } ) ) ; } ) ) ; this . addSubscription ( this . filterDomReadyEvent ( ) . subscribe ( ( { webView } ) => { this . action ( this . accountsService . generatePrimaryNotificationsStateResetAction ( { login : this . account . accountConfig . login } ) ) ; if ( ! testProtonMailAppPage ( { url : webView . src , logger : this . logger } ) . shouldInitProviderApi ) { this . log ( "info" , [ `skip webview.dom-ready processing for ${ webView . src } page` ] ) ; return ; } const finishPromise = this . filterDomReadyOrDestroyedPromise ( ) ; const breakPreviousSyncing$ = new Subject < void > ( ) ; this . action ( ACCOUNTS_ACTIONS . SetupPrimaryNotificationChannel ( { account : this . account , webView , finishPromise } ) ) ; this . account$ . pipe ( map ( ( { notifications , accountConfig , accountIndex } ) => ( { pk : { login : accountConfig . login , accountIndex } , data : { loggedIn : notifications . loggedIn , database : accountConfig . database } , } ) ) , distinctUntilChanged ( ( { data : prev } , { data : curr } ) => equals ( prev , curr ) ) , takeUntil ( from ( finishPromise ) ) ) . subscribe ( ( { pk , data : { loggedIn , database } } ) => { breakPreviousSyncing$ . next ( void 0 ) ; if ( ! loggedIn || ! database ) { return ; } this . action ( ACCOUNTS_ACTIONS . ToggleSyncing ( { pk , webView , finishPromise : lastValueFrom ( race ( [ from ( finishPromise ) , breakPreviousSyncing$ . pipe ( take ( 1 ) ) , ] ) ) , } ) ) ; } ) ; } ) ) ; } private pickCredentialFields ( value ) { return pick ( value , [ "credentials" ] ) ; } private pickLoginDelayFields ( value ) { return pick ( value , [ "loginDelayUntilSelected" , "loginDelaySecondsRange" ] ) ; } }