1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-27 10:03:23 +00:00
Files
browser/libs/angular/src/billing/directives/premium.directive.ts
Conner Turnbull 26a0594056 [PM-17655] Billing Code Ownership Updates (#13105)
* Moved has-premium.guard under billing

* Moved free-trial.ts to billing

* Moved premium directives to billing

* Moved families-policy.service.ts to billing

* Moved trial initiation from auth to billing
2025-01-28 13:17:00 -05:00

47 lines
1.4 KiB
TypeScript

import { Directive, OnDestroy, OnInit, TemplateRef, ViewContainerRef } from "@angular/core";
import { of, Subject, switchMap, takeUntil } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
/**
* Only shows the element if the user has premium.
*/
@Directive({
selector: "[appPremium]",
})
export class PremiumDirective implements OnInit, OnDestroy {
private directiveIsDestroyed$ = new Subject<boolean>();
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private billingAccountProfileStateService: BillingAccountProfileStateService,
private accountService: AccountService,
) {}
async ngOnInit(): Promise<void> {
this.accountService.activeAccount$
.pipe(
switchMap((account) =>
account
? this.billingAccountProfileStateService.hasPremiumFromAnySource$(account.id)
: of(false),
),
takeUntil(this.directiveIsDestroyed$),
)
.subscribe((premium: boolean) => {
if (premium) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
});
}
ngOnDestroy() {
this.directiveIsDestroyed$.next(true);
this.directiveIsDestroyed$.complete();
}
}