import { CoreStart } from 'opensearch-dashboards/public' ; import { renderOptedInNoticeBanner } from './render_opted_in_notice_banner' ; import { renderOptInBanner } from './render_opt_in_banner' ; import { TelemetryService } from '../telemetry_service' ; interface TelemetryNotificationsConstructor { overlays : CoreStart [ 'overlays' ] ; telemetryService : TelemetryService ; } export class TelemetryNotifications { private readonly overlays : CoreStart [ 'overlays' ] ; private readonly telemetryService : TelemetryService ; private optedInNoticeBannerId ? : string ; private optInBannerId ? : string ; constructor ( { overlays , telemetryService } ) { this . telemetryService = telemetryService ; this . overlays = overlays ; } public shouldShowOptedInNoticeBanner = ( ) : boolean => { const userShouldSeeOptInNotice = this . telemetryService . getUserShouldSeeOptInNotice ( ) ; const bannerOnScreen = typeof this . optedInNoticeBannerId !== 'undefined' ; return ! bannerOnScreen && userShouldSeeOptInNotice ; } ; public renderOptedInNoticeBanner = ( ) : void => { const bannerId = renderOptedInNoticeBanner ( { onSeen : this . setOptedInNoticeSeen , overlays : this . overlays , } ) ; this . optedInNoticeBannerId = bannerId ; } ; public shouldShowOptInBanner = ( ) : boolean => { const isOptedIn = this . telemetryService . getIsOptedIn ( ) ; const bannerOnScreen = typeof this . optInBannerId !== 'undefined' ; return ! bannerOnScreen && isOptedIn === null ; } ; public renderOptInBanner = ( ) : void => { const bannerId = renderOptInBanner ( { setOptIn : this . onSetOptInClick , overlays : this . overlays , } ) ; this . optInBannerId = bannerId ; } ; private onSetOptInClick = async ( isOptIn ) => { if ( this . optInBannerId ) { this . overlays . banners . remove ( this . optInBannerId ) ; this . optInBannerId = undefined ; } await this . telemetryService . setOptIn ( isOptIn ) ; } ; public setOptedInNoticeSeen = async ( ) : Promise < void > => { if ( this . optedInNoticeBannerId ) { this . overlays . banners . remove ( this . optedInNoticeBannerId ) ; this . optedInNoticeBannerId = undefined ; } await this . telemetryService . setUserHasSeenNotice ( ) ; } ; }