All files / web/src/composables/shared useEnsureWorkflowSaved.ts

0% Statements 0/27
0% Branches 0/1
0% Functions 0/1
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                           
import { ElMessage } from 'element-plus'
import { useWorkflowStore } from '@/stores/workflowStore'
import { useWorkflowPersistence } from '../persistence/useWorkflowPersistence'
import { ERROR_MESSAGES } from '@/constants'
 
/**
 * Composable to ensure a workflow is saved before performing operations
 * Useful for operations that require a workflow ID (test, execute, etc.)
 */
export function useEnsureWorkflowSaved() {
  const workflowStore = useWorkflowStore()
  const { saveWorkflow } = useWorkflowPersistence()
 
  /**
   * Ensure the current workflow is saved
   * If not saved, attempts to save it first
   *
   * @param options - Configuration options
   * @param options.showMessage - Whether to show success/error messages
   * @returns Result with success flag and workflow ID
   */
  const ensureSaved = async (options?: { showMessage?: boolean }) => {
    if (workflowStore.currentWorkflowId) {
      return { success: true, id: workflowStore.currentWorkflowId }
    }
 
    const result = await saveWorkflow(
      workflowStore.nodes,
      workflowStore.edges,
      {
        showMessage: options?.showMessage ?? false,
        meta: { name: workflowStore.currentWorkflowName || 'Untitled Workflow' },
      }
    )
 
    if (!result.success) {
      ElMessage.error(ERROR_MESSAGES.FAILED_TO_SAVE('workflow'))
      return { success: false, id: null }
    }
 
    return { success: true, id: result.id || null }
  }
 
  return { ensureSaved }
}