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.

2.6 KB
Raw Blame
import type { SendGalleryImageAsControlResult } from './controlSourceActions';
import type { ImportGalleryImagesResult } from './importGalleryImages';

export interface CanvasImportNotice {
  kind: 'error' | 'info' | 'success';
  titleKey:
    | 'widgets.canvas.import.blocked'
    | 'widgets.canvas.import.capabilitiesUnavailable'
    | 'widgets.canvas.import.controlFilled'
    | 'widgets.canvas.import.empty'
    | 'widgets.canvas.import.partial'
    | 'widgets.canvas.import.staleDocument'
    | 'widgets.canvas.import.staleProject'
    | 'widgets.canvas.import.success';
  options?: { count: number } | { count: number; failedCount: number; successCount: number } | { name: string };
}

const assertNever = (value: never): never => {
  throw new Error(`Unhandled canvas import result: ${JSON.stringify(value)}`);
};

export const getCanvasImportNotice = (result: ImportGalleryImagesResult): CanvasImportNotice => {
  switch (result.status) {
    case 'imported':
      if (result.failedImageNames.length > 0) {
        return {
          kind: 'info',
          options: {
            count: result.layerIds.length,
            failedCount: result.failedImageNames.length,
            successCount: result.layerIds.length,
          },
          titleKey: 'widgets.canvas.import.partial',
        };
      }
      return {
        kind: 'success',
        options: { count: result.layerIds.length },
        titleKey: 'widgets.canvas.import.success',
      };
    case 'blocked':
      return { kind: 'info', titleKey: 'widgets.canvas.import.blocked' };
    case 'capabilities-unavailable':
      return { kind: 'info', titleKey: 'widgets.canvas.import.capabilitiesUnavailable' };
    case 'empty':
      return { kind: 'info', titleKey: 'widgets.canvas.import.empty' };
    case 'stale-document':
      return { kind: 'error', titleKey: 'widgets.canvas.import.staleDocument' };
    case 'stale-project':
      return { kind: 'error', titleKey: 'widgets.canvas.import.staleProject' };
    default:
      return assertNever(result);
  }
};

/** A gallery image used as control: the layer it filled is named; a new layer reads as any one-image import. */
export const getSendAsControlNotice = (result: SendGalleryImageAsControlResult): CanvasImportNotice => {
  switch (result.status) {
    case 'filled':
      return { kind: 'success', options: { name: result.layerName }, titleKey: 'widgets.canvas.import.controlFilled' };
    case 'created':
      return getCanvasImportNotice({ failedImageNames: [], layerIds: [result.layerId], status: 'imported' });
    default:
      return getCanvasImportNotice(result);
  }
};