plane/web/layouts/auth-layout/user-wrapper.tsx
guru_sainath 7d3a96b3d0
[WEB-763] fix: workspace remains listed after leaving the workspace in the user profile (#3993)
* chore: build error

* fix: workspace not getting removed when user leaves the workspace
2024-03-20 13:43:18 +05:30

63 lines
1.8 KiB
TypeScript

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<IUserAuthWrapper> = 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
const { isLoading: userSettingsLoader } = useSWR("CURRENT_USER_SETTINGS", () => fetchCurrentUserSettings(), {
shouldRetryOnError: false,
});
// fetching all workspaces
const { isLoading: workspaceLoader } = useSWR("USER_WORKSPACES_LIST", () => fetchWorkspaces(), {
shouldRetryOnError: false,
});
if ((!currentUser && !currentUserError) || userSettingsLoader || workspaceLoader) {
return (
<div className="grid h-screen place-items-center bg-custom-background-100 p-4">
<div className="flex flex-col items-center gap-3 text-center">
<Spinner />
</div>
</div>
);
}
if (currentUserError) {
const redirectTo = router.asPath;
router.push(`/?next_path=${redirectTo}`);
return null;
}
return <>{children}</>;
});