feat(ui): cancel session when staging image accepted

This commit is contained in:
psychedelicious 2023-05-29 19:06:37 +10:00 committed by Kent Keirsey
parent d97438b0b3
commit a7cebbd970
4 changed files with 60 additions and 5 deletions

View File

@ -68,6 +68,7 @@ import {
addReceivedPageOfImagesRejectedListener, addReceivedPageOfImagesRejectedListener,
} from './listeners/receivedPageOfImages'; } from './listeners/receivedPageOfImages';
import { addStagingAreaImageSavedListener } from './listeners/stagingAreaImageSaved'; import { addStagingAreaImageSavedListener } from './listeners/stagingAreaImageSaved';
import { addCommitStagingAreaImageListener } from './listeners/addCommitStagingAreaImageListener';
export const listenerMiddleware = createListenerMiddleware(); export const listenerMiddleware = createListenerMiddleware();
@ -125,6 +126,7 @@ addCanvasDownloadedAsImageListener();
addCanvasCopiedToClipboardListener(); addCanvasCopiedToClipboardListener();
addCanvasMergedListener(); addCanvasMergedListener();
addStagingAreaImageSavedListener(); addStagingAreaImageSavedListener();
addCommitStagingAreaImageListener();
/** /**
* Socket.IO Events - these handle SIO events directly and pass on internal application actions. * Socket.IO Events - these handle SIO events directly and pass on internal application actions.

View File

@ -0,0 +1,37 @@
import { startAppListening } from '..';
import { log } from 'app/logging/useLogger';
import { commitStagingAreaImage } from 'features/canvas/store/canvasSlice';
import { sessionCanceled } from 'services/thunks/session';
const moduleLog = log.child({ namespace: 'canvas' });
export const addCommitStagingAreaImageListener = () => {
startAppListening({
actionCreator: commitStagingAreaImage,
effect: async (action, { dispatch, getState }) => {
const state = getState();
const { sessionId } = state.system;
const canvasSessionId = action.payload;
if (!canvasSessionId) {
moduleLog.debug('No canvas session, skipping cancel');
return;
}
if (canvasSessionId !== sessionId) {
moduleLog.debug(
{
data: {
canvasSessionId,
sessionId,
},
},
'Canvas session does not match global session, skipping cancel'
);
return;
}
dispatch(sessionCanceled({ sessionId }));
},
});
};

View File

@ -32,7 +32,7 @@ const selector = createSelector(
(canvas) => { (canvas) => {
const { const {
layerState: { layerState: {
stagingArea: { images, selectedImageIndex }, stagingArea: { images, selectedImageIndex, sessionId },
}, },
shouldShowStagingOutline, shouldShowStagingOutline,
shouldShowStagingImage, shouldShowStagingImage,
@ -45,6 +45,7 @@ const selector = createSelector(
isOnLastImage: selectedImageIndex === images.length - 1, isOnLastImage: selectedImageIndex === images.length - 1,
shouldShowStagingImage, shouldShowStagingImage,
shouldShowStagingOutline, shouldShowStagingOutline,
sessionId,
}; };
}, },
{ {
@ -61,6 +62,7 @@ const IAICanvasStagingAreaToolbar = () => {
isOnLastImage, isOnLastImage,
currentStagingAreaImage, currentStagingAreaImage,
shouldShowStagingImage, shouldShowStagingImage,
sessionId,
} = useAppSelector(selector); } = useAppSelector(selector);
const { t } = useTranslation(); const { t } = useTranslation();
@ -106,9 +108,20 @@ const IAICanvasStagingAreaToolbar = () => {
} }
); );
const handlePrevImage = () => dispatch(prevStagingAreaImage()); const handlePrevImage = useCallback(
const handleNextImage = () => dispatch(nextStagingAreaImage()); () => dispatch(prevStagingAreaImage()),
const handleAccept = () => dispatch(commitStagingAreaImage()); [dispatch]
);
const handleNextImage = useCallback(
() => dispatch(nextStagingAreaImage()),
[dispatch]
);
const handleAccept = useCallback(
() => dispatch(commitStagingAreaImage(sessionId)),
[dispatch, sessionId]
);
if (!currentStagingAreaImage) return null; if (!currentStagingAreaImage) return null;

View File

@ -696,7 +696,10 @@ export const canvasSlice = createSlice({
0 0
); );
}, },
commitStagingAreaImage: (state) => { commitStagingAreaImage: (
state,
action: PayloadAction<string | undefined>
) => {
if (!state.layerState.stagingArea.images.length) { if (!state.layerState.stagingArea.images.length) {
return; return;
} }