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.

4.2 KB
Raw Blame
/* eslint-disable react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-function-as-prop */
import type { ModelConfig } from '@features/models/core/types';

import { Input, Portal, Stack, Text } from '@chakra-ui/react';
import { modelPathSchema } from '@features/models/core/schemas';
import { updateModel } from '@features/models/data/api';
import { refreshModels, replaceModelInStore } from '@features/models/data/modelsStore';
import { useNotify } from '@features/models/ui/useModelsNotify';
import { useScopedAction } from '@platform/react/useScopedAction';
import { assertAccountScopeCurrent } from '@platform/state/accountLifecycle';
import { Button, CloseButton, Field } from '@platform/ui';
import { Dialog } from '@platform/ui/Dialog';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

/** Update only the record's absolute path; never move files or repoint managed relative paths. */
export const UpdatePathDialog = ({
  isOpen,
  model,
  onClose,
  onExitComplete,
}: {
  isOpen: boolean;
  model: Pick<ModelConfig, 'key' | 'name' | 'path'>;
  onClose: () => void;
  /** After the close animation; the host releases the retained dialog here. */
  onExitComplete: () => void;
}) => {
  const { t } = useTranslation();
  const notify = useNotify();
  const [path, setPath] = useState(model.path);
  const [validationError, setValidationError] = useState<string | null>(null);
  const { isBusy, run } = useScopedAction();

  const handleSave = async () => {
    const parsed = modelPathSchema.safeParse(path);

    if (!parsed.success) {
      setValidationError(parsed.error.issues[0]?.message ?? t('models.invalidPathFormat'));

      return;
    }

    await run(
      async (owner) => {
        const updated = await updateModel(model.key, { path: parsed.data }, owner.signal);

        assertAccountScopeCurrent(owner);
        replaceModelInStore(updated);
        // The new path may or may not hold the files: the library checks again.
        void refreshModels();
        notify.success(t('models.pathUpdated'), model.name);
        onClose();
      },
      (message) => notify.error(t('models.pathUpdateFailed'), message)
    );
  };

  return (
    <Dialog.Root
      open={isOpen}
      size="md"
      onExitComplete={onExitComplete}
      onOpenChange={(event) => {
        if (!event.open) {
          onClose();
        }
      }}
    >
      <Portal>
        <Dialog.Backdrop />
        <Dialog.Positioner>
          <Dialog.Content>
            <Dialog.Header borderBottomWidth="1px" borderColor="border.subtle">
              <Stack gap="0.5">
                <Dialog.Title>{t('models.updatePath')}</Dialog.Title>
                <Dialog.Description>{t('models.updatePathDescription')}</Dialog.Description>
              </Stack>
            </Dialog.Header>
            <Dialog.Body>
              <Stack gap="3">
                <Field label={t('models.currentPath')}>
                  <Text color="fg.muted" fontSize="xs" overflowWrap="anywhere">
                    {model.path}
                  </Text>
                </Field>
                <Field error={validationError} label={t('models.newPath')}>
                  <Input
                    aria-invalid={validationError ? true : undefined}
                    placeholder={t('models.newPathPlaceholder')}
                    size="lg"
                    value={path}
                    onChange={(event) => {
                      setPath(event.currentTarget.value);
                      setValidationError(null);
                    }}
                  />
                </Field>
              </Stack>
            </Dialog.Body>
            <Dialog.Footer>
              <Button disabled={isBusy} variant="ghost" onClick={onClose}>
                {t('common.cancel')}
              </Button>
              <Button
                disabled={path.trim() === model.path}
                loading={isBusy}
                variant="solid"
                onClick={() => void handleSave()}
              >
                {t('models.updatePath')}
              </Button>
            </Dialog.Footer>
            <Dialog.CloseTrigger asChild>
              <CloseButton />
            </Dialog.CloseTrigger>
          </Dialog.Content>
        </Dialog.Positioner>
      </Portal>
    </Dialog.Root>
  );
};