diff --git a/src/App.tsx b/src/App.tsx index ab8f183..57333a3 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -464,9 +464,11 @@ export default function App({ children }: { children: React.ReactNode }) { const pauseDownload = async (downloadState: DownloadState) => { try { - setIsErrorExpected(true); // Set error expected to true to handle UI state - console.log("Killing process with PID:", downloadState.process_id); - await invoke('kill_all_process', { pid: downloadState.process_id }); + if ((downloadState.download_status === 'downloading' && downloadState.process_id) || (downloadState.download_status === 'starting' && downloadState.process_id)) { + setIsErrorExpected(true); // Set error expected to true to handle UI state + console.log("Killing process with PID:", downloadState.process_id); + await invoke('kill_all_process', { pid: downloadState.process_id }); + } downloadStatusUpdater.mutate({ download_id: downloadState.download_id, download_status: 'paused' }, { onSuccess: (data) => { console.log("Download status updated successfully:", data); diff --git a/src/pages/library.tsx b/src/pages/library.tsx index 3d85a35..962ba48 100644 --- a/src/pages/library.tsx +++ b/src/pages/library.tsx @@ -6,7 +6,7 @@ import { Progress } from "@/components/ui/progress"; import { Separator } from "@/components/ui/separator"; import { useToast } from "@/hooks/use-toast"; import { useAppContext } from "@/providers/appContextProvider"; -import { useDownloadActionStatesStore, useDownloaderPageStatesStore, useDownloadStatesStore, useLibraryPageStatesStore } from "@/services/store"; +import { useDownloadActionStatesStore, useDownloadStatesStore, useLibraryPageStatesStore } from "@/services/store"; import { formatBitrate, formatCodec, formatDurationString, formatFileSize, formatSecToTimeString, formatSpeed } from "@/utils"; import { AudioLines, Clock, File, FileAudio2, FileQuestion, FileVideo2, FolderInput, ListVideo, Loader2, Music, Pause, Play, Square, Trash2, Video, X } from "lucide-react"; import { invoke } from "@tauri-apps/api/core"; @@ -33,8 +33,6 @@ export default function LibraryPage() { const setIsCancelingDownload = useDownloadActionStatesStore(state => state.setIsCancelingDownload); const setIsDeleteFileChecked = useDownloadActionStatesStore(state => state.setIsDeleteFileChecked); - const setIsErrorExpected = useDownloaderPageStatesStore((state) => state.setIsErrorExpected); - const { pauseDownload, resumeDownload, cancelDownload } = useAppContext() const { toast } = useToast(); @@ -108,21 +106,25 @@ export default function LibraryPage() { const stopOngoingDownloads = async () => { if (ongoingDownloads.length > 0) { - setIsErrorExpected(true); // Set error expected to true to handle UI state - try { - await invoke('pause_ongoing_downloads').then(() => { - queryClient.invalidateQueries({ queryKey: ['download-states'] }); + for (const state of ongoingDownloads) { + setIsPausingDownload(state.download_id, true); + try { + await pauseDownload(state); + } catch (e) { + console.error(e); toast({ - title: 'Stopped downloads', - description: 'All ongoing downloads are being stopped.', + title: 'Failed to stop download', + description: `An error occurred while trying to stop the download for ${state.title}.`, + variant: "destructive" }); - }); - } catch (e) { - console.error(e); + } finally { + setIsPausingDownload(state.download_id, false); + } + } + if (ongoingDownloads.length === 0) { toast({ - title: 'Failed to stop downloads', - description: 'An error occurred while trying to stop the downloads.', - variant: "destructive" + title: 'Stopped ongoing downloads', + description: 'All ongoing downloads have been stopped successfully.', }); } } else {