1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 01:03:35 +00:00

[SM-1065] SmOnboarding state provider (#8037)

* Create state definition

* Create SmOnboardingTaskService

* Replace usage of stateService value with state new state provider

* Migrate old state values to state provider

* Fix injection of SmOnboardingTasksService

* Remove smOnboardingTasks from state

* Fix state provider imports

* Fix migration after merge from main

* Move null handling to SMOnboardingTasksService
This commit is contained in:
Robyn MacCallum
2024-02-23 13:16:42 -05:00
committed by GitHub
parent dee0b20554
commit 8ab4eecc8a
9 changed files with 307 additions and 38 deletions

View File

@@ -11,12 +11,12 @@ import {
distinctUntilChanged,
take,
share,
firstValueFrom,
} from "rxjs";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
import { DialogService } from "@bitwarden/components";
import { ProjectListView } from "../models/view/project-list.view";
@@ -47,6 +47,8 @@ import {
import { ServiceAccountService } from "../service-accounts/service-account.service";
import { SecretsListComponent } from "../shared/secrets-list.component";
import { SMOnboardingTasks, SMOnboardingTasksService } from "./sm-onboarding-tasks.service";
type Tasks = {
[organizationId: string]: OrganizationTasks;
};
@@ -71,6 +73,7 @@ export class OverviewComponent implements OnInit, OnDestroy {
protected showOnboarding = false;
protected loading = true;
protected organizationEnabled = false;
protected onboardingTasks$: Observable<SMOnboardingTasks>;
protected view$: Observable<{
allProjects: ProjectListView[];
@@ -87,12 +90,14 @@ export class OverviewComponent implements OnInit, OnDestroy {
private serviceAccountService: ServiceAccountService,
private dialogService: DialogService,
private organizationService: OrganizationService,
private stateService: StateService,
private platformUtilsService: PlatformUtilsService,
private i18nService: I18nService,
private smOnboardingTasksService: SMOnboardingTasksService,
) {}
ngOnInit() {
this.onboardingTasks$ = this.smOnboardingTasksService.smOnboardingTasks$;
const orgId$ = this.route.params.pipe(
map((p) => p.organizationId),
distinctUntilChanged(),
@@ -184,7 +189,7 @@ export class OverviewComponent implements OnInit, OnDestroy {
organizationId: string,
orgTasks: OrganizationTasks,
): Promise<OrganizationTasks> {
const prevTasks = ((await this.stateService.getSMOnboardingTasks()) || {}) as Tasks;
const prevTasks = (await firstValueFrom(this.onboardingTasks$)) as Tasks;
const newlyCompletedOrgTasks = Object.fromEntries(
Object.entries(orgTasks).filter(([_k, v]) => v === true),
);
@@ -196,12 +201,12 @@ export class OverviewComponent implements OnInit, OnDestroy {
...prevTasks[organizationId],
...newlyCompletedOrgTasks,
};
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.stateService.setSMOnboardingTasks({
await this.smOnboardingTasksService.setSmOnboardingTasks({
...prevTasks,
[organizationId]: nextOrgTasks,
});
return nextOrgTasks as OrganizationTasks;
}

View File

@@ -0,0 +1,34 @@
import { Injectable } from "@angular/core";
import { Observable, map } from "rxjs";
import {
ActiveUserState,
KeyDefinition,
SM_ONBOARDING_DISK,
StateProvider,
} from "@bitwarden/common/platform/state";
export type SMOnboardingTasks = Record<string, Record<string, boolean>>;
const SM_ONBOARDING_TASKS_KEY = new KeyDefinition<SMOnboardingTasks>(SM_ONBOARDING_DISK, "tasks", {
deserializer: (b) => b,
});
@Injectable({
providedIn: "root",
})
export class SMOnboardingTasksService {
private smOnboardingTasks: ActiveUserState<SMOnboardingTasks>;
smOnboardingTasks$: Observable<SMOnboardingTasks>;
constructor(private stateProvider: StateProvider) {
this.smOnboardingTasks = this.stateProvider.getActive(SM_ONBOARDING_TASKS_KEY);
this.smOnboardingTasks$ = this.smOnboardingTasks.state$.pipe(map((tasks) => tasks ?? {}));
}
async setSmOnboardingTasks(newState: SMOnboardingTasks): Promise<void> {
await this.smOnboardingTasks.update(() => {
return { ...newState };
});
}
}