File size: 2,141 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { ConfirmationAlertDialog, Flex, Text } from '@invoke-ai/ui-library';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { useAssertSingleton } from 'common/hooks/useAssertSingleton';
import { buildUseDisclosure } from 'common/hooks/useBoolean';
import { nodeEditorReset } from 'features/nodes/store/nodesSlice';
import { selectWorkflowIsTouched, workflowModeChanged } from 'features/nodes/store/workflowSlice';
import { toast } from 'features/toast/toast';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';

const [useDialogState] = buildUseDisclosure(false);

export const useNewWorkflow = () => {
  const { t } = useTranslation();
  const dispatch = useAppDispatch();
  const dialog = useDialogState();
  const isTouched = useAppSelector(selectWorkflowIsTouched);

  const createImmediate = useCallback(() => {
    dispatch(nodeEditorReset());
    dispatch(workflowModeChanged('edit'));

    toast({
      id: 'NEW_WORKFLOW_CREATED',
      title: t('workflows.newWorkflowCreated'),
      status: 'success',
    });

    dialog.close();
  }, [dialog, dispatch, t]);

  const createWithDialog = useCallback(() => {
    if (!isTouched) {
      createImmediate();
      return;
    }
    dialog.open();
  }, [dialog, createImmediate, isTouched]);

  return {
    createImmediate,
    createWithDialog,
  } as const;
};

export const NewWorkflowConfirmationAlertDialog = memo(() => {
  useAssertSingleton('NewWorkflowConfirmationAlertDialog');
  const { t } = useTranslation();
  const dialog = useDialogState();
  const newWorkflow = useNewWorkflow();

  return (
    <ConfirmationAlertDialog
      isOpen={dialog.isOpen}
      onClose={dialog.close}
      title={t('nodes.newWorkflow')}
      acceptCallback={newWorkflow.createImmediate}
      useInert={false}
    >
      <Flex flexDir="column" gap={2}>
        <Text>{t('nodes.newWorkflowDesc')}</Text>
        <Text variant="subtext">{t('nodes.newWorkflowDesc2')}</Text>
      </Flex>
    </ConfirmationAlertDialog>
  );
});

NewWorkflowConfirmationAlertDialog.displayName = 'NewWorkflowConfirmationAlertDialog';