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.1 KB
Raw Blame
import { useStore } from '@nanostores/react';
import { useInvocationNodeContext } from 'features/nodes/components/flow/nodes/Invocation/context';
import { $nodeErrors } from 'features/nodes/store/util/fieldValidators';
import { computed } from 'nanostores';
import { useMemo } from 'react';

/**
 * A hook that returns a boolean representing whether the field is invalid. A field is invalid if it has any errors.
 * The errors calculation is debounced.
 *
 * @param fieldName The name of the field
 *
 * @returns A boolean representing whether the field is invalid
 */
export const useInputFieldIsInvalid = (fieldName: string) => {
  const ctx = useInvocationNodeContext();
  const $isInvalid = useMemo(
    () =>
      computed($nodeErrors, (nodeErrors) => {
        const thisNodeErrors = nodeErrors[ctx.nodeId];
        if (!thisNodeErrors) {
          return false;
        }
        const isFieldInvalid = thisNodeErrors.some((error) => {
          return error.type === 'field-error' && error.fieldName === fieldName;
        });
        return isFieldInvalid;
      }),
    [ctx, fieldName]
  );
  return useStore($isInvalid);
};