Skip to content

Commit

Permalink
remove SwitcherAccount class
Browse files Browse the repository at this point in the history
  • Loading branch information
rr-bw committed Oct 10, 2023
1 parent 75a508e commit 2e16845
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
22 changes: 11 additions & 11 deletions apps/desktop/src/app/layout/account-switcher.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
></app-avatar>
<div class="active-account">
<div>{{ activeAccount.email }}</div>
<span>{{ activeAccount.serverUrl }}</span>
<span>{{ activeAccount.server }}</span>
</div>
</ng-container>
<ng-template #noActiveAccount>
Expand Down Expand Up @@ -61,35 +61,35 @@
*ngFor="let a of accounts | keyvalue"
class="account"
(click)="switch(a.key)"
appA11yTitle="{{ 'loggedInAsOn' | i18n : a.value.profile.email : a.value.serverUrl }}"
appA11yTitle="{{ 'loggedInAsOn' | i18n : a.value.email : a.value.server }}"
attr.aria-label="{{ 'switchAccount' | i18n }}"
>
<app-avatar
[text]="a.value.profile.name ?? a.value.profile.email"
[id]="a.value.profile.userId"
[text]="a.value.name ?? a.value.email"
[id]="a.value.id"
[size]="25"
[circle]="true"
[fontSize]="14"
[dynamic]="true"
[color]="a.value.avatarColor"
*ngIf="a.value.profile.email != null"
*ngIf="a.value.email != null"
aria-hidden="true"
></app-avatar>
<div class="accountInfo">
<span class="email" aria-hidden="true">{{ a.value.profile.email }}</span>
<span class="server" aria-hidden="true">{{ a.value.serverUrl }}</span>
<span class="status" aria-hidden="true">{{
<span class="email" aria-hidden="true">{{ a.value.email }}</span>
<span class="server" aria-hidden="true">{{ a.value.server }}</span>
<!-- <span class="status" aria-hidden="true">{{
(a.value.profile.authenticationStatus === authStatus.Unlocked ? "unlocked" : "locked")
| i18n
}}</span>
}}</span> -->
</div>
<i
<!-- <i
class="bwi bwi-2x text-muted"
[ngClass]="
a.value.profile.authenticationStatus == authStatus.Unlocked ? 'bwi-unlock' : 'bwi-lock'
"
aria-hidden="true"
></i>
></i> -->
</button>
</div>
<ng-container *ngIf="activeAccount?.email != null">
Expand Down
88 changes: 57 additions & 31 deletions apps/desktop/src/app/layout/account-switcher.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { concatMap, Subject, takeUntil } from "rxjs";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
import { TokenService } from "@bitwarden/common/auth/abstractions/token.service";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { EnvironmentUrls } from "@bitwarden/common/auth/models/domain/environment-urls";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
Expand All @@ -18,25 +19,34 @@ type ActiveAccount = {
name: string;
email: string;
avatarColor: string;
serverUrl: string;
server: string;
};

export class SwitcherAccount extends Account {
get serverUrl() {
return this.removeWebProtocolFromString(
this.settings?.environmentUrls?.base ??
this.settings?.environmentUrls.api ??
this.settings.region
);
}
type SwitcherAccount = {
id: string;
name: string;
email: string;
avatarColor?: string;
server?: string;
environmentUrls?: EnvironmentUrls;
};

avatarColor: string;
// export class SwitcherAccount extends Account {
// get serverUrl() {
// return this.removeWebProtocolFromString(
// this.settings?.environmentUrls?.base ??
// this.settings?.environmentUrls.api ??
// this.settings.region
// );
// }

private removeWebProtocolFromString(urlString: string) {
const regex = /http(s)?(:)?(\/\/)?|(\/\/)?(www\.)?/g;
return urlString.replace(regex, "");
}
}
// avatarColor: string;

// private removeWebProtocolFromString(urlString: string) {
// const regex = /http(s)?(:)?(\/\/)?|(\/\/)?(www\.)?/g;
// return urlString.replace(regex, "");
// }
// }

@Component({
selector: "app-account-switcher",
Expand Down Expand Up @@ -120,13 +130,16 @@ export class AccountSwitcherComponent implements OnInit, OnDestroy {
name: (await this.tokenService.getName()) ?? (await this.tokenService.getEmail()),
email: await this.tokenService.getEmail(),
avatarColor: await this.stateService.getAvatarColor(),
serverUrl: Utils.removeVaultfromHostname(
server: Utils.removeVaultfromHostname(
Utils.getHostname(this.environmentService.getWebVaultUrl())
),
};
} catch {
this.activeAccount = undefined;
}

// console.log("this.accounts");
// console.log(this.accounts);
}),
takeUntil(this.destroy$)
)
Expand Down Expand Up @@ -169,22 +182,35 @@ export class AccountSwitcherComponent implements OnInit, OnDestroy {
continue;
}

switcherAccounts[userId] = {
id: userId,
avatarColor: await this.stateService.getAvatarColor({ userId: userId }),
name: baseAccounts[userId].profile.name,
email: baseAccounts[userId].profile.email,
server: (await this.stateService.getServerConfig({ userId: userId })).environment.vault,
/**
* environmentUrls are stored on disk and must be retrieved separately from the
* in memory state offered from subscribing to accounts
*/
environmentUrls: await this.stateService.getEnvironmentUrls({ userId: userId }),
};

// environmentUrls are stored on disk and must be retrieved separately from the in memory state offered from subscribing to accounts
baseAccounts[userId].settings.environmentUrls = await this.stateService.getEnvironmentUrls({
userId: userId,
});

const serverConfig = await this.stateService.getServerConfig({
userId: userId,
});

baseAccounts[userId].settings.region = Utils.removeVaultfromHostname(
Utils.getHostname(serverConfig.environment.vault)
);
switcherAccounts[userId] = new SwitcherAccount(baseAccounts[userId]);
switcherAccounts[userId].avatarColor = await this.stateService.getAvatarColor({
userId: userId,
});
// baseAccounts[userId].settings.environmentUrls = await this.stateService.getEnvironmentUrls({
// userId: userId,
// });

// const serverConfig = await this.stateService.getServerConfig({
// userId: userId,
// });

// baseAccounts[userId].settings.region = Utils.removeVaultfromHostname(
// Utils.getHostname(serverConfig.environment.vault)
// );
// switcherAccounts[userId] = new SwitcherAccount(baseAccounts[userId]);
// switcherAccounts[userId].avatarColor = await this.stateService.getAvatarColor({
// userId: userId,
// });
}

return switcherAccounts;
Expand Down

0 comments on commit 2e16845

Please sign in to comment.