blob: 7ba3cbc43c23579005a6d88d17e992e4a45705bd [file] [log] [blame]
import {isOptionEnabled} from '../../../common/optionsUtils.js';
import WorkflowsStorage from '../../../workflows/workflowsStorage.js';
import {addElementToThreadListActions, shouldAddBtnToActionBar} from '../utils/common.js';
const wfDebugId = 'twpt-workflows';
export default class Workflows {
constructor() {
this.menu = null;
this.workflows = null;
// Always keep the workflows list updated
WorkflowsStorage.watch(workflows => {
this.workflows = workflows;
this._emitWorkflowsUpdateEvent();
}, /* asProtobuf = */ false);
// Open the workflow manager when instructed to do so.
document.addEventListener('twpt-open-workflow-manager', () => {
chrome.runtime.sendMessage({
message: 'openWorkflowsManager',
});
});
}
_emitWorkflowsUpdateEvent() {
if (!this.menu) return;
const e = new CustomEvent('twpt-workflows-update', {
detail: {
workflows: this.workflows,
}
});
this.menu?.dispatchEvent?.(e);
}
addThreadListBtnIfEnabled(readToggle) {
isOptionEnabled('workflows').then(isEnabled => {
if (isEnabled) {
this.menu = document.createElement('twpt-workflows-inject');
this.menu.setAttribute('debugid', wfDebugId);
this._emitWorkflowsUpdateEvent();
addElementToThreadListActions(readToggle, this.menu);
}
});
}
shouldAddThreadListBtn(node) {
return shouldAddBtnToActionBar(wfDebugId, node);
}
};