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
import type { ArchitectureCapabilitiesSnapshot } from '@features/models/architecturesStore';
import type { CanvasControlLayerContract } from '@workbench/canvas-engine/api';
import type { WorkbenchPreferences } from '@workbench/settings/contracts';

import { Box, Icon, Spinner, VisuallyHidden } from '@chakra-ui/react';
import { useModelsSelector } from '@features/models';
import {
  getArchitectureCapabilitiesSnapshot,
  subscribeArchitectureCapabilities,
} from '@features/models/architecturesStore';
import { useExternalStoreSelector } from '@platform/state/selectors';
import { Tooltip } from '@platform/ui/Tooltip';
import {
  createControlLayerCheckContext,
  describeControlLayer,
  describeControlLayerReason,
} from '@workbench/controlLayerChecks';
import { useWorkbenchPreferenceSelector } from '@workbench/settings/store';
import { useControlDetectionStatus } from '@workbench/widgets/canvas/engineStoreHooks';
import { TriangleAlertIcon } from 'lucide-react';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import { useSelectedMainModel } from './useSelectedMainModel';

const selectCapabilitiesStatus = (snapshot: ArchitectureCapabilitiesSnapshot) => snapshot.status;
const selectControlModelMemory = (preferences: WorkbenchPreferences) => preferences.controlModelMemory;

/** Spinner while detecting, else a warning judged as Properties does; group-gated layers are not validated. */
export const ControlLayerStatusIcon = ({
  layer,
  contributing = layer.isEnabled,
  engine,
}: {
  contributing?: boolean;
  engine: object | null;
  layer: CanvasControlLayerContract;
}) => {
  const { t } = useTranslation();
  const detection = useControlDetectionStatus(engine, layer.id);
  const models = useModelsSelector((snapshot) => snapshot.models);
  const mainModel = useSelectedMainModel();
  const remembered = useWorkbenchPreferenceSelector(selectControlModelMemory);
  const capabilitiesStatus = useExternalStoreSelector(
    subscribeArchitectureCapabilities,
    getArchitectureCapabilitiesSnapshot,
    selectCapabilitiesStatus
  );
  // Read inside the store's selector: whether a kind is supported comes from the capability table, and a call memoised
  // on the layer and model would keep its first answer after the table loads.
  const reason = useExternalStoreSelector(
    subscribeArchitectureCapabilities,
    getArchitectureCapabilitiesSnapshot,
    useCallback(
      () =>
        contributing
          ? describeControlLayer(layer, {
              ...createControlLayerCheckContext(mainModel?.base ?? null, remembered),
              mainModel,
              models,
            }).reason
          : null,
      [contributing, layer, mainModel, models, remembered]
    )
  );

  // A queued run (debounce, a free slot, a commit waiting for the document) is as busy as a running one; it names the
  // detector the layer asks for, since it has not started one yet.
  const detector =
    detection.status === 'processing'
      ? detection.detector
      : detection.status === 'queued'
        ? layer.detection?.type
        : undefined;
  if (detector) {
    const label = t(
      detection.status === 'queued' ? 'widgets.layers.control.detectionQueued' : 'widgets.layers.control.detecting',
      { detector: t(`widgets.layers.control.filters.${detector}`, detector) }
    );
    return (
      <Tooltip content={label}>
        <Box alignItems="center" display="inline-flex" flexShrink={0}>
          <Spinner boxSize="3" color="fg.muted" />
          <VisuallyHidden>{label}</VisuallyHidden>
        </Box>
      </Tooltip>
    );
  }

  // Distinguish loading/failed capabilities from unsupported adapters; failures should point to retry.
  if (!reason || (reason === 'capabilities_unavailable' && capabilitiesStatus !== 'error')) {
    return null;
  }

  const message =
    reason === 'capabilities_unavailable'
      ? `${t('widgets.layers.control.capabilitiesLoadFailed')} ${t('widgets.layers.control.capabilitiesRetryHint')}`
      : describeControlLayerReason(t, reason);

  return (
    <Tooltip content={message}>
      <Icon
        aria-label={`${t('widgets.layers.control.needsAttention')}: ${message}`}
        as={TriangleAlertIcon}
        boxSize="3"
        color="fg.warning"
        flexShrink={0}
      />
    </Tooltip>
  );
};