mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-04-23 03:00:31 -04:00
The `getIntermediatesCount` query is set to `refetchOnMountOrArgsChange`. The intention was for when the settings modal opens (i.e. mounts), the `getIntermediatesCount` query is refetched. But it doesn't work - modals only mount once, there is no lazy rendering for them. So we have to imperatively refetch, by refetching as we open the modal. Closes #5639
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import { useAppDispatch } from 'app/store/storeHooks';
|
|
import { resetCanvas } from 'features/canvas/store/canvasSlice';
|
|
import { controlAdaptersReset } from 'features/controlAdapters/store/controlAdaptersSlice';
|
|
import { addToast } from 'features/system/store/systemSlice';
|
|
import { useCallback, useMemo } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useClearIntermediatesMutation, useGetIntermediatesCountQuery } from 'services/api/endpoints/images';
|
|
import { useGetQueueStatusQuery } from 'services/api/endpoints/queue';
|
|
|
|
export type UseClearIntermediatesReturn = {
|
|
intermediatesCount: number | undefined;
|
|
clearIntermediates: () => void;
|
|
isLoading: boolean;
|
|
hasPendingItems: boolean;
|
|
refetchIntermediatesCount: () => void;
|
|
};
|
|
|
|
export const useClearIntermediates = (shouldShowClearIntermediates: boolean): UseClearIntermediatesReturn => {
|
|
const { t } = useTranslation();
|
|
const dispatch = useAppDispatch();
|
|
|
|
const { data: intermediatesCount, refetch: refetchIntermediatesCount } = useGetIntermediatesCountQuery(undefined, {
|
|
refetchOnMountOrArgChange: true,
|
|
skip: !shouldShowClearIntermediates,
|
|
});
|
|
|
|
const [_clearIntermediates, { isLoading }] = useClearIntermediatesMutation();
|
|
|
|
const { data: queueStatus } = useGetQueueStatusQuery();
|
|
const hasPendingItems = useMemo(
|
|
() => Boolean(queueStatus && (queueStatus.queue.in_progress > 0 || queueStatus.queue.pending > 0)),
|
|
[queueStatus]
|
|
);
|
|
|
|
const clearIntermediates = useCallback(() => {
|
|
if (hasPendingItems) {
|
|
return;
|
|
}
|
|
|
|
_clearIntermediates()
|
|
.unwrap()
|
|
.then((clearedCount) => {
|
|
dispatch(controlAdaptersReset());
|
|
dispatch(resetCanvas());
|
|
dispatch(
|
|
addToast({
|
|
title: t('settings.intermediatesCleared', { count: clearedCount }),
|
|
status: 'info',
|
|
})
|
|
);
|
|
})
|
|
.catch(() => {
|
|
dispatch(
|
|
addToast({
|
|
title: t('settings.intermediatesClearedFailed'),
|
|
status: 'error',
|
|
})
|
|
);
|
|
});
|
|
}, [t, _clearIntermediates, dispatch, hasPendingItems]);
|
|
|
|
return { intermediatesCount, clearIntermediates, isLoading, hasPendingItems, refetchIntermediatesCount };
|
|
};
|