video.ts41 lines, 1.1 KB
| 1 | import { useQuery } from "@tanstack/react-query"; |
| 2 | import { getAllJobs } from "@/lib/api/videos"; |
| 3 | |
| 4 | interface VideoProcessingStatus { |
| 5 | isProcessing: boolean; |
| 6 | videoTitle?: string; |
| 7 | progress?: number; |
| 8 | error?: string; |
| 9 | } |
| 10 | |
| 11 | export function useVideoProcessingStatus() { |
| 12 | return useQuery<VideoProcessingStatus>({ |
| 13 | queryKey: ["runningJobs"], |
| 14 | queryFn: async () => { |
| 15 | const { jobs } = await getAllJobs(); |
| 16 | // If there are no running jobs |
| 17 | if (!jobs.length) { |
| 18 | return { isProcessing: false }; |
| 19 | } |
| 20 | |
| 21 | // Get the most recent active or waiting job |
| 22 | const activeJob = jobs.find( |
| 23 | (job) => job.state === "active" || job.state === "waiting" |
| 24 | ); |
| 25 | if (!activeJob) { |
| 26 | return { isProcessing: false }; |
| 27 | } |
| 28 | |
| 29 | return { |
| 30 | isProcessing: true, |
| 31 | videoTitle: activeJob.result?.videoInfo?.title || "Processing video...", |
| 32 | progress: activeJob.progress, |
| 33 | error: activeJob.failedReason, |
| 34 | }; |
| 35 | }, |
| 36 | // Poll every 5 seconds while the component is mounted |
| 37 | refetchInterval: 5000, |
| 38 | // Stop polling when the tab is not active |
| 39 | refetchIntervalInBackground: false, |
| 40 | }); |
| 41 | } |