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.

5.0 KB
Raw Blame
/* eslint-disable react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-object-as-prop */
import { Alert, Box, HStack, Icon, Input, Stack, Text } from '@chakra-ui/react';
import { validateInstallSource } from '@features/nodes/core/installSource';
import { useCustomNodesSelector } from '@features/nodes/data/nodesStore';
import { updateNodesUi, useNodesUiSelector } from '@features/nodes/ui/nodesUiStore';
import { useNodePackInstall } from '@features/nodes/ui/shared/useNodePackInstall';
import { useNotify } from '@features/nodes/ui/useNodesNotify';
import { canCopyText, copyText } from '@platform/browser/clipboard';
import { Button, Field, IconButton, Scrollable } from '@platform/ui';
import { ClipboardCopyIcon, FolderOpenIcon } from 'lucide-react';
import { useMemo, type ChangeEvent, type KeyboardEvent } from 'react';
import { useTranslation } from 'react-i18next';

/** Typed source and busy state outlive tab remounts (shared install activity); the source is validated before POST. */
export const AddNodesView = () => {
  const { t } = useTranslation();
  const notify = useNotify();
  const source = useNodesUiSelector((snapshot) => snapshot.installSource);
  const customNodesPath = useCustomNodesSelector((snapshot) => snapshot.customNodesPath);
  const nodePacks = useCustomNodesSelector((snapshot) => snapshot.nodePacks);
  const { install, isInstalling } = useNodePackInstall();

  const trimmedSource = source.trim();
  const installedPackNames = useMemo(() => new Set(nodePacks.map((pack) => pack.name)), [nodePacks]);
  const validation = useMemo(
    () => validateInstallSource(trimmedSource, installedPackNames),
    [installedPackNames, trimmedSource]
  );
  const fieldError =
    trimmedSource === '' || validation.issue === null
      ? undefined
      : validation.issue === 'alreadyInstalled'
        ? t('nodes.alreadyInstalledError', { name: validation.packName })
        : t('nodes.invalidSourceName');

  const handleInstall = () => {
    if (validation.issue === null) {
      install(trimmedSource, { onInstalled: () => updateNodesUi({ installSource: '' }) });
    }
  };

  const canCopyPath = canCopyText();
  const handleCopyPath = async (path: string) => {
    try {
      await copyText(path);
      notify.success(t('nodes.pathCopied'));
    } catch {
      notify.error(t('common.couldNotCopy'));
    }
  };

  const handleSourceChange = (event: ChangeEvent<HTMLInputElement>) =>
    updateNodesUi({ installSource: event.currentTarget.value });
  const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
    if (event.key === 'Enter') {
      event.preventDefault();
      handleInstall();
    }
  };

  return (
    <Scrollable h="full" label={t('nodes.addNodes')} minH="0" p="3">
      <Stack gap="4" maxW="44rem">
        <Alert.Root borderRadius="md" status="warning" variant="surface">
          <Alert.Indicator />
          <Alert.Title>{t('nodes.trustWarning')}</Alert.Title>
        </Alert.Root>
        <Field error={fieldError} helpText={t('nodes.gitUrlHelp')} label={t('nodes.gitUrl')}>
          <HStack align="start" gap="2" w="full">
            <Input
              aria-invalid={fieldError ? true : undefined}
              placeholder="https://github.com/owner/invokeai-node-pack.git"
              size="lg"
              value={source}
              onChange={handleSourceChange}
              onKeyDown={handleKeyDown}
            />
            <Button disabled={validation.issue !== null} loading={isInstalling} size="lg" onClick={handleInstall}>
              {t('nodes.install')}
            </Button>
          </HStack>
        </Field>
        <Stack gap="2">
          <HStack gap="1.5">
            <Icon as={FolderOpenIcon} boxSize="3.5" color="fg.muted" />
            <Text color="fg.muted" fontSize="xs" fontWeight="600" textTransform="uppercase">
              {t('nodes.installManually')}
            </Text>
          </HStack>
          <Text color="fg.muted">{t('nodes.scanFolderDescription')}</Text>
          {customNodesPath ? (
            <Box bg="bg.subtle" borderColor="border.subtle" borderWidth="1px" p="3" rounded="md">
              <HStack justify="space-between">
                <Text color="fg.muted" fontSize="xs" fontWeight="600" textTransform="uppercase">
                  {t('nodes.nodesDirectory')}
                </Text>
                <IconButton
                  tooltip={t('nodes.copyPath')}
                  unavailableReason={canCopyPath ? undefined : t('common.copyUnavailable')}
                  aria-label={t('nodes.copyPath')}
                  size="sm"
                  variant="ghost"
                  onClick={() => void handleCopyPath(customNodesPath)}
                >
                  <Icon as={ClipboardCopyIcon} boxSize="3" />
                </IconButton>
              </HStack>
              <Text fontFamily="mono" mt="1" overflowWrap="anywhere">
                {customNodesPath}
              </Text>
            </Box>
          ) : null}
        </Stack>
      </Stack>
    </Scrollable>
  );
};