File size: 1,018 Bytes
5881efa
cca515d
4f2c36e
cca515d
 
5881efa
 
cca515d
 
5881efa
 
 
cca515d
 
 
 
 
 
 
5881efa
4f2c36e
5881efa
 
 
 
 
 
 
 
6f0b822
5881efa
 
4f2c36e
cca515d
 
 
 
4f2c36e
 
 
 
 
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
import { useQuery } from "@tanstack/react-query";
import { useLocalStorage, useUpdateEffect } from "react-use";

export const useCollections = (category: string) => {
  const [myGenerationsId] = useLocalStorage<any>('my-own-generations', []);
  const {
    data: collections,
    isFetching: loading,
    refetch,
      } = useQuery(
    ["collections"],
    async () => {
      // if category is my-own, send to reauest myGenerationsId array
      const response = await fetch("/api/collections", {
        method: "POST",
        body: JSON.stringify({
          ids: category === 'my-own' ? myGenerationsId : undefined,
        }),
      })
      const data = await response.json()

      if (!response.ok) {
        throw new Error(data.message)
      }
      return data.images;
    },
    {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,      
    }
  );

  useUpdateEffect(() => {
    refetch()
  }, [category]);

  return {
    collections,
    loading,
  }
};