Files
InvokeAI/invokeai/frontend/web/src/features/queue/hooks/useResumeProcessor.ts
psychedelicious 7db4d26837 feat(ui): rework progress event handling
- Canvas manages its own progress socket event listeners and progress event data.
- Remove cancellations listener jank.
- Dip into low-level redux subscription API to watch for queue status changes, clearing the last "global" progress event when the queue has nothing in progress. Could also do this in a useEffect I guess.
- Had to shuffle some things around to prevent circular imports, so there are a lot of tiny changes here.
2024-09-18 06:40:47 +03:00

42 lines
1.3 KiB
TypeScript

import { useStore } from '@nanostores/react';
import { toast } from 'features/toast/toast';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useGetQueueStatusQuery, useResumeProcessorMutation } from 'services/api/endpoints/queue';
import { $isConnected } from 'services/events/stores';
export const useResumeProcessor = () => {
const isConnected = useStore($isConnected);
const { data: queueStatus } = useGetQueueStatusQuery();
const { t } = useTranslation();
const [trigger, { isLoading }] = useResumeProcessorMutation({
fixedCacheKey: 'resumeProcessor',
});
const isStarted = useMemo(() => Boolean(queueStatus?.processor.is_started), [queueStatus?.processor.is_started]);
const resumeProcessor = useCallback(async () => {
if (isStarted) {
return;
}
try {
await trigger().unwrap();
toast({
id: 'PROCESSOR_RESUMED',
title: t('queue.resumeSucceeded'),
status: 'success',
});
} catch {
toast({
id: 'PROCESSOR_RESUME_FAILED',
title: t('queue.resumeFailed'),
status: 'error',
});
}
}, [isStarted, trigger, t]);
const isDisabled = useMemo(() => !isConnected || isStarted, [isConnected, isStarted]);
return { resumeProcessor, isLoading, isStarted, isDisabled };
};