identities.component.ts raw

   1  import { Component, inject, OnInit } from '@angular/core';
   2  import { Router } from '@angular/router';
   3  import {
   4    IconButtonComponent,
   5    Identity_DECRYPTED,
   6    LoggerService,
   7    NavComponent,
   8    NostrHelper,
   9    ProfileMetadata,
  10    ProfileMetadataService,
  11    StorageService,
  12    ToastComponent,
  13  } from '@common';
  14  
  15  @Component({
  16    selector: 'app-identities',
  17    imports: [IconButtonComponent, ToastComponent],
  18    templateUrl: './identities.component.html',
  19    styleUrl: './identities.component.scss',
  20  })
  21  export class IdentitiesComponent extends NavComponent implements OnInit {
  22    override readonly storage = inject(StorageService);
  23    readonly #router = inject(Router);
  24    readonly #profileMetadata = inject(ProfileMetadataService);
  25    readonly #logger = inject(LoggerService);
  26  
  27    // Cache of pubkey -> profile for quick lookup
  28    #profileCache = new Map<string, ProfileMetadata | null>();
  29  
  30    get isRecklessMode(): boolean {
  31      return this.storage.getSignerMetaHandler().signerMetaData?.recklessMode ?? false;
  32    }
  33  
  34    async ngOnInit() {
  35      await this.#profileMetadata.initialize();
  36      this.#loadProfiles();
  37    }
  38  
  39    #loadProfiles() {
  40      const identities = this.storage.getBrowserSessionHandler().browserSessionData?.identities ?? [];
  41      for (const identity of identities) {
  42        const pubkey = NostrHelper.pubkeyFromPrivkey(identity.privkey);
  43        const profile = this.#profileMetadata.getCachedProfile(pubkey);
  44        this.#profileCache.set(identity.id, profile);
  45      }
  46    }
  47  
  48    getAvatarUrl(identity: Identity_DECRYPTED): string {
  49      const profile = this.#profileCache.get(identity.id);
  50      return profile?.picture || 'person-fill.svg';
  51    }
  52  
  53    getDisplayName(identity: Identity_DECRYPTED): string {
  54      const profile = this.#profileCache.get(identity.id) ?? null;
  55      return this.#profileMetadata.getDisplayName(profile) || identity.nick;
  56    }
  57  
  58    onClickNewIdentity() {
  59      this.#router.navigateByUrl('/new-identity');
  60    }
  61  
  62    onClickEditIdentity(identityId: string, event: MouseEvent) {
  63      event.stopPropagation();
  64      this.#router.navigateByUrl(`/edit-identity/${identityId}/home`);
  65    }
  66  
  67    async onClickSelectIdentity(identityId: string) {
  68      await this.storage.switchIdentity(identityId);
  69    }
  70  
  71    async onToggleRecklessMode() {
  72      const newValue = !this.isRecklessMode;
  73      await this.storage.getSignerMetaHandler().setRecklessMode(newValue);
  74    }
  75  
  76    onClickWhitelistedApps() {
  77      this.#router.navigateByUrl('/whitelisted-apps');
  78    }
  79  
  80    async onClickLock() {
  81      this.#logger.logVaultLock();
  82      await this.storage.lockVault();
  83      this.#router.navigateByUrl('/vault-login');
  84    }
  85  }
  86