import { Component , Input , OnDestroy } from '@angular/core' ; import { ComposerService , AccessIdSubjectValue , LicenseSubjectValue , PostToPermawebSubjectValue , } from '../../../services/composer.service' ; import { BehaviorSubject , combineLatest , Subscription } from 'rxjs' ; import { LicensesEntry , LICENSES , ACCESS , } from '../../../../../services/list-options' ; import { PermawebTermsComponent } from '../../popup/permaweb/permaweb-terms.component' ; import { FeaturesService } from '../../../../../services/features.service' ; import { FormToastService } from '../../../../../common/services/form-toast.service' ; import { Session } from '../../../../../services/session' ; import { take } from 'rxjs/operators' ; import { PopupService } from '../../popup/popup.service' ; @ Component ( { selector : 'm-composerTitleBar__dropdown' , templateUrl : './dropdown.component.html' , } ) export class ComposerTitleBarDropdownComponent implements OnDestroy { @ Input ( ) anchorPosition = { top : '0' , left : '0' } ; @ Input ( ) displayAsButton : boolean = false ; /**
     * Visibility items list
     */ visibilityItems : Array < { text : string ; value : string ; } > = ACCESS . map ( ( { text , value } ) => ( { text , value : `${ value }` , } ) ) ; /**
     * License items list
     */ licenseItems : Array < LicensesEntry > = LICENSES . filter ( license => license . selectable ) ; permawebPostClickSubscription : Subscription ; constructor ( protected service , protected features , protected toaster , protected session , protected popup ) { } ngOnDestroy ( ) { if ( this . permawebPostClickSubscription ) { this . permawebPostClickSubscription . unsubscribe ( ) ; } } /**
     * Access ID subject from service
     */ get accessId$ ( ) : BehaviorSubject < AccessIdSubjectValue > { return this . service . accessId$ ; } /**
     * License subject from service
     */ get license$ ( ) : BehaviorSubject < LicenseSubjectValue > { return this . service . license$ ; } /**
     * Can the actor change visibility? (disabled when there's a container)
     */ get canChangeVisibility ( ) : boolean { return ! this . service . getContainerGuid ( ) ; } /**
     * Is editing? subject value from service
     */ get isEditing$ ( ) { return this . service . isEditing$ ; } /**
     * Is posting? subject value from service
     */ get isPosting$ ( ) { return this . service . isPosting$ ; } /**
     * Get monetization subject value from service
     */ get monetization$ ( ) { return this . service . monetization$ ; } /**
     * Post to permaweb subject from service
     */ get postToPermaweb$ ( ) : BehaviorSubject < PostToPermawebSubjectValue > { return this . service . postToPermaweb$ ; } param $event onVisibilityClick ( $event ) { if ( ! this . canChangeVisibility ) { return ; } if ( this . features . has ( 'permaweb' ) && this . service . postToPermaweb$ . getValue ( ) && $event !== 2 ) { this . toaster . warn ( 'Cannot set visibility to non-public on permaweb posts.' ) ; return ; } this . accessId$ . next ( $event ) ; } param $event onLicenseClick ( $event ) { this . license$ . next ( $event ) ; } returns { void } public async onPostToPermawebClick ( ) { this . permawebPostClickSubscription = combineLatest ( [ this . postToPermaweb$ , this . monetization$ , this . accessId$ , ] ) . pipe ( take ( 1 ) ) . subscribe ( async ( [ postToPermaweb , monetization , accessId ] ) => { if ( postToPermaweb ) { this . postToPermaweb$ . next ( ! postToPermaweb ) ; return ; } if ( monetization ) { this . toaster . warn ( 'Cannot post monetized posts to permaweb' ) ; return ; } if ( accessId !== '2' ) { this . toaster . warn ( 'Only public posts can be posted to the permaweb' ) ; return ; } await this . popup . create ( PermawebTermsComponent ) . present ( ) . toPromise ( ) ; } ) ; } returns { boolean } public shouldShowPermawebOption ( ) { return ( this . features . has ( 'permaweb' ) && ! this . service . isEditing$ . getValue ( ) && ! this . service . remind$ . getValue ( ) && this . canChangeVisibility ) ; } }