import { useEffect, useState } from "react";
import { useQuery } 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 [boards, setBoards] = useState<BoardSchema[]>([]);

    const { data, refetch, isLoading } = useQuery({
        ...getBoardsOptions({ path: { projectId: projectId! } }),
        enabled: projectId !== undefined,
    });

    useEffect(() => {
        if (projectId === undefined) {
            setBoards([]);
            return;
        }
        if (data?.boards) {
            setBoards(data.boards);
        }
    }, [data?.boards, projectId]);

    return { boards, setBoards, refetch, isLoading };
};

export default useBoardsList;
