2023-06-15 23:53:21 +02:00
|
|
|
import { Directive, Input, OnInit, TemplateRef, ViewContainerRef } from "@angular/core";
|
|
|
|
|
2023-09-08 16:05:37 +02:00
|
|
|
import { FeatureFlag, FeatureFlagValue } from "@bitwarden/common/enums/feature-flag.enum";
|
2023-06-15 23:53:21 +02:00
|
|
|
import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction";
|
|
|
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive that conditionally renders the element when the feature flag is enabled and/or
|
|
|
|
* matches the value specified by {@link appIfFeatureValue}.
|
|
|
|
*
|
|
|
|
* When a feature flag is not found in the config service, the element is hidden.
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: "[appIfFeature]",
|
|
|
|
})
|
|
|
|
export class IfFeatureDirective implements OnInit {
|
|
|
|
/**
|
|
|
|
* The feature flag to check.
|
|
|
|
*/
|
|
|
|
@Input() appIfFeature: FeatureFlag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optional value to compare against the value of the feature flag in the config service.
|
|
|
|
* @default true
|
|
|
|
*/
|
2023-09-08 16:05:37 +02:00
|
|
|
@Input() appIfFeatureValue: FeatureFlagValue = true;
|
2023-06-15 23:53:21 +02:00
|
|
|
|
|
|
|
private hasView = false;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private templateRef: TemplateRef<any>,
|
|
|
|
private viewContainer: ViewContainerRef,
|
|
|
|
private configService: ConfigServiceAbstraction,
|
|
|
|
private logService: LogService,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
try {
|
2023-09-08 16:05:37 +02:00
|
|
|
const flagValue = await this.configService.getFeatureFlag(this.appIfFeature);
|
2023-06-15 23:53:21 +02:00
|
|
|
|
|
|
|
if (this.appIfFeatureValue === flagValue) {
|
|
|
|
if (!this.hasView) {
|
|
|
|
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
|
|
this.hasView = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.viewContainer.clear();
|
|
|
|
this.hasView = false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
this.viewContainer.clear();
|
|
|
|
this.hasView = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|