File size: 1,073 Bytes
72f0edb
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
6bc7874
72f0edb
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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,
  };
};