2023-05-16 02:43:46 -07:00
|
|
|
import { defineComponent } from 'vue';
|
|
|
|
import { mapStores } from 'pinia';
|
|
|
|
|
2022-11-23 04:41:53 -08:00
|
|
|
import { externalHooks } from '@/mixins/externalHooks';
|
|
|
|
import { workflowHelpers } from '@/mixins/workflowHelpers';
|
2023-05-15 09:41:13 -07:00
|
|
|
import { useToast } from '@/composables';
|
2022-06-20 12:39:24 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
LOCAL_STORAGE_ACTIVATION_FLAG,
|
|
|
|
PLACEHOLDER_EMPTY_WORKFLOW_ID,
|
|
|
|
WORKFLOW_ACTIVE_MODAL_KEY,
|
|
|
|
} from '@/constants';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { useUIStore } from '@/stores/ui.store';
|
|
|
|
import { useSettingsStore } from '@/stores/settings.store';
|
|
|
|
import { useWorkflowsStore } from '@/stores/workflows.store';
|
2022-06-20 12:39:24 -07:00
|
|
|
|
2023-05-15 09:41:13 -07:00
|
|
|
export const workflowActivate = defineComponent({
|
|
|
|
mixins: [externalHooks, workflowHelpers],
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
...useToast(),
|
|
|
|
};
|
|
|
|
},
|
2022-06-20 12:39:24 -07:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
updatingWorkflowActivation: false,
|
|
|
|
};
|
2022-12-14 01:04:10 -08:00
|
|
|
},
|
2022-11-04 06:04:31 -07:00
|
|
|
computed: {
|
|
|
|
...mapStores(useSettingsStore, useUIStore, useWorkflowsStore),
|
2022-12-14 01:04:10 -08:00
|
|
|
},
|
2022-06-20 12:39:24 -07:00
|
|
|
methods: {
|
|
|
|
async activateCurrentWorkflow(telemetrySource?: string) {
|
2022-11-04 06:04:31 -07:00
|
|
|
const workflowId = this.workflowsStore.workflowId;
|
2022-06-20 12:39:24 -07:00
|
|
|
return this.updateWorkflowActivation(workflowId, true, telemetrySource);
|
|
|
|
},
|
|
|
|
async updateWorkflowActivation(
|
|
|
|
workflowId: string | undefined,
|
|
|
|
newActiveState: boolean,
|
|
|
|
telemetrySource?: string,
|
|
|
|
) {
|
|
|
|
this.updatingWorkflowActivation = true;
|
2023-10-27 05:15:02 -07:00
|
|
|
const nodesIssuesExist = this.workflowsStore.nodesIssuesExist;
|
2022-06-20 12:39:24 -07:00
|
|
|
|
|
|
|
let currWorkflowId: string | undefined = workflowId;
|
|
|
|
if (!currWorkflowId || currWorkflowId === PLACEHOLDER_EMPTY_WORKFLOW_ID) {
|
|
|
|
const saved = await this.saveCurrentWorkflow();
|
|
|
|
if (!saved) {
|
|
|
|
this.updatingWorkflowActivation = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-27 05:15:02 -07:00
|
|
|
currWorkflowId = this.workflowsStore.workflowId;
|
2022-12-14 01:04:10 -08:00
|
|
|
}
|
2022-11-04 06:04:31 -07:00
|
|
|
const isCurrentWorkflow = currWorkflowId === this.workflowsStore.workflowId;
|
2022-06-20 12:39:24 -07:00
|
|
|
|
2022-08-19 06:35:39 -07:00
|
|
|
const activeWorkflows = this.workflowsStore.activeWorkflows;
|
|
|
|
const isWorkflowActive = activeWorkflows.includes(currWorkflowId);
|
2022-06-20 12:39:24 -07:00
|
|
|
|
|
|
|
const telemetryPayload = {
|
|
|
|
workflow_id: currWorkflowId,
|
|
|
|
is_active: newActiveState,
|
|
|
|
previous_status: isWorkflowActive,
|
|
|
|
ndv_input: telemetrySource === 'ndv',
|
2022-12-14 01:04:10 -08:00
|
|
|
};
|
2022-06-20 12:39:24 -07:00
|
|
|
this.$telemetry.track('User set workflow active status', telemetryPayload);
|
2023-05-15 09:41:13 -07:00
|
|
|
void this.$externalHooks().run('workflowActivate.updateWorkflowActivation', telemetryPayload);
|
2022-06-20 12:39:24 -07:00
|
|
|
|
2022-12-14 01:04:10 -08:00
|
|
|
try {
|
2022-06-20 12:39:24 -07:00
|
|
|
if (isWorkflowActive && newActiveState) {
|
2023-05-15 09:41:13 -07:00
|
|
|
this.showMessage({
|
2022-06-20 12:39:24 -07:00
|
|
|
title: this.$locale.baseText('workflowActivator.workflowIsActive'),
|
|
|
|
type: 'success',
|
2022-12-14 01:04:10 -08:00
|
|
|
});
|
2022-06-20 12:39:24 -07:00
|
|
|
this.updatingWorkflowActivation = false;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-27 05:15:02 -07:00
|
|
|
if (isCurrentWorkflow && nodesIssuesExist && newActiveState) {
|
2023-05-15 09:41:13 -07:00
|
|
|
this.showMessage({
|
2022-06-20 12:39:24 -07:00
|
|
|
title: this.$locale.baseText(
|
|
|
|
'workflowActivator.showMessage.activeChangedNodesIssuesExistTrue.title',
|
2022-12-14 01:04:10 -08:00
|
|
|
),
|
2022-06-20 12:39:24 -07:00
|
|
|
message: this.$locale.baseText(
|
|
|
|
'workflowActivator.showMessage.activeChangedNodesIssuesExistTrue.message',
|
2022-12-14 01:04:10 -08:00
|
|
|
),
|
2022-06-20 12:39:24 -07:00
|
|
|
type: 'error',
|
|
|
|
});
|
|
|
|
|
|
|
|
this.updatingWorkflowActivation = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-26 00:25:01 -07:00
|
|
|
await this.updateWorkflow(
|
|
|
|
{ workflowId: currWorkflowId, active: newActiveState },
|
|
|
|
!this.uiStore.stateIsDirty,
|
|
|
|
);
|
2022-06-20 12:39:24 -07:00
|
|
|
} catch (error) {
|
2023-10-27 05:15:02 -07:00
|
|
|
const newStateName = newActiveState ? 'activated' : 'deactivated';
|
2023-05-15 09:41:13 -07:00
|
|
|
this.showError(
|
2022-06-20 12:39:24 -07:00
|
|
|
error,
|
|
|
|
this.$locale.baseText('workflowActivator.showError.title', {
|
|
|
|
interpolate: { newStateName },
|
|
|
|
}) + ':',
|
|
|
|
);
|
|
|
|
this.updatingWorkflowActivation = false;
|
2022-12-14 01:04:10 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-20 12:39:24 -07:00
|
|
|
const activationEventName = isCurrentWorkflow
|
|
|
|
? 'workflow.activeChangeCurrent'
|
|
|
|
: 'workflow.activeChange';
|
2023-05-15 09:41:13 -07:00
|
|
|
void this.$externalHooks().run(activationEventName, {
|
2022-06-20 12:39:24 -07:00
|
|
|
workflowId: currWorkflowId,
|
|
|
|
active: newActiveState,
|
2022-12-14 01:04:10 -08:00
|
|
|
});
|
|
|
|
|
2022-06-20 12:39:24 -07:00
|
|
|
this.$emit('workflowActiveChanged', { id: currWorkflowId, active: newActiveState });
|
|
|
|
this.updatingWorkflowActivation = false;
|
|
|
|
|
|
|
|
if (isCurrentWorkflow) {
|
|
|
|
if (
|
|
|
|
newActiveState &&
|
|
|
|
window.localStorage.getItem(LOCAL_STORAGE_ACTIVATION_FLAG) !== 'true'
|
|
|
|
) {
|
2022-11-04 06:04:31 -07:00
|
|
|
this.uiStore.openModal(WORKFLOW_ACTIVE_MODAL_KEY);
|
2022-06-20 12:39:24 -07:00
|
|
|
} else {
|
2023-05-10 08:10:03 -07:00
|
|
|
await this.settingsStore.fetchPromptsData();
|
2022-06-20 12:39:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-12-14 01:04:10 -08:00
|
|
|
},
|
2022-06-20 12:39:24 -07:00
|
|
|
});
|