2023-05-19 17:35:42 +01:00
|
|
|
import { animate, state, style, transition, trigger } from "@angular/animations";
|
|
|
|
|
import { ConnectedPosition } from "@angular/cdk/overlay";
|
|
|
|
|
import { Component, EventEmitter, OnDestroy, OnInit, Output } from "@angular/core";
|
|
|
|
|
import { Router } from "@angular/router";
|
2023-05-25 14:38:23 +01:00
|
|
|
import { Subject, takeUntil } from "rxjs";
|
2023-05-19 17:35:42 +01:00
|
|
|
|
|
|
|
|
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
2023-06-06 15:34:53 -05:00
|
|
|
import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction";
|
|
|
|
|
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
2023-05-19 17:35:42 +01:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: "environment-selector",
|
|
|
|
|
templateUrl: "environment-selector.component.html",
|
|
|
|
|
animations: [
|
|
|
|
|
trigger("transformPanel", [
|
|
|
|
|
state(
|
|
|
|
|
"void",
|
|
|
|
|
style({
|
|
|
|
|
opacity: 0,
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
transition(
|
|
|
|
|
"void => open",
|
|
|
|
|
animate(
|
|
|
|
|
"100ms linear",
|
|
|
|
|
style({
|
|
|
|
|
opacity: 1,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
transition("* => void", animate("100ms linear", style({ opacity: 0 }))),
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
export class EnvironmentSelectorComponent implements OnInit, OnDestroy {
|
|
|
|
|
@Output() onOpenSelfHostedSettings = new EventEmitter();
|
2023-05-25 14:38:23 +01:00
|
|
|
euServerFlagEnabled: boolean;
|
2023-05-19 17:35:42 +01:00
|
|
|
isOpen = false;
|
|
|
|
|
showingModal = false;
|
|
|
|
|
selectedEnvironment: ServerEnvironment;
|
|
|
|
|
ServerEnvironmentType = ServerEnvironment;
|
|
|
|
|
overlayPostition: ConnectedPosition[] = [
|
|
|
|
|
{
|
|
|
|
|
originX: "start",
|
|
|
|
|
originY: "bottom",
|
|
|
|
|
overlayX: "start",
|
|
|
|
|
overlayY: "top",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
protected componentDestroyed$: Subject<void> = new Subject();
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
protected environmentService: EnvironmentService,
|
|
|
|
|
protected configService: ConfigServiceAbstraction,
|
|
|
|
|
protected router: Router
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async ngOnInit() {
|
2023-05-25 14:38:23 +01:00
|
|
|
this.configService.serverConfig$.pipe(takeUntil(this.componentDestroyed$)).subscribe(() => {
|
|
|
|
|
this.updateEnvironmentInfo();
|
|
|
|
|
});
|
2023-05-19 17:35:42 +01:00
|
|
|
this.updateEnvironmentInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
this.componentDestroyed$.next();
|
|
|
|
|
this.componentDestroyed$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async toggle(option: ServerEnvironment) {
|
|
|
|
|
this.isOpen = !this.isOpen;
|
2023-05-25 14:38:23 +01:00
|
|
|
if (option === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-19 17:35:42 +01:00
|
|
|
if (option === ServerEnvironment.EU) {
|
|
|
|
|
await this.environmentService.setUrls({ base: "https://vault.bitwarden.eu" });
|
|
|
|
|
} else if (option === ServerEnvironment.US) {
|
|
|
|
|
await this.environmentService.setUrls({ base: "https://vault.bitwarden.com" });
|
|
|
|
|
} else if (option === ServerEnvironment.SelfHosted) {
|
|
|
|
|
this.onOpenSelfHostedSettings.emit();
|
|
|
|
|
}
|
|
|
|
|
this.updateEnvironmentInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 14:38:23 +01:00
|
|
|
async updateEnvironmentInfo() {
|
|
|
|
|
this.euServerFlagEnabled = await this.configService.getFeatureFlagBool(
|
|
|
|
|
FeatureFlag.DisplayEuEnvironmentFlag
|
|
|
|
|
);
|
2023-05-19 17:35:42 +01:00
|
|
|
const webvaultUrl = this.environmentService.getWebVaultUrl();
|
|
|
|
|
if (this.environmentService.isSelfHosted()) {
|
|
|
|
|
this.selectedEnvironment = ServerEnvironment.SelfHosted;
|
|
|
|
|
} else if (webvaultUrl != null && webvaultUrl.includes("bitwarden.eu")) {
|
|
|
|
|
this.selectedEnvironment = ServerEnvironment.EU;
|
|
|
|
|
} else {
|
|
|
|
|
this.selectedEnvironment = ServerEnvironment.US;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close() {
|
|
|
|
|
this.isOpen = false;
|
|
|
|
|
this.updateEnvironmentInfo();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ServerEnvironment {
|
|
|
|
|
US = "US",
|
|
|
|
|
EU = "EU",
|
|
|
|
|
SelfHosted = "Self-hosted",
|
|
|
|
|
}
|