2023-01-26 18:12:20 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-07-12 14:25:08 +00:00
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// services
|
|
|
|
import workspaceService from "services/workspace.service";
|
2023-07-12 14:25:08 +00:00
|
|
|
// fetch-keys
|
2023-01-26 18:12:20 +00:00
|
|
|
import { USER_WORKSPACES } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const useWorkspaces = () => {
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
// API to fetch user information
|
2023-07-12 14:25:08 +00:00
|
|
|
const { data, error, mutate } = useSWR(USER_WORKSPACES, () => workspaceService.userWorkspaces());
|
2023-01-26 18:12:20 +00:00
|
|
|
// active workspace
|
|
|
|
const activeWorkspace = data?.find((w) => w.slug === workspaceSlug);
|
|
|
|
|
|
|
|
return {
|
|
|
|
workspaces: data,
|
|
|
|
error,
|
|
|
|
activeWorkspace,
|
|
|
|
mutateWorkspaces: mutate,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useWorkspaces;
|