aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/TabSelectUseCase.ts
blob: 663ceb86e29e2f31642b5fa02c32292bc2ad52e9 (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
import { inject, injectable } from "tsyringe";
import TabPresenter from "../presenters/TabPresenter";

@injectable()
export default class TabSelectUseCase {
  constructor(@inject("TabPresenter") private tabPresenter: TabPresenter) {}

  async selectPrev(count: number): Promise<any> {
    const tabs = await this.tabPresenter.getAll();
    if (tabs.length < 2) {
      return;
    }
    const tab = tabs.find((t) => t.active);
    if (!tab) {
      return;
    }
    const select = (tab.index - count + tabs.length) % tabs.length;
    return this.tabPresenter.select(tabs[select].id as number);
  }

  async selectNext(count: number): Promise<any> {
    const tabs = await this.tabPresenter.getAll();
    if (tabs.length < 2) {
      return;
    }
    const tab = tabs.find((t) => t.active);
    if (!tab) {
      return;
    }
    const select = (tab.index + count) % tabs.length;
    return this.tabPresenter.select(tabs[select].id as number);
  }

  async selectFirst(): Promise<any> {
    const tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[0].id as number);
  }

  async selectLast(): Promise<any> {
    const tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[tabs.length - 1].id as number);
  }

  async selectPrevSelected(): Promise<any> {
    const tabId = await this.tabPresenter.getLastSelectedId();
    if (tabId === null || typeof tabId === "undefined") {
      return Promise.resolve();
    }
    return this.tabPresenter.select(tabId);
  }
}