import * as ReactDOM from 'react-dom' ; import * as React from 'react' ; import { IEventDispatcher , SimpleReactor } from '../../reactor/SimpleReactor' ; import { IDocInfo } from 'polar-shared/src/metadata/IDocInfo' ; import { AppInstance } from '../../electron/framework/AppInstance' ; import { PersistenceLayerManager , } from '../../datastore/PersistenceLayerManager' ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { RepoDocMetaManager } from '../../../../apps/repository/js/RepoDocMetaManager' ; import { RepoDocMetaLoader } from '../../../../apps/repository/js/RepoDocMetaLoader' ; import { Accounts } from '../../accounts/Accounts' ; import { App , AppInitializer } from "./AppInitializer" ; import { RepositoryApp } from './RepositoryApp' ; import { Tracer } from 'polar-shared/src/util/Tracer' ; import { AuthHandlers } from "./auth_handler/AuthHandler" ; import { AppRuntime } from "polar-shared/src/util/AppRuntime" ; import { ConsoleRecorder } from "polar-shared/src/util/ConsoleRecorder" ; import { SentryBrowser } from "../../logger/SentryBrowser" ; const log = Logger . create ( ) ; export class Repository { constructor ( private readonly persistenceLayerManager = new PersistenceLayerManager ( ) , private readonly repoDocMetaManager = new RepoDocMetaManager ( persistenceLayerManager ) , private readonly repoDocMetaLoader = new RepoDocMetaLoader ( persistenceLayerManager ) ) { } public async start ( ) { SentryBrowser . initWhenNecessary ( ) ; ConsoleRecorder . start ( ) ; console . log ( "Starting repository with app runtime: " + AppRuntime . get ( ) ) ; const updatedDocInfoEventDispatcher = new SimpleReactor ( ) ; const persistenceLayerManager = this . persistenceLayerManager ; const app = await AppInitializer . init ( { persistenceLayerManager , onNeedsAuthentication : async ( app ) => { } } ) ; Accounts . listenForPlanUpgrades ( ) . catch ( err => log . error ( "Unable to listen for plan upgrades: " , err ) ) ; const rootElement = getRootElement ( ) ; ReactDOM . render ( < RepositoryApp app = { app } persistenceLayerManager = { persistenceLayerManager } repoDocMetaManager = { this . repoDocMetaManager } repoDocMetaLoader = { this . repoDocMetaLoader } updatedDocInfoEventDispatcher = { updatedDocInfoEventDispatcher } onFileUpload = { this . onFileUpload } / > , rootElement ) ; const handleAuth = async ( ) => { const authHandler = AuthHandlers . get ( ) ; console . log ( "Getting auth status..." ) ; const authStatus = await Tracer . async ( authHandler . status ( ) , 'auth-handler' ) ; console . log ( "Getting auth status...done" ) ; if ( authStatus . type !== 'needs-authentication' ) { this . handleRepoDocInfoEvents ( ) ; await this . repoDocMetaLoader . start ( ) ; await persistenceLayerManager . start ( ) ; console . log ( "Started repo doc loader." ) ; } } ; handleAuth ( ) . catch ( err => log . error ( "Could not handle auth: " , err ) ) ; AppInstance . notifyStarted ( 'RepositoryApp' ) ; } private onFileUpload ( ) { console . log ( "File uploaded and sending event via postMessage" ) ; window . postMessage ( { type : 'file-uploaded' } , '*' ) ; } private handleRepoDocInfoEvents ( ) { this . repoDocMetaLoader . addEventListener ( event => { for ( const mutation of event . mutations ) { if ( mutation . mutationType === 'created' || mutation . mutationType === 'updated' ) { this . repoDocMetaManager . updateFromRepoDocMeta ( mutation . fingerprint , mutation . repoDocMeta ! ) ; } else { this . repoDocMetaManager . updateFromRepoDocMeta ( mutation . fingerprint , undefined ) ; } } } ) ; } } function getRootElement ( ) { const rootElement = document . getElementById ( 'root' ) as HTMLElement ; if ( ! rootElement ) { throw new Error ( "No root element to which to render" ) ; } return rootElement ; }