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

const useProjectsList = () => {
    const queryClient = useQueryClient();

    const [projects, setProjects] = useState<ProjectSchema[]>([]);

    const fetchProjects = async () => {
        return queryClient
            .fetchQuery({
                ...getProjectsOptions(),
            })
            .then(data => {
                setProjects(data.projects);
            })
            .catch(err => console.error(err));
    };

    useEffect(() => {
        fetchProjects();
    }, []);

    return { projects, setProjects, refetch: fetchProjects };
};

export default useProjectsList;
