import { FC, ReactNode } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import useSWR from "swr"; import useSWRImmutable from "swr/immutable"; // hooks import { Spinner } from "@plane/ui"; import { useUser, useWorkspace } from "@/hooks/store"; // ui export interface IUserAuthWrapper { children: ReactNode; } export const UserAuthWrapper: FC = observer((props) => { const { children } = props; // store hooks const { currentUser, currentUserError, fetchCurrentUser, fetchCurrentUserInstanceAdminStatus, fetchCurrentUserSettings, } = useUser(); const { fetchWorkspaces } = useWorkspace(); // router const router = useRouter(); // fetching user information useSWR("CURRENT_USER_DETAILS", () => fetchCurrentUser(), { shouldRetryOnError: false, }); // fetching current user instance admin status useSWRImmutable("CURRENT_USER_INSTANCE_ADMIN_STATUS", () => fetchCurrentUserInstanceAdminStatus(), { shouldRetryOnError: false, }); // fetching user settings useSWR("CURRENT_USER_SETTINGS", () => fetchCurrentUserSettings(), { shouldRetryOnError: false, }); // fetching all workspaces useSWR("USER_WORKSPACES_LIST", () => fetchWorkspaces(), { shouldRetryOnError: false, }); if (!currentUser && !currentUserError) { return (
); } if (currentUserError) { const redirectTo = router.asPath; router.push(`/?next_path=${redirectTo}`); return null; } return <>{children}; });