-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): move clear queue hook to separate file
- Loading branch information
Showing
7 changed files
with
64 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
invokeai/frontend/web/src/features/queue/hooks/useClearQueue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useStore } from '@nanostores/react'; | ||
import { useAppDispatch } from 'app/store/storeHooks'; | ||
import { listCursorChanged, listPriorityChanged } from 'features/queue/store/queueSlice'; | ||
import { toast } from 'features/toast/toast'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useClearQueueMutation, useGetQueueStatusQuery } from 'services/api/endpoints/queue'; | ||
import { $isConnected } from 'services/events/stores'; | ||
|
||
export const useClearQueue = () => { | ||
const { t } = useTranslation(); | ||
const dispatch = useAppDispatch(); | ||
const { data: queueStatus } = useGetQueueStatusQuery(); | ||
const isConnected = useStore($isConnected); | ||
const [trigger, { isLoading }] = useClearQueueMutation({ | ||
fixedCacheKey: 'clearQueue', | ||
}); | ||
|
||
const clearQueue = useCallback(async () => { | ||
if (!queueStatus?.queue.total) { | ||
return; | ||
} | ||
|
||
try { | ||
await trigger().unwrap(); | ||
toast({ | ||
id: 'QUEUE_CLEAR_SUCCEEDED', | ||
title: t('queue.clearSucceeded'), | ||
status: 'success', | ||
}); | ||
dispatch(listCursorChanged(undefined)); | ||
dispatch(listPriorityChanged(undefined)); | ||
} catch { | ||
toast({ | ||
id: 'QUEUE_CLEAR_FAILED', | ||
title: t('queue.clearFailed'), | ||
status: 'error', | ||
}); | ||
} | ||
}, [queueStatus?.queue.total, trigger, dispatch, t]); | ||
|
||
const isDisabled = useMemo(() => !isConnected || !queueStatus?.queue.total, [isConnected, queueStatus?.queue.total]); | ||
|
||
return { | ||
clearQueue, | ||
isLoading, | ||
queueStatus, | ||
isDisabled, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters