2023-04-08 18:32:33 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
2023-04-13 10:08:00 +00:00
|
|
|
import Router from "next/router";
|
2023-04-08 18:32:33 +00:00
|
|
|
|
|
|
|
// services
|
2023-04-13 10:08:00 +00:00
|
|
|
import userService from "services/user.service";
|
2023-04-08 18:32:33 +00:00
|
|
|
import workspaceService from "services/workspace.service";
|
|
|
|
// 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";
|
2023-04-12 13:33:08 +00:00
|
|
|
|
2023-04-13 10:08:00 +00:00
|
|
|
const redirectUserTo = async () => {
|
|
|
|
const user = await userService
|
|
|
|
.currentUser()
|
|
|
|
.then((res) => res)
|
|
|
|
.catch(() => {
|
|
|
|
Router.push("/signin");
|
2023-04-08 18:32:33 +00:00
|
|
|
return;
|
2023-04-13 10:08:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
Router.push("/signin");
|
|
|
|
return;
|
|
|
|
} else if (!user.user.is_onboarded) {
|
|
|
|
Router.push("/onboarding");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
const userWorkspaces = await workspaceService.userWorkspaces();
|
2023-04-08 18:32:33 +00:00
|
|
|
|
2023-04-13 10:08:00 +00:00
|
|
|
const lastActiveWorkspace = userWorkspaces.find(
|
|
|
|
(workspace) => workspace.id === user.user.last_workspace_id
|
|
|
|
);
|
2023-04-12 13:33:08 +00:00
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
if (lastActiveWorkspace) {
|
2023-04-13 10:08:00 +00:00
|
|
|
Router.push(`/${lastActiveWorkspace.slug}`);
|
2023-04-08 18:32:33 +00:00
|
|
|
return;
|
2023-04-13 10:08:00 +00:00
|
|
|
} else if (userWorkspaces.length > 0) {
|
|
|
|
Router.push(`/${userWorkspaces[0].slug}`);
|
2023-04-08 18:32:33 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2023-04-13 10:08:00 +00:00
|
|
|
const invitations = await workspaceService.userWorkspaceInvitations();
|
|
|
|
if (invitations.length > 0) {
|
|
|
|
Router.push(`/invitations`);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Router.push(`/create-workspace`);
|
|
|
|
return;
|
|
|
|
}
|
2023-04-08 18:32:33 +00:00
|
|
|
}
|
2023-04-13 10:08:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const Home: NextPage = () => {
|
|
|
|
useEffect(() => {
|
|
|
|
redirectUserTo();
|
|
|
|
}, []);
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
return (
|
2023-04-12 13:33:08 +00:00
|
|
|
<div className="grid h-screen place-items-center">
|
2023-04-08 18:32:33 +00:00
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Home;
|