Spaces:
Running
Running
import { useQuery } from "@tanstack/react-query"; | |
import { getUrdfs, getCategories, getUrdfById } from "@/api/urdfApi"; | |
import { ContentItem, Category } from "@/lib/types"; | |
// Hook to fetch all Urdfs | |
export const useUrdfs = () => { | |
return useQuery({ | |
queryKey: ["urdfs"], | |
queryFn: getUrdfs, | |
retry: 2, // Retry at most 2 times in case of network issues | |
}); | |
}; | |
// Hook to fetch all categories | |
export const useCategories = () => { | |
return useQuery({ | |
queryKey: ["categories"], | |
queryFn: getCategories, | |
}); | |
}; | |
// Hook to fetch a specific Urdf by ID | |
export const useUrdf = (id: string) => { | |
return useQuery({ | |
queryKey: ["urdf", id], | |
queryFn: () => getUrdfById(id), | |
enabled: !!id, // Only run query if id is provided | |
}); | |
}; | |
// Hook to get Urdfs filtered by category | |
export const useUrdfsByCategory = (categoryId: string) => { | |
const urdfsQuery = useUrdfs(); | |
const filteredUrdfs = | |
urdfsQuery.data?.filter((urdf) => urdf.categories.includes(categoryId)) || | |
[]; | |
return { | |
...urdfsQuery, | |
data: filteredUrdfs, | |
}; | |
}; | |