blob: 7ba3cbc43c23579005a6d88d17e992e4a45705bd [file] [log] [blame]
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +02001import {isOptionEnabled} from '../../../common/optionsUtils.js';
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +02002import WorkflowsStorage from '../../../workflows/workflowsStorage.js';
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +02003import {addElementToThreadListActions, shouldAddBtnToActionBar} from '../utils/common.js';
4
5const wfDebugId = 'twpt-workflows';
6
7export default class Workflows {
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +02008 constructor() {
9 this.menu = null;
10 this.workflows = null;
11
12 // Always keep the workflows list updated
13 WorkflowsStorage.watch(workflows => {
14 this.workflows = workflows;
15 this._emitWorkflowsUpdateEvent();
16 }, /* asProtobuf = */ false);
17
18 // Open the workflow manager when instructed to do so.
19 document.addEventListener('twpt-open-workflow-manager', () => {
20 chrome.runtime.sendMessage({
21 message: 'openWorkflowsManager',
22 });
23 });
24 }
25
26 _emitWorkflowsUpdateEvent() {
27 if (!this.menu) return;
28 const e = new CustomEvent('twpt-workflows-update', {
29 detail: {
30 workflows: this.workflows,
31 }
32 });
33 this.menu?.dispatchEvent?.(e);
34 }
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020035
36 addThreadListBtnIfEnabled(readToggle) {
37 isOptionEnabled('workflows').then(isEnabled => {
38 if (isEnabled) {
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +020039 this.menu = document.createElement('twpt-workflows-inject');
40 this.menu.setAttribute('debugid', wfDebugId);
41 this._emitWorkflowsUpdateEvent();
42 addElementToThreadListActions(readToggle, this.menu);
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020043 }
44 });
45 }
46
47 shouldAddThreadListBtn(node) {
48 return shouldAddBtnToActionBar(wfDebugId, node);
49 }
50};