plane/web/hooks/use-user.tsx
sriram veeraghanta 1e152c666c
New Directory Setup (#2065)
* chore: moved app & space from apps to root

* chore: modified workspace configuration

* chore: modified dockerfiles for space and web

* chore: modified icons for space

* feat: updated files for new svg icons supported by next-images

* chore: added /spaces base path for next

* chore: added compose config for space

* chore: updated husky configuration

* chore: updated workflows for new configuration

* chore: changed app name to web

* fix: resolved build errors with web

* chore: reset file tracing root for both projects

* chore: added nginx config for deploy

* fix: eslint and tsconfig settings for space app

* husky setup fixes based on new dir

* eslint fixes

* prettier formatting

---------

Co-authored-by: Henit Chobisa <chobisa.henit@gmail.com>
2023-09-03 18:50:30 +05:30

55 lines
1.6 KiB
TypeScript

import { useEffect } from "react";
import { useRouter } from "next/router";
import useSWR from "swr";
// services
import userService from "services/user.service";
// constants
import { CURRENT_USER } from "constants/fetch-keys";
// types
import type { ICurrentUserResponse, IUser } from "types";
export default function useUser({ redirectTo = "", redirectIfFound = false, options = {} } = {}) {
const router = useRouter();
// API to fetch user information
const { data, isLoading, error, mutate } = useSWR<ICurrentUserResponse>(
CURRENT_USER,
() => userService.currentUser(),
options
);
const user = error ? undefined : data;
useEffect(() => {
// if no redirect needed, just return (example: already on /dashboard)
// if user data not yet there (fetch in progress, logged in or not) then don't do anything yet
if (!redirectTo || !user) return;
if (
// If redirectTo is set, redirect if the user was not found.
(redirectTo && !redirectIfFound) ||
// If redirectIfFound is also set, redirect if the user was found
(redirectIfFound && user)
) {
router.push(redirectTo);
return;
// const nextLocation = router.asPath.split("?next=")[1];
// if (nextLocation) {
// router.push(nextLocation as string);
// return;
// } else {
// router.push("/");
// return;
// }
}
}, [user, redirectIfFound, redirectTo, router]);
return {
user,
isUserLoading: isLoading,
mutateUser: mutate,
userError: error,
assignedIssuesLength: user?.assigned_issues ?? 0,
workspaceInvitesLength: user?.workspace_invites ?? 0,
};
}