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.

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

import { Checkbox, Flex, Portal, Spinner, Stack, Text } from '@chakra-ui/react';
import { deleteOrphanedModels, getOrphanedModels } from '@features/models/data/api';
import { refreshModels } from '@features/models/data/modelsStore';
import { useNotify } from '@features/models/ui/useModelsNotify';
import { formatBytes } from '@platform/i18n/languages';
import { useMountEffect } from '@platform/react/useMountEffect';
import { useScopedAction } from '@platform/react/useScopedAction';
import {
  assertAccountScopeCurrent,
  captureAccountScope,
  isAccountScopeCurrent,
  type AccountScope,
} from '@platform/state/accountLifecycle';
import { getApiErrorMessage } from '@platform/transport/http';
import { Button, CloseButton } from '@platform/ui/Button';
import { Dialog } from '@platform/ui/Dialog';
import { Panel } from '@platform/ui/Panel';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

/** Rescan after partial orphan deletion and keep the dialog open for retrying remaining paths. */
export const OrphanedModelsDialog = ({
  isOpen,
  onClose,
  onExitComplete,
}: {
  isOpen: boolean;
  onClose: () => void;
  /** After the close animation; the host releases the retained dialog here. */
  onExitComplete: () => void;
}) => {
  const { t } = useTranslation();
  const notify = useNotify();
  const [orphans, setOrphans] = useState<OrphanedModelInfo[] | null>(null);
  const [loadError, setLoadError] = useState<string | null>(null);
  // Nothing pre-selected: deleting files on disk must be an explicit choice.
  const [selectedPaths, setSelectedPaths] = useState<ReadonlySet<string>>(new Set());
  const { isBusy: isDeleting, run } = useScopedAction();

  const loadOrphans = async (owner: AccountScope): Promise<void> => {
    try {
      const result = await getOrphanedModels(owner.signal);

      if (isAccountScopeCurrent(owner)) {
        setOrphans(result);
      }
    } catch (error) {
      if (isAccountScopeCurrent(owner)) {
        setLoadError(getApiErrorMessage(error, t('models.failedToScanOrphaned')));
      }
    }
  };

  useMountEffect(() => {
    void loadOrphans(captureAccountScope());
  });

  const togglePath = (path: string) => {
    setSelectedPaths((current) => {
      const next = new Set(current);

      if (next.has(path)) {
        next.delete(path);
      } else {
        next.add(path);
      }

      return next;
    });
  };

  const handleDelete = () =>
    run(
      async (owner) => {
        const result = await deleteOrphanedModels([...selectedPaths], owner.signal);

        assertAccountScopeCurrent(owner);
        const errorCount = Object.keys(result.errors).length;

        void refreshModels(owner);

        if (errorCount > 0) {
          notify.error(
            t('models.orphanedCleanup'),
            t('models.orphanedCleanupPartialDescription', {
              deleted: result.deleted.length,
              error: Object.values(result.errors)[0],
              failed: errorCount,
            })
          );
          const deleted = new Set(result.deleted);

          setSelectedPaths((current) => new Set([...current].filter((path) => !deleted.has(path))));
          setOrphans(null);
          await loadOrphans(owner);

          return;
        }

        notify.success(
          t('models.orphanedCleanup'),
          t('models.orphanedDeletedDescription', { count: result.deleted.length })
        );
        onClose();
      },
      (message) => notify.error(t('models.orphanedCleanupFailed'), message)
    );

  const totalSelectedBytes =
    orphans?.filter((orphan) => selectedPaths.has(orphan.path)).reduce((sum, orphan) => sum + orphan.size_bytes, 0) ??
    0;

  return (
    <Dialog.Root
      open={isOpen}
      scrollBehavior="inside"
      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.orphanedTitle')}</Dialog.Title>
                <Dialog.Description>{t('models.orphanedDescription')}</Dialog.Description>
              </Stack>
            </Dialog.Header>
            <Dialog.Body>
              {loadError ? (
                <Text color="fg.error" fontSize="md" py="4">
                  {loadError}
                </Text>
              ) : orphans === null ? (
                <Flex align="center" justify="center" py="8">
                  <Spinner color="fg.subtle" size="lg" />
                </Flex>
              ) : orphans.length === 0 ? (
                <Text color="fg.muted" fontSize="md" py="4" textAlign="center">
                  {t('models.noOrphaned')}
                </Text>
              ) : (
                <Stack gap="1.5">
                  <Checkbox.Root
                    checked={
                      selectedPaths.size === 0 ? false : selectedPaths.size === orphans.length ? true : 'indeterminate'
                    }
                    colorPalette="accent"
                    ps="2"
                    onCheckedChange={() => {
                      setSelectedPaths(
                        selectedPaths.size === orphans.length
                          ? new Set()
                          : new Set(orphans.map((orphan) => orphan.path))
                      );
                    }}
                  >
                    <Checkbox.HiddenInput />
                    <Checkbox.Control />
                    <Checkbox.Label color="fg.muted" fontSize="xs">
                      {t('models.selectAllCount', { count: orphans.length })}
                    </Checkbox.Label>
                  </Checkbox.Root>
                  {orphans.map((orphan) => (
                    <Panel key={orphan.path} alignItems="center" flexDirection="row" gap="2" p="2" tone="control">
                      <Checkbox.Root
                        checked={selectedPaths.has(orphan.path)}
                        colorPalette="accent"
                        onCheckedChange={() => togglePath(orphan.path)}
                      >
                        <Checkbox.HiddenInput />
                        <Checkbox.Control />
                      </Checkbox.Root>
                      <Stack flex="1" gap="0" minW="0">
                        <Text fontSize="xs" fontWeight="600" overflowWrap="anywhere">
                          {orphan.path}
                        </Text>
                        <Text color="fg.subtle" fontSize="xs">
                          {t('models.fileCount', { count: orphan.files.length })} · {formatBytes(orphan.size_bytes)}
                        </Text>
                      </Stack>
                    </Panel>
                  ))}
                </Stack>
              )}
            </Dialog.Body>
            <Dialog.Footer>
              <Button disabled={isDeleting} variant="ghost" onClick={onClose}>
                {t('common.close')}
              </Button>
              <Button
                colorPalette="red"
                disabled={selectedPaths.size === 0 || !orphans || orphans.length === 0}
                loading={isDeleting}
                variant="solid"
                onClick={() => void handleDelete()}
              >
                {t('models.deleteSelectedWithBytes', { bytes: formatBytes(totalSelectedBytes) })}
              </Button>
            </Dialog.Footer>
            <Dialog.CloseTrigger asChild>
              <CloseButton />
            </Dialog.CloseTrigger>
          </Dialog.Content>
        </Dialog.Positioner>
      </Portal>
    </Dialog.Root>
  );
};