mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-04-23 03:00:31 -04:00
110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
import type { PayloadAction } from '@reduxjs/toolkit';
|
|
import { createSelector, createSlice } from '@reduxjs/toolkit';
|
|
import type { PersistConfig, RootState } from 'app/store/store';
|
|
import { canvasSessionStarted } from 'features/controlLayers/store/canvasStagingAreaSlice';
|
|
import type { Dimensions } from 'features/controlLayers/store/types';
|
|
import { workflowLoaded } from 'features/nodes/store/nodesSlice';
|
|
import { atom } from 'nanostores';
|
|
|
|
import type { CanvasRightPanelTabName, TabName, UIState } from './uiTypes';
|
|
|
|
const initialUIState: UIState = {
|
|
_version: 3,
|
|
activeTab: 'canvas',
|
|
activeTabCanvasRightPanel: 'gallery',
|
|
shouldShowImageDetails: false,
|
|
shouldShowProgressInViewer: true,
|
|
accordions: {},
|
|
expanders: {},
|
|
textAreaSizes: {},
|
|
shouldShowNotificationV2: true,
|
|
};
|
|
|
|
export const uiSlice = createSlice({
|
|
name: 'ui',
|
|
initialState: initialUIState,
|
|
reducers: {
|
|
setActiveTab: (state, action: PayloadAction<TabName>) => {
|
|
state.activeTab = action.payload;
|
|
},
|
|
activeTabCanvasRightPanelChanged: (state, action: PayloadAction<CanvasRightPanelTabName>) => {
|
|
state.activeTabCanvasRightPanel = action.payload;
|
|
},
|
|
setShouldShowImageDetails: (state, action: PayloadAction<boolean>) => {
|
|
state.shouldShowImageDetails = action.payload;
|
|
},
|
|
setShouldShowProgressInViewer: (state, action: PayloadAction<boolean>) => {
|
|
state.shouldShowProgressInViewer = action.payload;
|
|
},
|
|
accordionStateChanged: (state, action: PayloadAction<{ id: string; isOpen: boolean }>) => {
|
|
const { id, isOpen } = action.payload;
|
|
state.accordions[id] = isOpen;
|
|
},
|
|
expanderStateChanged: (state, action: PayloadAction<{ id: string; isOpen: boolean }>) => {
|
|
const { id, isOpen } = action.payload;
|
|
state.expanders[id] = isOpen;
|
|
},
|
|
textAreaSizesStateChanged: (state, action: PayloadAction<{ id: string; size: Partial<Dimensions> }>) => {
|
|
const { id, size } = action.payload;
|
|
state.textAreaSizes[id] = size;
|
|
},
|
|
shouldShowNotificationChanged: (state, action: PayloadAction<boolean>) => {
|
|
state.shouldShowNotificationV2 = action.payload;
|
|
},
|
|
},
|
|
extraReducers(builder) {
|
|
builder.addCase(workflowLoaded, (state) => {
|
|
state.activeTab = 'workflows';
|
|
});
|
|
builder.addCase(canvasSessionStarted, (state) => {
|
|
state.activeTab = 'canvas';
|
|
});
|
|
},
|
|
});
|
|
|
|
export const {
|
|
setActiveTab,
|
|
activeTabCanvasRightPanelChanged,
|
|
setShouldShowImageDetails,
|
|
setShouldShowProgressInViewer,
|
|
accordionStateChanged,
|
|
expanderStateChanged,
|
|
shouldShowNotificationChanged,
|
|
textAreaSizesStateChanged,
|
|
} = uiSlice.actions;
|
|
|
|
export const selectUiSlice = (state: RootState) => state.ui;
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
const migrateUIState = (state: any): any => {
|
|
if (!('_version' in state)) {
|
|
state._version = 1;
|
|
}
|
|
if (state._version === 1) {
|
|
state.activeTab = 'generation';
|
|
state._version = 2;
|
|
}
|
|
if (state._version === 2) {
|
|
state.activeTab = 'canvas';
|
|
state._version = 3;
|
|
}
|
|
return state;
|
|
};
|
|
|
|
export const uiPersistConfig: PersistConfig<UIState> = {
|
|
name: uiSlice.name,
|
|
initialState: initialUIState,
|
|
migrate: migrateUIState,
|
|
persistDenylist: ['shouldShowImageDetails'],
|
|
};
|
|
|
|
const TABS_WITH_LEFT_PANEL: TabName[] = ['canvas', 'upscaling', 'workflows'] as const;
|
|
export const LEFT_PANEL_MIN_SIZE_PX = 400;
|
|
export const $isLeftPanelOpen = atom(true);
|
|
export const selectWithLeftPanel = createSelector(selectUiSlice, (ui) => TABS_WITH_LEFT_PANEL.includes(ui.activeTab));
|
|
|
|
const TABS_WITH_RIGHT_PANEL: TabName[] = ['canvas', 'upscaling', 'workflows'] as const;
|
|
export const RIGHT_PANEL_MIN_SIZE_PX = 390;
|
|
export const $isRightPanelOpen = atom(true);
|
|
export const selectWithRightPanel = createSelector(selectUiSlice, (ui) => TABS_WITH_RIGHT_PANEL.includes(ui.activeTab));
|