import { useEffect, useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { BoardSchema } from "@/lib/client";
import { getBoardsOptions } from "@/lib/client/@tanstack/react-query.gen";

type Props = {
    projectId?: number;
};

const useBoardsList = ({ projectId }: Props) => {
    const queryClient = useQueryClient();

    const [boards, setBoards] = useState<BoardSchema[]>([]);

    const fetchBoards = () => {
        if (!projectId) return;
        queryClient
            .fetchQuery({
                ...getBoardsOptions({ path: { projectId } }),
            })
            .then(data => {
                setBoards(data.boards);
            })
            .catch(err => console.error(err));
    };

    useEffect(() => {
        fetchBoards();
    }, [projectId]);

    return { boards, setBoards, refetch: fetchBoards };
};

export default useBoardsList;
