aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl/TabRepositoryImpl.ts
blob: adcaba75fc162c7c3a67187f50fce31aece08f88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import TabRepository, { Tab } from "../TabRepository";

const COMPLETION_ITEM_LIMIT = 10;

export default class TabRepositoryImpl implements TabRepository {
  async queryTabs(query: string, excludePinned: boolean): Promise<Tab[]> {
    const tabs = await browser.tabs.query({ currentWindow: true });
    return tabs
      .filter((t) => {
        return t.url && t.url.toLowerCase().includes(query.toLowerCase()) ||
          t.title && t.title.toLowerCase().includes(query.toLowerCase());
      })
      .filter((t) => {
        return !(excludePinned && t.pinned);
      })
      .filter(item => item.id && item.title && item.url)
      .slice(0, COMPLETION_ITEM_LIMIT)
      .map(TabRepositoryImpl.toEntity);
  }

  async getAllTabs(excludePinned: boolean): Promise<Tab[]> {
      if (excludePinned) {
          return (await browser.tabs.query({ currentWindow: true, pinned: true }))
              .map(TabRepositoryImpl.toEntity)

      }
      return (await browser.tabs.query({ currentWindow: true }))
          .map(TabRepositoryImpl.toEntity)
  }

  private static toEntity(tab: browser.tabs.Tab,): Tab {
    return {
      id: tab.id!!,
      url: tab.url!!,
      active: tab.active,
      title: tab.title!!,
      faviconUrl: tab.favIconUrl,
      index: tab.index,
    }
  }
}