File size: 1,633 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { useAppDispatch } from 'app/store/storeHooks';
import { workflowLoadRequested } from 'features/nodes/store/actions';
import { toast } from 'features/toast/toast';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useLazyGetImageWorkflowQuery } from 'services/api/endpoints/images';

type UseGetAndLoadEmbeddedWorkflowOptions = {
  onSuccess?: () => void;
  onError?: () => void;
};

export const useGetAndLoadEmbeddedWorkflow = (options?: UseGetAndLoadEmbeddedWorkflowOptions) => {
  const dispatch = useAppDispatch();
  const { t } = useTranslation();
  const [_getAndLoadEmbeddedWorkflow, result] = useLazyGetImageWorkflowQuery();
  const getAndLoadEmbeddedWorkflow = useCallback(
    async (imageName: string) => {
      try {
        const { data } = await _getAndLoadEmbeddedWorkflow(imageName);
        if (data) {
          dispatch(workflowLoadRequested({ data, asCopy: true }));
          // No toast - the listener for this action does that after the workflow is loaded
          options?.onSuccess && options?.onSuccess();
        } else {
          toast({
            id: 'PROBLEM_RETRIEVING_WORKFLOW',
            title: t('toast.problemRetrievingWorkflow'),
            status: 'error',
          });
        }
      } catch {
        toast({
          id: 'PROBLEM_RETRIEVING_WORKFLOW',
          title: t('toast.problemRetrievingWorkflow'),
          status: 'error',
        });
        options?.onError && options?.onError();
      }
    },
    [_getAndLoadEmbeddedWorkflow, dispatch, options, t]
  );

  return [getAndLoadEmbeddedWorkflow, result] as const;
};