aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/TabCompletionUseCase.ts
blob: d0d3bb7ffc5aebb253ac2a86cc9c59aeab0a793b (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
42
43
44
45
46
47
48
49
50
51
52
53
54
import { inject, injectable } from "tsyringe";
import TabItem from "./TabItem";
import TabRepository, { Tab } from "./TabRepository";
import TabPresenter from "../presenters/TabPresenter";
import TabFlag from "../../shared/TabFlag";

@injectable()
export default class TabCompletionUseCase {
  constructor(
    @inject("TabRepository") private tabRepository: TabRepository,
    @inject("TabPresenter") private tabPresenter: TabPresenter
  ) {}

  async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
    const lastTabId = await this.tabPresenter.getLastSelectedId();
    const allTabs = await this.tabRepository.getAllTabs(excludePinned);
    const num = parseInt(query, 10);
    let tabs: Tab[] = [];
    if (!isNaN(num)) {
      const tab = allTabs.find((t) => t.index === num - 1);
      if (tab) {
        tabs = [tab];
      }
    } else if (query == "%") {
      const tab = allTabs.find((t) => t.active);
      if (tab) {
        tabs = [tab];
      }
    } else if (query == "#") {
      const tab = allTabs.find((t) => t.id === lastTabId);
      if (tab) {
        tabs = [tab];
      }
    } else {
      tabs = await this.tabRepository.queryTabs(query, excludePinned);
    }

    return tabs.map((tab) => {
      let flag = TabFlag.None;
      if (tab.active) {
        flag = TabFlag.CurrentTab;
      } else if (tab.id == lastTabId) {
        flag = TabFlag.LastTab;
      }
      return {
        index: tab.index + 1,
        flag: flag,
        title: tab.title,
        url: tab.url,
        faviconUrl: tab.faviconUrl,
      };
    });
  }
}