From 7e15a41fe6c66521f568e33c1d9e46d6fa96ddab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Lu=C3=ADs?= Date: Thu, 21 Nov 2024 10:15:21 -0300 Subject: [PATCH] apps/spu-ui: fix queue show running item even when the rest of the queue is empty. also, refactor useQueue hook to include start and stop methods. --- apps/spu-ui/src/app/_components/queue.tsx | 38 +++++++++++------------ apps/spu-ui/src/app/_hooks/use-queue.tsx | 14 +++++++++ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/apps/spu-ui/src/app/_components/queue.tsx b/apps/spu-ui/src/app/_components/queue.tsx index b1044f3..9e0edac 100644 --- a/apps/spu-ui/src/app/_components/queue.tsx +++ b/apps/spu-ui/src/app/_components/queue.tsx @@ -21,7 +21,6 @@ import type { QueueItemProps } from "../../lib/types"; import { useQueue } from "../_hooks/use-queue"; import { useStatus } from "../_hooks/use-status"; import { kwargsResponseSchema } from "../../lib/schemas/acquisition"; -import { api } from "../../trpc/react"; import { Dropzone } from "./dropzone"; import { EnvMenu } from "./env-menu"; @@ -144,12 +143,8 @@ function QueueItem({ } export function Queue() { - const utils = api.useUtils(); - const { queue } = useQueue(); - const { clear } = useQueue(); + const { queue, clear, start, stop } = useQueue(); const { status } = useStatus(); - const start = api.queue.start.useMutation(); - const stop = api.queue.stop.useMutation(); // const handleDragEnd = ({ active, over }: DragEndEvent) => { // if (over && active.id !== over.id) { @@ -162,33 +157,35 @@ export function Queue() { const startQueue = useCallback(() => { start.mutate(undefined, { - onSuccess: async () => { - await utils.queue.get.invalidate(); + onSuccess: () => { toast.success("Queue started"); }, - onError: async () => { - await utils.queue.get.invalidate(); + onError: () => { toast.error("Failed to start queue"); }, }); - }, [start, utils.queue.get]); + }, [start]); const stopQueue = useCallback(() => { stop.mutate(undefined, { - onSuccess: async () => { - await utils.queue.get.invalidate(); + onSuccess: () => { toast.success("Queue stopped"); }, - onError: async () => { - await utils.queue.get.invalidate(); + onError: () => { toast.error("Failed to stop queue"); }, }); - }, [stop, utils.queue.get]); + }, [stop]); - const clearQueue = useCallback(async () => { - await clear.mutateAsync(); - toast.info("Queue cleared"); + const clearQueue = useCallback(() => { + clear.mutate(undefined, { + onSuccess: () => { + toast.success("Queue cleared"); + }, + onError: () => { + toast.error("Failed to clear queue"); + }, + }); }, [clear]); const handleDragEnd = ({ active, over }: DragEndEvent) => { @@ -232,7 +229,8 @@ export function Queue() { - {queue.data?.items.length === 0 ? ( + {queue.data?.items.length === 0 && + !queue.data.runningItem?.itemUid ? (

Queue is empty. Drag samples here to add them to the queue.

diff --git a/apps/spu-ui/src/app/_hooks/use-queue.tsx b/apps/spu-ui/src/app/_hooks/use-queue.tsx index dcfacbc..52987cc 100644 --- a/apps/spu-ui/src/app/_hooks/use-queue.tsx +++ b/apps/spu-ui/src/app/_hooks/use-queue.tsx @@ -64,11 +64,25 @@ export const useQueue = () => { }, }); + const start = api.queue.start.useMutation({ + onSettled: async () => { + await utils.queue.get.invalidate(); + }, + }); + + const stop = api.queue.stop.useMutation({ + onSettled: async () => { + await utils.queue.get.invalidate(); + }, + }); + return { queue, add, addBatch, remove, clear, + start, + stop, }; };