Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.2 KB
Raw Blame
import type { WorkflowV3 } from 'features/nodes/types/workflow';
import { useValidateAndLoadWorkflow } from 'features/workflowLibrary/hooks/useValidateAndLoadWorkflow';
import { useCallback } from 'react';

/**
 * Loads a workflow from an object.
 *
 * You probably should instead use `useLoadWorkflowWithDialog`, which opens a dialog to prevent loss of unsaved changes
 * and handles the loading process.
 */
export const useLoadWorkflowFromObject = () => {
  const validateAndLoadWorkflow = useValidateAndLoadWorkflow();
  const loadWorkflowFromObject = useCallback(
    async (
      unvalidatedWorkflow: unknown,
      options: {
        onSuccess?: (workflow: WorkflowV3) => void;
        onError?: () => void;
        onCompleted?: () => void;
      } = {}
    ) => {
      const { onSuccess, onError, onCompleted } = options;
      try {
        const validatedWorkflow = await validateAndLoadWorkflow(unvalidatedWorkflow, 'object');

        if (!validatedWorkflow) {
          onError?.();
          return;
        }
        onSuccess?.(validatedWorkflow);
      } finally {
        onCompleted?.();
      }
    },
    [validateAndLoadWorkflow]
  );

  return loadWorkflowFromObject;
};