fix: weird redirection in index page (#801)

This commit is contained in:
Dakshesh Jain 2023-04-12 19:03:08 +05:30 committed by GitHub
parent 9196fb4562
commit db488338fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 24 deletions

View File

@ -67,19 +67,17 @@ export const WorkspaceSidebarDropdown = () => {
};
const handleSignOut = async () => {
await authenticationService
.signOut()
.catch(() =>
router.push("/signin").then(() => {
mutateUser();
});
await authenticationService.signOut().catch(() =>
setToastAlert({
type: "error",
title: "Error!",
message: "Failed to sign out. Please try again.",
})
)
.finally(() => {
router.push("/signin");
mutateUser();
});
);
};
return (

View File

@ -19,46 +19,55 @@ import { USER_WORKSPACE_INVITATIONS } from "constants/fetch-keys";
const Home: NextPage = () => {
const router = useRouter();
const { user } = useUser();
const { workspaces } = useWorkspaces();
const { user, isUserLoading } = useUser();
const { workspaces, error: workspacesError } = useWorkspaces();
const lastActiveWorkspace =
user && workspaces.find((workspace) => workspace.id === user.last_workspace_id);
const { data: invitations } = useSWR(USER_WORKSPACE_INVITATIONS, () =>
const { data: invitations, error: invitationsError } = useSWR(USER_WORKSPACE_INVITATIONS, () =>
workspaceService.userWorkspaceInvitations()
);
useEffect(() => {
if (isUserLoading) return;
if (!user) {
router.push("/signin");
return;
}
if (!user.is_onboarded) {
} else if (!user.is_onboarded) {
router.push("/onboarding");
return;
}
if (!user || (!workspaces && !workspacesError)) return;
if (lastActiveWorkspace) {
router.push(`/${lastActiveWorkspace.slug}`);
return;
} else if (workspaces.length > 0) {
router.push(`/${workspaces[0].slug}`);
return;
}
if (invitations && invitations.length > 0) {
} else if (!invitationsError && invitations && invitations.length > 0) {
router.push("/invitations");
return;
} else {
router.push("/create-workspace");
return;
}
}, [user, router, lastActiveWorkspace, workspaces, invitations]);
}, [
invitations,
invitationsError,
isUserLoading,
lastActiveWorkspace,
user,
workspaces,
router,
workspacesError,
]);
return (
<div className="h-screen grid place-items-center">
<div className="grid h-screen place-items-center">
<Spinner />
</div>
);