From 734265c64591be22eb07b322fecf7b269cc5f4d1 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Fri, 12 Dec 2025 08:16:00 +0000 Subject: [PATCH] feat: migrate dashboard page to use searchSpacesAtom and deleteSearchSpaceMutationAtom --- surfsense_web/app/dashboard/page.tsx | 31 +++++-------------- .../app/dashboard/searchspaces/page.tsx | 4 +-- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/surfsense_web/app/dashboard/page.tsx b/surfsense_web/app/dashboard/page.tsx index 21dbce82b..b1525a9db 100644 --- a/surfsense_web/app/dashboard/page.tsx +++ b/surfsense_web/app/dashboard/page.tsx @@ -1,5 +1,6 @@ "use client"; +import { useAtomValue } from "jotai"; import { AlertCircle, Loader2, Plus, Search, Trash2, UserCheck, Users } from "lucide-react"; import { motion, type Variants } from "motion/react"; import Image from "next/image"; @@ -35,7 +36,8 @@ import { import { Spotlight } from "@/components/ui/spotlight"; import { Tilt } from "@/components/ui/tilt"; import { useUser } from "@/hooks"; -import { useSearchSpaces } from "@/hooks/use-search-spaces"; +import { searchSpacesAtom } from "@/atoms/search-spaces/search-space-query.atoms"; +import { deleteSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-space-mutation.atoms"; import { authenticatedFetch } from "@/lib/auth-utils"; /** @@ -154,7 +156,8 @@ const DashboardPage = () => { }, }; - const { searchSpaces, loading, error, refreshSearchSpaces } = useSearchSpaces(); + const { data: searchSpaces = [], isLoading: loading, error, refetch: refreshSearchSpaces } = useAtomValue(searchSpacesAtom); + const { mutateAsync: deleteSearchSpace } = useAtomValue(deleteSearchSpaceMutationAtom); // Fetch user details const { user, loading: isLoadingUser, error: userError } = useUser(); @@ -169,29 +172,11 @@ const DashboardPage = () => { }; if (loading) return ; - if (error) return ; + if (error) return ; const handleDeleteSearchSpace = async (id: number) => { - // Send DELETE request to the API - try { - const response = await authenticatedFetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${id}`, - { method: "DELETE" } - ); - - if (!response.ok) { - toast.error("Failed to delete search space"); - throw new Error("Failed to delete search space"); - } - - // Refresh the search spaces list after successful deletion - refreshSearchSpaces(); - } catch (error) { - console.error("Error deleting search space:", error); - toast.error("An error occurred while deleting the search space"); - return; - } - toast.success("Search space deleted successfully"); + await deleteSearchSpace({ id }); + refreshSearchSpaces(); }; return ( diff --git a/surfsense_web/app/dashboard/searchspaces/page.tsx b/surfsense_web/app/dashboard/searchspaces/page.tsx index db2e06d44..76e17c0ce 100644 --- a/surfsense_web/app/dashboard/searchspaces/page.tsx +++ b/surfsense_web/app/dashboard/searchspaces/page.tsx @@ -8,10 +8,10 @@ import { createSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-spac export default function SearchSpacesPage() { const router = useRouter(); - const createSearchSpace = useAtomValue(createSearchSpaceMutationAtom); + const { mutateAsync: createSearchSpace } = useAtomValue(createSearchSpaceMutationAtom); const handleCreateSearchSpace = async (data: { name: string; description?: string }) => { - const result = await createSearchSpace.mutateAsync({ + const result = await createSearchSpace({ name: data.name, description: data.description || "", });