Do not call useAssume on every route change
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -9,7 +9,7 @@ import type { useAssumeMutation } from "#/__generated__/iam/useAssumeMutation.gr
|
|||||||
import { useOrganizationId } from "../useOrganizationId";
|
import { useOrganizationId } from "../useOrganizationId";
|
||||||
|
|
||||||
interface UseAssumeParameters {
|
interface UseAssumeParameters {
|
||||||
afterAssumePath: string;
|
afterAssumePath?: string;
|
||||||
onSuccess: () => void;
|
onSuccess: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ export function useAssume(params: UseAssumeParameters) {
|
|||||||
if (error instanceof UnAuthenticatedError) {
|
if (error instanceof UnAuthenticatedError) {
|
||||||
const search = new URLSearchParams([
|
const search = new URLSearchParams([
|
||||||
["organization-id", organizationId],
|
["organization-id", organizationId],
|
||||||
["redirect-path", afterAssumePath],
|
["redirect-path", afterAssumePath ?? window.location.href],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
void navigate({ pathname: "/auth/login", search: "?" + search.toString() });
|
void navigate({ pathname: "/auth/login", search: "?" + search.toString() });
|
||||||
@@ -76,7 +76,7 @@ export function useAssume(params: UseAssumeParameters) {
|
|||||||
switch (result.__typename) {
|
switch (result.__typename) {
|
||||||
case "PasswordRequired":
|
case "PasswordRequired":
|
||||||
search.set("organization-id", organizationId);
|
search.set("organization-id", organizationId);
|
||||||
search.set("redirect-path", afterAssumePath);
|
search.set("redirect-path", afterAssumePath ?? window.location.href);
|
||||||
|
|
||||||
void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() });
|
void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() });
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Skeleton } from "@probo/ui";
|
import { Skeleton } from "@probo/ui";
|
||||||
import { Suspense, useCallback } from "react";
|
import { Suspense, useCallback } from "react";
|
||||||
import { useQueryLoader } from "react-relay";
|
import { useQueryLoader } from "react-relay";
|
||||||
import { useLocation } from "react-router";
|
|
||||||
|
|
||||||
import type { ViewerMembershipLayoutQuery } from "#/__generated__/iam/ViewerMembershipLayoutQuery.graphql";
|
import type { ViewerMembershipLayoutQuery } from "#/__generated__/iam/ViewerMembershipLayoutQuery.graphql";
|
||||||
import { useAssume } from "#/hooks/iam/useAssume";
|
import { useAssume } from "#/hooks/iam/useAssume";
|
||||||
@@ -15,7 +14,6 @@ import {
|
|||||||
|
|
||||||
function ViewerMembershipLayoutQueryLoader() {
|
function ViewerMembershipLayoutQueryLoader() {
|
||||||
const organizationId = useOrganizationId();
|
const organizationId = useOrganizationId();
|
||||||
const location = useLocation();
|
|
||||||
|
|
||||||
const [queryRef, loadQuery] = useQueryLoader<ViewerMembershipLayoutQuery>(
|
const [queryRef, loadQuery] = useQueryLoader<ViewerMembershipLayoutQuery>(
|
||||||
viewerMembershipLayoutQuery,
|
viewerMembershipLayoutQuery,
|
||||||
@@ -30,10 +28,7 @@ function ViewerMembershipLayoutQueryLoader() {
|
|||||||
[loadQuery, organizationId],
|
[loadQuery, organizationId],
|
||||||
);
|
);
|
||||||
|
|
||||||
useAssume({
|
useAssume({ onSuccess: onAssumeSuccess });
|
||||||
afterAssumePath: location.pathname,
|
|
||||||
onSuccess: onAssumeSuccess,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!queryRef) {
|
if (!queryRef) {
|
||||||
return <Skeleton className="w-full h-screen" />;
|
return <Skeleton className="w-full h-screen" />;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Skeleton } from "@probo/ui";
|
import { Skeleton } from "@probo/ui";
|
||||||
import { Suspense, useCallback } from "react";
|
import { Suspense, useCallback } from "react";
|
||||||
import { useQueryLoader } from "react-relay";
|
import { useQueryLoader } from "react-relay";
|
||||||
import { useLocation } from "react-router";
|
|
||||||
|
|
||||||
import type { ViewerMembershipLayoutQuery } from "#/__generated__/iam/ViewerMembershipLayoutQuery.graphql";
|
import type { ViewerMembershipLayoutQuery } from "#/__generated__/iam/ViewerMembershipLayoutQuery.graphql";
|
||||||
import { useAssume } from "#/hooks/iam/useAssume";
|
import { useAssume } from "#/hooks/iam/useAssume";
|
||||||
@@ -18,7 +17,6 @@ function EmployeeLayoutQueryLoader() {
|
|||||||
const [queryRef, loadQuery] = useQueryLoader<ViewerMembershipLayoutQuery>(
|
const [queryRef, loadQuery] = useQueryLoader<ViewerMembershipLayoutQuery>(
|
||||||
viewerMembershipLayoutQuery,
|
viewerMembershipLayoutQuery,
|
||||||
);
|
);
|
||||||
const location = useLocation();
|
|
||||||
|
|
||||||
const onAssumeSuccess = useCallback(
|
const onAssumeSuccess = useCallback(
|
||||||
() =>
|
() =>
|
||||||
@@ -29,10 +27,7 @@ function EmployeeLayoutQueryLoader() {
|
|||||||
[loadQuery, organizationId],
|
[loadQuery, organizationId],
|
||||||
);
|
);
|
||||||
|
|
||||||
useAssume({
|
useAssume({ onSuccess: onAssumeSuccess });
|
||||||
afterAssumePath: location.pathname,
|
|
||||||
onSuccess: onAssumeSuccess,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!queryRef) {
|
if (!queryRef) {
|
||||||
return <Skeleton className="w-full h-screen" />;
|
return <Skeleton className="w-full h-screen" />;
|
||||||
|
|||||||
Reference in New Issue
Block a user