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 { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { RootState } from 'app/store/store';
import type { SliceConfig } from 'app/store/types';
import { isPlainObject, uniq } from 'es-toolkit';
import { logout } from 'features/auth/store/authSlice';
import type { BoardRecordOrderBy } from 'services/api/types';
import { assert } from 'tsafe';
import {
type BoardId,
type ComparisonMode,
type GalleryState,
type GalleryView,
isVideoName,
isVirtualBoardId,
type OrderDir,
zGalleryState,
} from './types';
const getInitialState = (): GalleryState => ({
selection: [],
shouldAutoSwitch: true,
autoAssignBoardOnClick: true,
autoAddBoardId: 'none',
galleryImageMinimumWidth: 90,
alwaysShowImageSizeBadge: false,
selectedBoardId: 'none',
galleryView: 'images',
boardSearchText: '',
starredFirst: true,
orderDir: 'DESC',
searchTerm: '',
imageToCompare: null,
comparisonMode: 'slider',
comparisonFit: 'fill',
shouldShowArchivedBoards: false,
showVirtualBoards: false,
virtualBoardsSectionOpen: true,
boardsListOrderBy: 'created_at',
boardsListOrderDir: 'DESC',
});
const slice = createSlice({
name: 'gallery',
initialState: getInitialState(),
reducers: {
imageSelected: (state, action: PayloadAction<string | null>) => {
const selectedItem = action.payload;
if (!selectedItem) {
state.selection = [];
} else {
state.selection = [selectedItem];
}
if (selectedItem && isVideoName(selectedItem)) {
state.imageToCompare = null;
}
},
selectionChanged: (state, action: PayloadAction<string[]>) => {
state.selection = uniq(action.payload);
const activeItem = action.payload.at(-1);
if (activeItem && isVideoName(activeItem)) {
state.imageToCompare = null;
}
},
imageToCompareChanged: (state, action: PayloadAction<string | null>) => {
state.imageToCompare = action.payload && !isVideoName(action.payload) ? action.payload : null;
},
comparisonModeChanged: (state, action: PayloadAction<ComparisonMode>) => {
state.comparisonMode = action.payload;
},
comparisonModeCycled: (state) => {
switch (state.comparisonMode) {
case 'slider':
state.comparisonMode = 'side-by-side';
break;
case 'side-by-side':
state.comparisonMode = 'hover';
break;
case 'hover':
state.comparisonMode = 'slider';
break;
}
},
shouldAutoSwitchChanged: (state, action: PayloadAction<boolean>) => {
state.shouldAutoSwitch = action.payload;
},
setGalleryImageMinimumWidth: (state, action: PayloadAction<number>) => {
state.galleryImageMinimumWidth = action.payload;
},
autoAssignBoardOnClickChanged: (state, action: PayloadAction<boolean>) => {
state.autoAssignBoardOnClick = action.payload;
},
boardIdSelected: (
state,
action: PayloadAction<{
boardId: BoardId;
select?: {
selection: GalleryState['selection'];
galleryView: GalleryState['galleryView'];
};
}>
) => {
const { boardId, select } = action.payload;
state.selectedBoardId = boardId;
if (select) {
state.selection = select.selection;
state.galleryView = select.galleryView;
}
},
autoAddBoardIdChanged: (state, action: PayloadAction<BoardId>) => {
if (!action.payload) {
state.autoAddBoardId = 'none';
return;
}
// Virtual boards cannot be auto-add targets
if (isVirtualBoardId(action.payload)) {
return;
}
state.autoAddBoardId = action.payload;
},
galleryViewChanged: (state, action: PayloadAction<GalleryView>) => {
state.galleryView = action.payload;
},
boardSearchTextChanged: (state, action: PayloadAction<string>) => {
state.boardSearchText = action.payload;
},
alwaysShowImageSizeBadgeChanged: (state, action: PayloadAction<boolean>) => {
state.alwaysShowImageSizeBadge = action.payload;
},
comparedImagesSwapped: (state) => {
if (state.imageToCompare) {
const oldSelection = state.selection;
state.selection = [state.imageToCompare];
state.imageToCompare = oldSelection[0] ?? null;
}
},
comparisonFitChanged: (state, action: PayloadAction<'contain' | 'fill'>) => {
state.comparisonFit = action.payload;
},
shouldShowArchivedBoardsChanged: (state, action: PayloadAction<boolean>) => {
state.shouldShowArchivedBoards = action.payload;
},
showVirtualBoardsChanged: (state, action: PayloadAction<boolean>) => {
state.showVirtualBoards = action.payload;
// If virtual boards are hidden and a virtual board is selected, reset to 'none'
if (!action.payload && isVirtualBoardId(state.selectedBoardId)) {
state.selectedBoardId = 'none';
state.selection = [];
}
},
virtualBoardsSectionOpenChanged: (state, action: PayloadAction<boolean>) => {
state.virtualBoardsSectionOpen = action.payload;
},
starredFirstChanged: (state, action: PayloadAction<boolean>) => {
state.starredFirst = action.payload;
},
orderDirChanged: (state, action: PayloadAction<OrderDir>) => {
state.orderDir = action.payload;
},
searchTermChanged: (state, action: PayloadAction<string>) => {
state.searchTerm = action.payload;
},
boardsListOrderByChanged: (state, action: PayloadAction<BoardRecordOrderBy>) => {
state.boardsListOrderBy = action.payload;
},
boardsListOrderDirChanged: (state, action: PayloadAction<OrderDir>) => {
state.boardsListOrderDir = action.payload;
},
},
extraReducers(builder) {
// Clear board-related state on logout to prevent stale data when switching users.
// `selection` holds only item-name strings (never persisted, and the DTOs behind
// them are wiped by the store-level resetApiState on logout), but clear it anyway
// so the next user never sees the previous user's selection highlighted.
builder.addCase(logout, (state) => {
state.selectedBoardId = 'none';
state.autoAddBoardId = 'none';
state.boardSearchText = '';
state.selection = [];
});
},
});
export const {
imageSelected,
shouldAutoSwitchChanged,
autoAssignBoardOnClickChanged,
setGalleryImageMinimumWidth,
boardIdSelected,
autoAddBoardIdChanged,
galleryViewChanged,
selectionChanged,
boardSearchTextChanged,
alwaysShowImageSizeBadgeChanged,
imageToCompareChanged,
comparisonModeChanged,
comparedImagesSwapped,
comparisonFitChanged,
comparisonModeCycled,
orderDirChanged,
starredFirstChanged,
shouldShowArchivedBoardsChanged,
showVirtualBoardsChanged,
virtualBoardsSectionOpenChanged,
searchTermChanged,
boardsListOrderByChanged,
boardsListOrderDirChanged,
} = slice.actions;
export const selectGallerySlice = (state: RootState) => state.gallery;
export const gallerySliceConfig: SliceConfig<typeof slice> = {
slice,
schema: zGalleryState,
getInitialState,
persistConfig: {
migrate: (state) => {
assert(isPlainObject(state));
if (!('_version' in state)) {
state._version = 1;
}
// Add virtual boards fields if missing (added in virtual boards feature)
if (!('showVirtualBoards' in state)) {
state.showVirtualBoards = false;
}
if (!('virtualBoardsSectionOpen' in state)) {
state.virtualBoardsSectionOpen = true;
}
return zGalleryState.parse(state);
},
persistDenylist: ['selection', 'galleryView', 'imageToCompare'],
},
};