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.5 KB
Raw Blame
import type { CanvasDocumentContractV3, LayerStackKind } from '@workbench/canvas-engine/api';
import type { CanvasEngineHandle } from '@workbench/canvas-operations/react';
import type { LucideIcon } from 'lucide-react';

import { toaster } from '@platform/ui';
import {
  canMergeVisibleRasters,
  compileDocumentNodes,
  getDocumentLeaves,
  isAdjustmentRefusal,
} from '@workbench/canvas-engine/api';
import { useCanvasEngineRead, usePsdExportStatus } from '@workbench/widgets/canvas/engineStoreHooks';
import {
  describeAdjustmentRefusal,
  describeNamedRefusal,
  usePreparedCommit,
} from '@workbench/widgets/canvas/useStructuralCommit';
import { useActiveProjectName } from '@workbench/WorkbenchContext';
import { EyeIcon, EyeOffIcon, FileDownIcon, LayersIcon, PlusIcon, XIcon } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { stackAddItemId } from './addLayerMenu';
import {
  canExportRasterPsd,
  getStackActions,
  isStackAllVisible,
  planStackVisibilityToggle,
  stackVisibilityAxis,
  type StackActionId,
} from './layerStackActions';
import { getPsdExportNotice } from './psdExportNotice';
import { useAddLayer } from './useAddLayer';

export type LayerStackActionsEngine = Pick<CanvasEngineHandle, 'document' | 'exports' | 'interaction' | 'layers'>;

/** One stack action as the header buttons and the stack menu both render it. */
export interface LayerStackAction {
  readonly id: StackActionId;
  readonly label: string;
  readonly icon: LucideIcon;
  readonly disabled: boolean;
  readonly status?: string;
  run(): void;
}

/** The stack's actions with their enablement and handlers, shared by every surface that offers them. */
export const useLayerStackActions = (
  stack: LayerStackKind,
  document: CanvasDocumentContractV3,
  engine: LayerStackActionsEngine | null,
  editingLocked: boolean
): readonly LayerStackAction[] => {
  const commitPrepared = usePreparedCommit(engine);
  const { t } = useTranslation();
  const addLayer = useAddLayer();
  const projectName = useActiveProjectName();
  const axis = stackVisibilityAxis(stack);
  const { stacks } = document;
  // Read from the forests, so selection and other document-level changes cost nothing here.
  const { allVisible, exportable, nodes } = useMemo(() => {
    const own = compileDocumentNodes({ stacks }).filter((node) => node.stack === stack);
    return {
      allVisible: isStackAllVisible(own, axis),
      exportable: stack === 'raster' && canExportRasterPsd(getDocumentLeaves({ stacks })),
      nodes: own,
    };
  }, [axis, stack, stacks]);
  const canMerge = useCanvasEngineRead(
    engine,
    () =>
      !editingLocked &&
      !!engine &&
      stack === 'raster' &&
      canMergeVisibleRasters(engine.document.model()?.compileLeaves() ?? [], engine.exports.hasExportableLayerContent)
  );
  const canExport = !!engine && exportable;
  const exportStatus = usePsdExportStatus(engine);
  const exporting = exportStatus !== 'idle';

  return useMemo(() => {
    const actions: Record<StackActionId, LayerStackAction> = {
      cancelExportPsd: {
        disabled: exportStatus === 'downloading' || exportStatus === 'cancelling',
        icon: XIcon,
        id: 'cancelExportPsd',
        label: t('widgets.layers.groupActions.cancelExportPsd'),
        run: () => engine?.exports.cancelPsdExport(),
      },
      exportPsd: {
        disabled: editingLocked || !canExport || exporting,
        icon: FileDownIcon,
        id: 'exportPsd',
        label: t('widgets.layers.groupActions.exportPsd'),
        status: exporting ? t(`widgets.layers.groupActions.psdExportStatus.${exportStatus}`) : undefined,
        run: () => {
          if (!engine || exporting) {
            return;
          }
          void engine.exports
            .exportRasterLayersToPsd(projectName)
            .then((result) => {
              if (isAdjustmentRefusal(result)) {
                toaster.create({
                  description: describeAdjustmentRefusal(result, t),
                  title: t('widgets.layers.groupActions.exportFailed'),
                  type: 'warning',
                });
                return;
              }
              const notice = getPsdExportNotice(result);
              if (notice) {
                toaster.create({ title: t(notice.key, notice.options), type: 'warning' });
              }
            })
            .catch(() => {
              toaster.create({ title: t('widgets.layers.groupActions.exportFailed'), type: 'error' });
            });
        },
      },
      mergeVisible: {
        disabled: !canMerge,
        icon: LayersIcon,
        id: 'mergeVisible',
        label: t('widgets.layers.groupActions.mergeVisible'),
        run: () => {
          if (!engine) {
            return;
          }
          void engine.layers.mergeVisibleRasterLayers().then((result) => {
            if (typeof result === 'object') {
              toaster.create({
                description: describeNamedRefusal(result, t),
                title: t('widgets.layers.actions.actionFailed'),
                type: isAdjustmentRefusal(result) ? 'warning' : 'error',
              });
            } else if (result === 'not-ready') {
              toaster.create({ title: t('widgets.layers.groupActions.mergeNotReady'), type: 'warning' });
            } else if (result === 'over-budget') {
              toaster.create({ title: t('widgets.layers.groupActions.mergeOverBudget'), type: 'warning' });
            } else if (result === 'failed') {
              toaster.create({ title: t('widgets.layers.actions.operationFailed'), type: 'error' });
            }
          });
        },
      },
      new: {
        disabled: editingLocked,
        icon: PlusIcon,
        id: 'new',
        label: t('widgets.layers.groupActions.new'),
        run: () => addLayer(stackAddItemId(stack)),
      },
      toggleVisibility: {
        disabled: editingLocked,
        icon: allVisible ? EyeIcon : EyeOffIcon,
        id: 'toggleVisibility',
        // Overlay stacks hide on the canvas only (the stack still shapes generation), and say so.
        label: t(
          axis === 'hidden'
            ? allVisible
              ? 'widgets.layers.groupActions.hideAllOnCanvas'
              : 'widgets.layers.groupActions.showAllOnCanvas'
            : allVisible
              ? 'widgets.layers.groupActions.hideAll'
              : 'widgets.layers.groupActions.showAll'
        ),
        run: () => {
          const { ids, nextVisible } = planStackVisibilityToggle(nodes, axis);
          if (ids.length === 0) {
            return;
          }
          if (axis === 'hidden') {
            commitPrepared(
              t(
                nextVisible
                  ? 'widgets.layers.groupActions.showAllOnCanvas'
                  : 'widgets.layers.groupActions.hideAllOnCanvas'
              ),
              (model) =>
                model.prepare({ type: 'set-hidden', updates: ids.map((id) => ({ id, isHidden: !nextVisible })) })
            );
            return;
          }
          commitPrepared(t('widgets.layers.groupActions.toggleVisibility'), (model) =>
            model.prepare({ type: 'set-enabled', updates: ids.map((id) => ({ id, isEnabled: nextVisible })) })
          );
        },
      },
    };
    return getStackActions(stack).flatMap((id) =>
      id === 'exportPsd' && exporting ? [actions[id], actions.cancelExportPsd] : [actions[id]]
    );
  }, [
    addLayer,
    allVisible,
    axis,
    canExport,
    canMerge,
    commitPrepared,
    editingLocked,
    engine,
    exporting,
    exportStatus,
    nodes,
    projectName,
    stack,
    t,
  ]);
};