bookmarks.component.ts raw

   1  import { Component, inject, OnInit } from '@angular/core';
   2  import { Router } from '@angular/router';
   3  import { Bookmark, LoggerService, NavComponent, SignerMetaData } from '@common';
   4  import { FirefoxMetaHandler } from '../../../common/data/firefox-meta-handler';
   5  import browser from 'webextension-polyfill';
   6  
   7  @Component({
   8    selector: 'app-bookmarks',
   9    templateUrl: './bookmarks.component.html',
  10    styleUrl: './bookmarks.component.scss',
  11    imports: [],
  12  })
  13  export class BookmarksComponent extends NavComponent implements OnInit {
  14    readonly #logger = inject(LoggerService);
  15    readonly #metaHandler = new FirefoxMetaHandler();
  16    readonly #router = inject(Router);
  17  
  18    bookmarks: Bookmark[] = [];
  19    isLoading = true;
  20  
  21    async ngOnInit() {
  22      await this.loadBookmarks();
  23    }
  24  
  25    async loadBookmarks() {
  26      this.isLoading = true;
  27      try {
  28        const metaData = await this.#metaHandler.loadFullData() as SignerMetaData;
  29        this.#metaHandler.setFullData(metaData);
  30        this.bookmarks = this.#metaHandler.getBookmarks();
  31      } catch (error) {
  32        console.error('Failed to load bookmarks:', error);
  33      } finally {
  34        this.isLoading = false;
  35      }
  36    }
  37  
  38    async onBookmarkThisPage() {
  39      try {
  40        // Get the current tab URL and title
  41        const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  42        if (!tab?.url || !tab?.title) {
  43          console.error('Could not get current tab info');
  44          return;
  45        }
  46  
  47        // Check if already bookmarked
  48        if (this.bookmarks.some(b => b.url === tab.url)) {
  49          console.log('Page already bookmarked');
  50          return;
  51        }
  52  
  53        const newBookmark: Bookmark = {
  54          id: crypto.randomUUID(),
  55          url: tab.url,
  56          title: tab.title,
  57          createdAt: Date.now(),
  58        };
  59  
  60        this.bookmarks = [newBookmark, ...this.bookmarks];
  61        await this.saveBookmarks();
  62        this.#logger.logBookmarkAdded(newBookmark.url, newBookmark.title);
  63      } catch (error) {
  64        console.error('Failed to bookmark page:', error);
  65      }
  66    }
  67  
  68    async onRemoveBookmark(bookmark: Bookmark) {
  69      this.bookmarks = this.bookmarks.filter(b => b.id !== bookmark.id);
  70      await this.saveBookmarks();
  71      this.#logger.logBookmarkRemoved(bookmark.url, bookmark.title);
  72    }
  73  
  74    async saveBookmarks() {
  75      try {
  76        await this.#metaHandler.setBookmarks(this.bookmarks);
  77      } catch (error) {
  78        console.error('Failed to save bookmarks:', error);
  79      }
  80    }
  81  
  82    openBookmark(bookmark: Bookmark) {
  83      browser.tabs.create({ url: bookmark.url });
  84    }
  85  
  86    getDomain(url: string): string {
  87      try {
  88        return new URL(url).hostname;
  89      } catch {
  90        return url;
  91      }
  92    }
  93  
  94    async onClickLock() {
  95      this.#logger.logVaultLock();
  96      await this.storage.lockVault();
  97      this.#router.navigateByUrl('/vault-login');
  98    }
  99  }
 100