From 666b79777271f9bb78382ddee542fa8d68734bf6 Mon Sep 17 00:00:00 2001 From: thierryverse Date: Wed, 19 Nov 2025 08:41:14 +0200 Subject: [PATCH] replace imperativ fetch with tanstack query --- .../podcasts/podcasts-client.tsx | 79 ++++++------------- 1 file changed, 25 insertions(+), 54 deletions(-) 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 21bb3d05a..730defae8 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,7 +1,7 @@ "use client"; import { format } from "date-fns"; -import { useAtom } from "jotai"; +import { useAtom, useAtomValue } from "jotai"; import { Calendar, MoreHorizontal, @@ -21,6 +21,7 @@ import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import { toast } from "sonner"; import { deletePodcastMutationAtom } from "@/atoms/podcasts/podcast-mutation.atoms"; +import { podcastsAtom } from "@/atoms/podcasts/podcast-query.atoms"; // UI Components import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; @@ -79,10 +80,7 @@ const podcastCardVariants: Variants = { const MotionCard = motion(Card); export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClientProps) { - const [podcasts, setPodcasts] = useState([]); const [filteredPodcasts, setFilteredPodcasts] = useState([]); - const [isLoading, setIsLoading] = useState(true); - const [error, setError] = useState(null); const [searchQuery, setSearchQuery] = useState(""); const [sortOrder, setSortOrder] = useState("newest"); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); @@ -104,64 +102,37 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient const currentObjectUrlRef = useRef(null); const [{ isPending: isDeletingPodcast, mutateAsync: deletePodcast, error: deleteError }] = useAtom(deletePodcastMutationAtom); + const { + data: podcasts, + isLoading: isFetchingPodcasts, + error: fetchError, + } = useAtomValue(podcastsAtom); // Add podcast image URL constant const PODCAST_IMAGE_URL = "https://static.vecteezy.com/system/resources/thumbnails/002/157/611/small_2x/illustrations-concept-design-podcast-channel-free-vector.jpg"; - // Fetch podcasts from API useEffect(() => { - const fetchPodcasts = async () => { - try { - setIsLoading(true); + if (isFetchingPodcasts) return; - // Get token from localStorage - const token = localStorage.getItem("surfsense_bearer_token"); + if (fetchError) { + console.error("Error fetching podcasts:", fetchError); + setFilteredPodcasts([]); + return; + } - if (!token) { - setError("Authentication token not found. Please log in again."); - setIsLoading(false); - return; - } + if (!podcasts) { + setFilteredPodcasts([]); + return; + } - // Fetch all podcasts for this search space - const response = await fetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/podcasts`, - { - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - cache: "no-store", - } - ); - - if (!response.ok) { - const errorData = await response.json().catch(() => null); - throw new Error( - `Failed to fetch podcasts: ${response.status} ${errorData?.detail || ""}` - ); - } - - const data: Podcast[] = await response.json(); - setPodcasts(data); - setFilteredPodcasts(data); - setError(null); - } catch (error) { - console.error("Error fetching podcasts:", error); - setError(error instanceof Error ? error.message : "Unknown error occurred"); - setPodcasts([]); - setFilteredPodcasts([]); - } finally { - setIsLoading(false); - } - }; - - fetchPodcasts(); + setFilteredPodcasts(podcasts); }, []); // Filter and sort podcasts based on search query and sort order useEffect(() => { + if (!podcasts) return; + let result = [...podcasts]; // Filter by search term @@ -433,7 +404,7 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient {/* Status Messages */} - {isLoading && ( + {isFetchingPodcasts && (
@@ -442,14 +413,14 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient
)} - {error && !isLoading && ( + {fetchError && !isFetchingPodcasts && (

Error loading podcasts

-

{error}

+

{fetchError.message ?? "Failed to load podcasts"}

)} - {!isLoading && !error && filteredPodcasts.length === 0 && ( + {!isFetchingPodcasts && !fetchError && filteredPodcasts.length === 0 && (

No podcasts found

@@ -462,7 +433,7 @@ export default function PodcastsPageClient({ searchSpaceId }: PodcastsPageClient )} {/* Podcast Grid */} - {!isLoading && !error && filteredPodcasts.length > 0 && ( + {!isFetchingPodcasts && !fetchError && filteredPodcasts.length > 0 && (