2023-01-26 18:12:20 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-05-10 20:45:39 +00:00
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// services
|
|
|
|
import projectService from "services/project.service";
|
2023-05-10 20:45:39 +00:00
|
|
|
// helpers
|
|
|
|
import { orderArrayBy } from "helpers/array.helper";
|
2023-03-07 19:37:00 +00:00
|
|
|
// fetch-keys
|
2023-01-26 18:12:20 +00:00
|
|
|
import { PROJECTS_LIST } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const useProjects = () => {
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
// api fetching
|
2023-03-07 19:37:00 +00:00
|
|
|
const { data: projects, mutate: mutateProjects } = useSWR(
|
2023-01-26 18:12:20 +00:00
|
|
|
workspaceSlug ? PROJECTS_LIST(workspaceSlug as string) : null,
|
|
|
|
workspaceSlug ? () => projectService.getProjects(workspaceSlug as string) : null
|
|
|
|
);
|
|
|
|
|
2023-05-25 07:08:15 +00:00
|
|
|
const recentProjects = [...(projects ?? [])]
|
2023-01-26 18:12:20 +00:00
|
|
|
?.sort((a, b) => Date.parse(`${a.updated_at}`) - Date.parse(`${b.updated_at}`))
|
2023-05-25 07:08:15 +00:00
|
|
|
?.slice(0, 3);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
return {
|
2023-05-10 20:45:39 +00:00
|
|
|
projects: orderArrayBy(projects ?? [], "is_favorite", "descending") || [],
|
2023-01-26 18:12:20 +00:00
|
|
|
recentProjects: recentProjects || [],
|
|
|
|
mutateProjects,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useProjects;
|