File size: 3,051 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Button, Checkbox, Flex, FormControl, FormHelperText, FormLabel, Input } from '@invoke-ai/ui-library';
import { useAppDispatch, useAppSelector } from 'app/store/storeHooks';
import { useInstallModel } from 'features/modelManagerV2/hooks/useInstallModel';
import {
  selectShouldInstallInPlace,
  shouldInstallInPlaceChanged,
} from 'features/modelManagerV2/store/modelManagerV2Slice';
import { t } from 'i18next';
import type { ChangeEvent } from 'react';
import { memo, useCallback } from 'react';
import type { SubmitHandler } from 'react-hook-form';
import { useForm } from 'react-hook-form';

type SimpleImportModelConfig = {
  location: string;
};

export const InstallModelForm = memo(() => {
  const inplace = useAppSelector(selectShouldInstallInPlace);
  const dispatch = useAppDispatch();
  const [installModel, { isLoading }] = useInstallModel();

  const { register, handleSubmit, formState, reset } = useForm<SimpleImportModelConfig>({
    defaultValues: {
      location: '',
    },
    mode: 'onChange',
  });

  const resetForm = useCallback(() => reset(undefined, { keepValues: true }), [reset]);

  const onSubmit = useCallback<SubmitHandler<SimpleImportModelConfig>>(
    (values) => {
      if (!values?.location) {
        return;
      }

      installModel({
        source: values.location,
        inplace: inplace,
        onSuccess: resetForm,
        onError: resetForm,
      });
    },
    [installModel, resetForm, inplace]
  );

  const onChangeInplace = useCallback(
    (e: ChangeEvent<HTMLInputElement>) => {
      dispatch(shouldInstallInPlaceChanged(e.target.checked));
    },
    [dispatch]
  );

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <Flex flexDir="column" gap={4}>
        <Flex gap={2} alignItems="flex-end" justifyContent="space-between">
          <FormControl orientation="vertical">
            <FormLabel>{t('modelManager.urlOrLocalPath')}</FormLabel>
            <Flex alignItems="center" gap={3} w="full">
              <Input placeholder={t('modelManager.simpleModelPlaceholder')} {...register('location')} />
              <Button
                onClick={handleSubmit(onSubmit)}
                isDisabled={!formState.dirtyFields.location}
                isLoading={isLoading}
                size="sm"
              >
                {t('modelManager.install')}
              </Button>
            </Flex>
            <FormHelperText>{t('modelManager.urlOrLocalPathHelper')}</FormHelperText>
          </FormControl>
        </Flex>

        <FormControl>
          <Flex flexDir="column" gap={2}>
            <Flex gap={4}>
              <Checkbox isChecked={inplace} onChange={onChangeInplace} />
              <FormLabel>
                {t('modelManager.inplaceInstall')} ({t('modelManager.localOnly')})
              </FormLabel>
            </Flex>
            <FormHelperText>{t('modelManager.inplaceInstallDesc')}</FormHelperText>
          </Flex>
        </FormControl>
      </Flex>
    </form>
  );
});

InstallModelForm.displayName = 'InstallModelForm';