import { observable , computed , makeObservable } from 'mobx' ; import type { IProperty } from '@cloudbeaver/core-blocks' ; import { injectable , IInitializableController , IDestructibleController } from '@cloudbeaver/core-di' ; import { CommonDialogService } from '@cloudbeaver/core-dialogs' ; import { NotificationService } from '@cloudbeaver/core-events' ; import { ErrorDetailsDialog } from '@cloudbeaver/core-notifications' ; import { DataTransferProcessorInfo , GQLErrorCatcher } from '@cloudbeaver/core-sdk' ; import { DataExportService } from '../DataExportService' ; import type { IExportContext } from '../IExportContext' ; export enum DataExportStep { DataTransferProcessor , Configure } @ injectable ( ) export class DataExportController implements IInitializableController , IDestructibleController { step = DataExportStep . DataTransferProcessor ; get isLoading ( ) : boolean { return this . dataExportService . processors . isLoading ( ) ; } isExporting = false ; processor : DataTransferProcessorInfo | null = null ; get processors ( ) : DataTransferProcessorInfo [ ] { return Array . from ( this . dataExportService . processors . data . values ( ) ) . sort ( sortProcessors ) ; } processorProperties : any = { } ; properties : IProperty [ ] = [ ] ; readonly error = new GQLErrorCatcher ( ) ; private context ! : IExportContext ; private close ! : ( ) => void ; private isDistructed = false ; constructor ( private dataExportService , private notificationService , private commonDialogService ) { makeObservable ( this , { step : observable , isExporting : observable , processor : observable , processors : computed , processorProperties : observable , properties : observable , } ) ; } init ( context , close ) { this . context = context ; this . close = close ; this . loadProcessors ( ) ; } destruct ( ) { this . isDistructed = true ; } prepareExport = async ( ) => { if ( ! this . processor || this . isExporting ) { return ; } this . isExporting = true ; try { await this . dataExportService . exportData ( this . context , { processorId : this . processor . id , processorProperties : this . processorProperties , } ) ; this . close ( ) ; } catch ( exception ) { if ( ! this . error . catch ( exception ) || this . isDistructed ) { this . notificationService . logException ( exception , 'Can\'t export' ) ; } } finally { this . isExporting = false ; this . close ( ) ; } } ; setStep = ( step ) => { this . step = step ; } ; selectProcessor = ( processorId ) => { this . processor = this . dataExportService . processors . data . get ( processorId ) ! ; this . properties = this . processor . properties ?. map < IProperty > ( property => ( { id : property . id ! , key : property . id ! , displayName : property . displayName , description : property . description , validValues : property . validValues , defaultValue : property . defaultValue , valuePlaceholder : property . defaultValue , } ) ) || [ ] ; this . processorProperties = { } ; this . step = DataExportStep . Configure ; this . error . clear ( ) ; } ; showDetails = ( ) => { if ( this . error . exception ) { this . commonDialogService . open ( ErrorDetailsDialog , this . error . exception ) ; } } ; private async loadProcessors ( ) { try { await this . dataExportService . processors . load ( ) ; } catch ( exception ) { this . notificationService . logException ( exception , 'Can\'t load data export processors' ) ; } } } function sortProcessors ( processorA , processorB ) { if ( processorA . order === processorB . order ) { return ( processorA . name || '' ) . localeCompare ( ( processorB . name || '' ) ) ; } return processorA . order - processorB . order ; }