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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
import type { GalleryItem, GalleryItemRef } from '@features/gallery/contracts';
import type { GalleryItemContextMenuRequest, GalleryProjectRef, GalleryUiAdapter } from '@features/gallery/react';
import type { ProjectLibrarySnapshot } from '@workbench/projects/library';
import type { ReactNode } from 'react';
import { getGalleryAutoAddBoardId, getGallerySettings, isLegacyFollowAutoAdd } from '@features/gallery/contracts';
import { GalleryUiProvider } from '@features/gallery/react';
import { useAuthSession } from '@features/identity';
import { useMountEffect } from '@platform/react/useMountEffect';
import { captureAccountScope, isAccountScopeCurrent } from '@platform/state/accountLifecycle';
import { ApiError } from '@platform/transport/http';
import { useWorkbenchFocus } from '@workbench/focusRegions';
import { useProjectLibrarySelector } from '@workbench/projects/library';
import { getProjectSyncSnapshot } from '@workbench/projects/syncStore';
import { useExportLibraryProject } from '@workbench/projects/useProjectFileActions';
import {
areWorkbenchPreferencesReady,
getWorkbenchPreferences,
patchWorkbenchPreferences,
useWorkbenchPreferenceSelector,
} from '@workbench/settings/store';
import { useNotify } from '@workbench/useNotify';
import { useOpenWorkbenchWidget } from '@workbench/useOpenWorkbenchWidget';
import { revealPlacedWidget } from '@workbench/widgetPlacementCommands';
import { useLivePreviewFollow } from '@workbench/widgets/preview/livePreviewFollow';
import { getProjectWidgetInstance, resolveWidgetValues } from '@workbench/widgetState';
import {
useWorkbenchCommands,
useWorkbenchInternalStore,
useWorkbenchPersistenceService,
useWorkbenchQueries,
useWorkbenchSelector,
} from '@workbench/WorkbenchContext';
import { lazy, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { createGalleryItemMenu, GalleryItemMenuSlot } from './galleryItemMenu';
const EMPTY_WIDGET_VALUES: Record<string, unknown> = Object.freeze({});
/** The Gallery's values as it reads them: the project's pins over the user's gallery defaults. */
const getGalleryValues = (
project: Parameters<typeof getProjectWidgetInstance>[0] | null | undefined,
scopedDefaults: Parameters<typeof resolveWidgetValues>[1]
) => resolveWidgetValues(project ? getProjectWidgetInstance(project, 'gallery') : undefined, scopedDefaults);
const selectGalleryProjects = (snapshot: ProjectLibrarySnapshot): GalleryProjectRef[] =>
snapshot.summaries.map((summary) => ({ id: summary.id, name: summary.name }));
const areGalleryProjectsEqual = (left: GalleryProjectRef[], right: GalleryProjectRef[]): boolean =>
left.length === right.length &&
left.every((project, index) => project.id === right[index]?.id && project.name === right[index]?.name);
// Loaded on first reveal; the label cache is not part of the editor's initial graph.
const getItemLabel = (item: GalleryItemRef): Promise<string | null> =>
import('@workbench/image-map/imageLabelCache')
.then(({ getImageLabels }) => getImageLabels(item))
.then((labels) => labels?.label ?? null)
// A chunk that fails to load (stale deploy, dropped network) means no label, not an unhandled rejection.
.catch(() => null);
const GalleryItemActionsAdapter = lazy(() =>
import('./GalleryImageActionsBridge').then((module) => ({ default: module.GalleryItemActionsAdapter }))
);
const GalleryImageContextMenu = lazy(() =>
import('./GalleryImageActionsBridge').then((module) => ({ default: module.GalleryImageContextMenu }))
);
const loadGalleryItemMenu = (ref: GalleryItemRef, item: GalleryItem | undefined, signal: AbortSignal) =>
import('./GalleryImageActionsBridge').then((module) => module.loadGalleryItemMenu(ref, item, signal));
export const GalleryUiAdapterProvider = ({ children }: { children: ReactNode }) => {
const { projectId, projectName, galleryValues, generateValues, liveFollowEnabled } = useWorkbenchSelector(
({ activeProject: project, scopedDefaults }) => ({
projectId: project.id,
projectName: project.name,
galleryValues: getGalleryValues(project, scopedDefaults),
generateValues: getProjectWidgetInstance(project, 'generate')?.state?.values ?? EMPTY_WIDGET_VALUES,
liveFollowEnabled: project.settings.viewerAutoSwitch === 'progress',
})
);
const antialiasProgressImages = useWorkbenchPreferenceSelector((preferences) => preferences.antialiasProgressImages);
const livePreview = useLivePreviewFollow();
const boardSharing = useAuthSession().multiuserEnabled;
const { gallery, notifications, widgets } = useWorkbenchCommands();
const notify = useNotify();
const { i18n, t } = useTranslation();
const queries = useWorkbenchQueries();
const accountScope = captureAccountScope();
const exportProject = useExportLibraryProject();
const openWorkbenchWidget = useOpenWorkbenchWidget();
const { focusFloating, focusRegion } = useWorkbenchFocus();
// Only what the gallery needs to label other projects' boards, compared structurally: every autosave ack and library
// refresh rebuilds the summaries, and the gallery must not re-render for cover or timestamp churn.
const projects = useProjectLibrarySelector(selectGalleryProjects, areGalleryProjectsEqual);
const store = useWorkbenchInternalStore();
const persistence = useWorkbenchPersistenceService();
const [itemMenu] = useState(() => createGalleryItemMenu({ load: loadGalleryItemMenu }));
// Stable, unlike the adapter: media surfaces subscribe to the opener alone.
const [openItemContextMenu] = useState(() => (request: GalleryItemContextMenuRequest) => {
const requestProjectId = queries.getSnapshot().activeProject.id;
itemMenu.open(request, {
isCurrent: () => queries.isActiveProject(requestProjectId),
onFailure: (error) =>
notifications.reportError({
area: 'gallery-item-menu',
message: i18n.t(
error instanceof ApiError && (error.status === 403 || error.status === 404)
? 'widgets.gallery.itemMenu.unavailable'
: 'widgets.gallery.itemMenu.loadFailed'
),
namespace: 'gallery',
}),
});
});
// Preload row dependencies here to avoid a second fetch wave after the gallery mounts.
useMountEffect(() => {
void import('./GalleryImageActionsBridge');
return itemMenu.dispose;
});
const adapter = useMemo<GalleryUiAdapter>(
() => ({
antialiasProgressImages,
boardSharing,
exportProject,
gallery: {
...gallery,
selectItem: (item, selectionPage) =>
selectionPage === undefined ? gallery.selectItem(item) : gallery.selectItem(item, undefined, selectionPage),
setItemMultiSelection: (itemKeys, primaryItem, selectionPage) =>
selectionPage === undefined
? gallery.setItemMultiSelection(itemKeys, primaryItem)
: gallery.setItemMultiSelection(itemKeys, primaryItem, undefined, selectionPage),
toggleItemSelection: (item, nextPrimaryItem, selectionPage) =>
selectionPage === undefined
? gallery.toggleItemSelection(item, nextPrimaryItem)
: gallery.toggleItemSelection(item, nextPrimaryItem, undefined, selectionPage),
updateSettings: (settings, targetProjectId) => {
if (!isAccountScopeCurrent(accountScope)) {
return;
}
if (targetProjectId !== undefined) {
if (queries.getProject(targetProjectId)) {
gallery.updateSettings(settings, targetProjectId);
}
} else if (queries.isActiveProject(projectId)) {
gallery.updateSettings(settings, projectId);
}
},
},
galleryValues,
generateValues,
// Loaded on first use, beside the item actions it shares with the item deletion dialog.
getBoardMediaUsage: (boardId, signal) =>
import('@workbench/image-actions/mediaUsageReport').then(({ resolveMediaUsage }) =>
resolveMediaUsage(
{ boardId, kind: 'board' },
{
getLoadedProjects: () => queries.getSnapshot().projects,
getSyncBase: (targetProjectId) => getProjectSyncSnapshot().projects[targetProjectId],
openProjectId: projectId,
},
{ signal }
)
),
getItemLabel,
noteBoardBrowsed: (boardId) => {
const values = getGalleryValues(queries.getProject(projectId), queries.getSnapshot().scopedDefaults);
// Once per account, for a project that followed the selected board as earlier builds did by default (it stored
// 'follow', now read as the default destination): say that browsing no longer moves results, the first time the
// user browses away from where they go.
if (
isLegacyFollowAutoAdd(values) &&
!getGallerySettings(values).followSelectedBoard &&
boardId !== (getGalleryAutoAddBoardId(values, () => true) ?? 'none') &&
areWorkbenchPreferencesReady() &&
!getWorkbenchPreferences().galleryDestinationNoticeShown
) {
void patchWorkbenchPreferences({ galleryDestinationNoticeShown: true });
notifications.add({
kind: 'info',
message: t('widgets.gallery.destinationNoticeMessage'),
title: t('widgets.gallery.destinationNoticeTitle'),
});
}
},
getProjectGalleryValues: (targetProjectId) => {
const project = isAccountScopeCurrent(accountScope) ? queries.getProject(targetProjectId) : null;
return project ? getGalleryValues(project, queries.getSnapshot().scopedDefaults) : null;
},
ItemActionsProvider: GalleryItemActionsAdapter,
ImageContextMenu: GalleryImageContextMenu,
liveFollowEnabled,
progressSessions: livePreview.gallerySessions,
pinnedProgressSessionId: livePreview.pinnedSessionId,
followedProgressSessionId: livePreview.followedSessionId,
followProgressSession: (sessionId, { revealPreview }) => {
if (!isAccountScopeCurrent(accountScope) || !queries.isActiveProject(projectId)) {
return;
}
livePreview.follow(sessionId);
if (!revealPreview) {
return;
}
// Reveal Preview where it already is; the default open would pull it into the center over the Gallery.
const revealed = revealPlacedWidget({
project: queries.getSnapshot().activeProject,
typeId: 'preview',
widgets,
});
if (!revealed) {
openWorkbenchWidget('preview');
} else if (revealed.kind === 'floating') {
focusFloating(revealed.instanceId);
} else {
focusRegion(revealed.region, 'preview');
}
},
notifications: {
add: notifications.add,
reportError: notifications.reportError,
undoable: (title, notice) => notify.undoable(title, { ...notice, projectId }),
},
openItemContextMenu,
projectId,
projectName,
projects,
// A board created in or moved into a project needs the project to exist on the server, which a new project does
// only after its first save; the canvas path makes the same request before its first write.
ensureProjectOnServer: async () => {
const project = store.getState().projects.find((candidate) => candidate.id === projectId);
if (project) {
await persistence.ensureProjectOnServer(project);
}
},
widgets: {
openGallery: () => openWorkbenchWidget('gallery').ok,
patchGalleryValues: (values) => widgets.patchValues('gallery', values),
},
}),
[
accountScope,
antialiasProgressImages,
boardSharing,
exportProject,
focusFloating,
focusRegion,
gallery,
galleryValues,
generateValues,
liveFollowEnabled,
livePreview,
notifications,
notify,
openItemContextMenu,
openWorkbenchWidget,
persistence,
projectId,
projectName,
projects,
queries,
store,
t,
widgets,
]
);
return (
<GalleryUiProvider adapter={adapter}>
{children}
<GalleryItemMenuSlot generateValues={generateValues} menu={itemMenu} projectId={projectId} />
</GalleryUiProvider>
);
};