2023-04-08 18:32:33 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import workspaceService from "services/workspace.service";
|
|
|
|
// hooks
|
|
|
|
import useUser from "hooks/use-user";
|
|
|
|
import useWorkspaces from "hooks/use-workspaces";
|
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
2023-02-08 04:45:18 +00:00
|
|
|
// types
|
2023-04-08 18:32:33 +00:00
|
|
|
import type { NextPage } from "next";
|
|
|
|
// fetch-keys
|
|
|
|
import { USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
const Home: NextPage = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const { user } = useUser();
|
|
|
|
const { workspaces } = useWorkspaces();
|
|
|
|
|
|
|
|
const lastActiveWorkspace =
|
|
|
|
user && workspaces.find((workspace) => workspace.id === user.last_workspace_id);
|
|
|
|
|
|
|
|
const { data: invitations } = useSWR(USER_WORKSPACE_INVITATIONS, () =>
|
|
|
|
workspaceService.userWorkspaceInvitations()
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!user) {
|
|
|
|
router.push("/signin");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.is_onboarded) {
|
|
|
|
router.push("/onboarding");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastActiveWorkspace) {
|
|
|
|
router.push(`/${lastActiveWorkspace.slug}`);
|
|
|
|
return;
|
|
|
|
} else if (workspaces.length > 0) {
|
|
|
|
router.push(`/${workspaces[0].slug}`);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
if (invitations && invitations.length > 0) {
|
|
|
|
router.push("/invitations");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
router.push("/create-workspace");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, [user, router, lastActiveWorkspace, workspaces, invitations]);
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
return (
|
|
|
|
<div className="h-screen grid place-items-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Home;
|