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.

1.9 KB
Raw Blame
import { useStore } from '@nanostores/react';
import type { HandleType } from '@xyflow/react';
import { useAppSelector } from 'app/store/storeHooks';
import {
  $edgePendingUpdate,
  $isConnectionInProgress,
  $pendingConnection,
  $templates,
} from 'features/nodes/store/nodesSlice';
import { makeConnectionErrorSelector } from 'features/nodes/store/util/makeConnectionErrorSelector';
import { useMemo } from 'react';

export const useConnectionErrorTKey = (nodeId: string, fieldName: string, handleType: HandleType): string | null => {
  const pendingConnection = useStore($pendingConnection);
  const templates = useStore($templates);
  const edgePendingUpdate = useStore($edgePendingUpdate);

  const selectConnectionError = useMemo(
    () => makeConnectionErrorSelector(templates, nodeId, fieldName, handleType, pendingConnection, edgePendingUpdate),
    [templates, nodeId, fieldName, handleType, pendingConnection, edgePendingUpdate]
  );

  const connectionError = useAppSelector(selectConnectionError);
  return connectionError;
};

export const useIsConnectionStartField = (nodeId: string, fieldName: string, handleType: HandleType) => {
  const pendingConnection = useStore($pendingConnection);

  const isConnectionStartField = useMemo(() => {
    if (!pendingConnection) {
      return false;
    }
    if (pendingConnection.nodeId !== nodeId || pendingConnection.handleId !== fieldName) {
      return false;
    }
    if (handleType === 'source' && pendingConnection.fieldTemplate.fieldKind === 'output') {
      return true;
    }
    if (handleType === 'target' && pendingConnection.fieldTemplate.fieldKind === 'input') {
      return true;
    }
    return false;
  }, [fieldName, handleType, nodeId, pendingConnection]);

  return isConnectionStartField;
};

export const useIsConnectionInProgress = () => {
  const isConnectionInProgress = useStore($isConnectionInProgress);

  return isConnectionInProgress;
};