diff --git a/surfsense_web/app/dashboard/[search_space_id]/chats/chats-client.tsx b/surfsense_web/app/dashboard/[search_space_id]/chats/chats-client.tsx index 64661620d..d7fc5e076 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/chats/chats-client.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/chats/chats-client.tsx @@ -161,7 +161,7 @@ export default function ChatsPageClient({ searchSpaceId }: ChatsPageClientProps) const handleDeleteChat = async () => { if (!chatToDelete) return; - await deleteChat(chatToDelete.id); + await deleteChat({ id: chatToDelete.id }); setDeleteDialogOpen(false); setChatToDelete(null); diff --git a/surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx b/surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx index 535524b8e..21bb3d05a 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/podcasts/podcasts-client.tsx @@ -1,6 +1,7 @@ "use client"; import { format } from "date-fns"; +import { useAtom } from "jotai"; import { Calendar, MoreHorizontal, @@ -19,6 +20,7 @@ import { AnimatePresence, motion, type Variants } from "motion/react"; import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import { toast } from "sonner"; +import { deletePodcastMutationAtom } from "@/atoms/podcasts/podcast-mutation.atoms"; // UI Components import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; @@ -88,7 +90,6 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient id: number; title: string; } | null>(null); - const [isDeleting, setIsDeleting] = useState(false); // Audio player state const [currentPodcast, setCurrentPodcast] = useState(null); @@ -101,6 +102,8 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient const [isMuted, setIsMuted] = useState(false); const audioRef = useRef(null); const currentObjectUrlRef = useRef(null); + const [{ isPending: isDeletingPodcast, mutateAsync: deletePodcast, error: deleteError }] = + useAtom(deletePodcastMutationAtom); // Add podcast image URL constant const PODCAST_IMAGE_URL = @@ -330,7 +333,7 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient try { const response = await podcastsApiService.loadPodcast({ - podcast, + request: { id: podcast.id }, controller, }); const objectUrl = URL.createObjectURL(response); @@ -364,38 +367,13 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient const handleDeletePodcast = async () => { if (!podcastToDelete) return; - setIsDeleting(true); try { - const token = localStorage.getItem("surfsense_bearer_token"); - if (!token) { - setIsDeleting(false); - return; - } + await deletePodcast({ id: podcastToDelete.id }); - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts/${podcastToDelete.id}`, - { - method: "DELETE", - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - } - ); - - if (!response.ok) { - throw new Error(`Failed to delete podcast: ${response.statusText}`); - } - - // Close dialog and refresh podcasts + // Close dialog setDeleteDialogOpen(false); setPodcastToDelete(null); - // Update local state by removing the deleted podcast - setPodcasts((prevPodcasts) => - prevPodcasts.filter((podcast) => podcast.id !== podcastToDelete.id) - ); - // If the current playing podcast is deleted, stop playback if (currentPodcast && currentPodcast.id === podcastToDelete.id) { if (audioRef.current) { @@ -404,13 +382,9 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient setCurrentPodcast(null); setIsPlaying(false); } - - toast.success("Podcast deleted successfully"); } catch (error) { console.error("Error deleting podcast:", error); toast.error(error instanceof Error ? error.message : "Failed to delete podcast"); - } finally { - setIsDeleting(false); } }; @@ -933,17 +907,17 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient