From 0fca372ed4534efe2f6cfe5f4ad88abfe069ae27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89mile=20R=C3=A9?= Date: Tue, 10 Feb 2026 18:21:46 +0400 Subject: [PATCH] No need for custom hook, logic only used on AssumePage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Émile Ré --- apps/console/relay.config.json | 1 - ...aphql.ts => AssumePageMutation.graphql.ts} | 24 ++--- apps/console/src/hooks/iam/useAssume.ts | 94 ------------------- .../pages/iam/organizations/AssumePage.tsx | 88 ++++++++++++++++- apps/console/vite.config.ts | 2 - 5 files changed, 95 insertions(+), 114 deletions(-) rename apps/console/src/__generated__/iam/{useAssumeMutation.graphql.ts => AssumePageMutation.graphql.ts} (82%) delete mode 100644 apps/console/src/hooks/iam/useAssume.ts diff --git a/apps/console/relay.config.json b/apps/console/relay.config.json index 38c9a36cf..ec6e89044 100644 --- a/apps/console/relay.config.json +++ b/apps/console/relay.config.json @@ -36,7 +36,6 @@ }, "sources": { "apps/console/src/pages/iam": "iam", - "apps/console/src/hooks/iam": "iam", "apps/console/src": "core" } } diff --git a/apps/console/src/__generated__/iam/useAssumeMutation.graphql.ts b/apps/console/src/__generated__/iam/AssumePageMutation.graphql.ts similarity index 82% rename from apps/console/src/__generated__/iam/useAssumeMutation.graphql.ts rename to apps/console/src/__generated__/iam/AssumePageMutation.graphql.ts index 7e4bffbff..17b1d4c94 100644 --- a/apps/console/src/__generated__/iam/useAssumeMutation.graphql.ts +++ b/apps/console/src/__generated__/iam/AssumePageMutation.graphql.ts @@ -1,5 +1,5 @@ /** - * @generated SignedSource<> + * @generated SignedSource<<754086efd418674e8d92f629f7a01736>> * @lightSyntaxTransform * @nogrep */ @@ -13,10 +13,10 @@ export type ReauthenticationReason = "POLICY_REQUIREMENT" | "SENSITIVE_ACTION" | export type AssumeOrganizationSessionInput = { organizationId: string; }; -export type useAssumeMutation$variables = { +export type AssumePageMutation$variables = { input: AssumeOrganizationSessionInput; }; -export type useAssumeMutation$data = { +export type AssumePageMutation$data = { readonly assumeOrganizationSession: { readonly result: { readonly __typename: "OrganizationSessionCreated"; @@ -41,9 +41,9 @@ export type useAssumeMutation$data = { }; } | null | undefined; }; -export type useAssumeMutation = { - response: useAssumeMutation$data; - variables: useAssumeMutation$variables; +export type AssumePageMutation = { + response: AssumePageMutation$data; + variables: AssumePageMutation$variables; }; const node: ConcreteRequest = (function(){ @@ -171,7 +171,7 @@ return { "argumentDefinitions": (v0/*: any*/), "kind": "Fragment", "metadata": null, - "name": "useAssumeMutation", + "name": "AssumePageMutation", "selections": (v3/*: any*/), "type": "Mutation", "abstractKey": null @@ -180,20 +180,20 @@ return { "operation": { "argumentDefinitions": (v0/*: any*/), "kind": "Operation", - "name": "useAssumeMutation", + "name": "AssumePageMutation", "selections": (v3/*: any*/) }, "params": { - "cacheID": "6c65712730108428976b1d0007ca2993", + "cacheID": "cc56faddf33aa8f6b84f139f262647f6", "id": null, "metadata": {}, - "name": "useAssumeMutation", + "name": "AssumePageMutation", "operationKind": "mutation", - "text": "mutation useAssumeMutation(\n $input: AssumeOrganizationSessionInput!\n) {\n assumeOrganizationSession(input: $input) {\n result {\n __typename\n ... on OrganizationSessionCreated {\n membership {\n id\n lastSession {\n id\n expiresAt\n }\n }\n }\n ... on PasswordRequired {\n reason\n }\n ... on SAMLAuthenticationRequired {\n reason\n redirectUrl\n }\n }\n }\n}\n" + "text": "mutation AssumePageMutation(\n $input: AssumeOrganizationSessionInput!\n) {\n assumeOrganizationSession(input: $input) {\n result {\n __typename\n ... on OrganizationSessionCreated {\n membership {\n id\n lastSession {\n id\n expiresAt\n }\n }\n }\n ... on PasswordRequired {\n reason\n }\n ... on SAMLAuthenticationRequired {\n reason\n redirectUrl\n }\n }\n }\n}\n" } }; })(); -(node as any).hash = "1ccd539abfc276084939857e65421be9"; +(node as any).hash = "e5799d78e6d954273c07db7c022b31e7"; export default node; diff --git a/apps/console/src/hooks/iam/useAssume.ts b/apps/console/src/hooks/iam/useAssume.ts deleted file mode 100644 index 4293914b4..000000000 --- a/apps/console/src/hooks/iam/useAssume.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { UnAuthenticatedError } from "@probo/relay"; -import { useEffect } from "react"; -import { useMutation } from "react-relay"; -import { useNavigate } from "react-router"; -import { graphql } from "relay-runtime"; - -import type { useAssumeMutation } from "#/__generated__/iam/useAssumeMutation.graphql"; - -import { useOrganizationId } from "../useOrganizationId"; - -interface UseAssumeParameters { - afterAssumePath?: string; - onSuccess: () => void; -} - -const assumeMutation = graphql` - mutation useAssumeMutation( - $input: AssumeOrganizationSessionInput! - ) { - assumeOrganizationSession(input: $input) { - result { - __typename - ... on OrganizationSessionCreated { - membership { - id - lastSession { - id - expiresAt - } - } - } - ... on PasswordRequired { - reason - } - ... on SAMLAuthenticationRequired { - reason - redirectUrl - } - } - } - } -`; - -export function useAssume(params: UseAssumeParameters) { - const { afterAssumePath, onSuccess } = params; - - const organizationId = useOrganizationId(); - const navigate = useNavigate(); - - const [assumeOrganizationSession] = useMutation(assumeMutation); - - useEffect(() => { - assumeOrganizationSession({ - variables: { - input: { organizationId }, - }, - onError: (error) => { - if (error instanceof UnAuthenticatedError) { - const search = new URLSearchParams([ - ["organization-id", organizationId], - ["redirect-path", afterAssumePath ?? window.location.pathname + window.location.search], - ]); - - void navigate({ pathname: "/auth/login", search: "?" + search.toString() }); - return; - } - }, - onCompleted: ({ assumeOrganizationSession }) => { - if (!assumeOrganizationSession) { - throw new Error("complete mutation result is empty"); - } - - const { result } = assumeOrganizationSession; - const search = new URLSearchParams(); - - switch (result.__typename) { - case "PasswordRequired": - search.set("organization-id", organizationId); - search.set("redirect-path", afterAssumePath ?? window.location.pathname + window.location.search); - - void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() }); - break; - case "SAMLAuthenticationRequired": - window.location.href = result.redirectUrl; - break; - default: - onSuccess(); - } - }, - }); - }, [afterAssumePath, organizationId, onSuccess, navigate, assumeOrganizationSession]); - - return; -} diff --git a/apps/console/src/pages/iam/organizations/AssumePage.tsx b/apps/console/src/pages/iam/organizations/AssumePage.tsx index e2a7c1939..9f389b5dd 100644 --- a/apps/console/src/pages/iam/organizations/AssumePage.tsx +++ b/apps/console/src/pages/iam/organizations/AssumePage.tsx @@ -1,20 +1,98 @@ import { useTranslate } from "@probo/i18n"; +import { UnAuthenticatedError } from "@probo/relay"; +import { useEffect } from "react"; +import { useMutation } from "react-relay"; import { useNavigate, useSearchParams } from "react-router"; +import { graphql } from "relay-runtime"; -import { useAssume } from "#/hooks/iam/useAssume"; +import type { AssumePageMutation } from "#/__generated__/iam/AssumePageMutation.graphql"; +import { useOrganizationId } from "#/hooks/useOrganizationId"; import { IAMRelayProvider } from "#/providers/IAMRelayProvider"; import AuthLayout from "../auth/AuthLayout"; +const assumeMutation = graphql` + mutation AssumePageMutation( + $input: AssumeOrganizationSessionInput! + ) { + assumeOrganizationSession(input: $input) { + result { + __typename + ... on OrganizationSessionCreated { + membership { + id + lastSession { + id + expiresAt + } + } + } + ... on PasswordRequired { + reason + } + ... on SAMLAuthenticationRequired { + reason + redirectUrl + } + } + } + } +`; + function AssumePageInner() { + const organizationId = useOrganizationId(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const { __ } = useTranslate(); - useAssume({ - afterAssumePath: searchParams.get("redirect-path") ?? "/", - onSuccess: () => void navigate(searchParams.get("redirect-path") ?? "/"), - }); + const [assumeOrganizationSession] = useMutation(assumeMutation); + + useEffect(() => { + assumeOrganizationSession({ + variables: { + input: { organizationId }, + }, + onError: (error) => { + if (error instanceof UnAuthenticatedError) { + const search = new URLSearchParams([ + ["organization-id", organizationId], + [ + "redirect-path", + searchParams.get("redirect-path") ?? window.location.pathname + window.location.search, + ], + ]); + + void navigate({ pathname: "/auth/login", search: "?" + search.toString() }); + return; + } + }, + onCompleted: ({ assumeOrganizationSession }) => { + if (!assumeOrganizationSession) { + throw new Error("complete mutation result is empty"); + } + + const { result } = assumeOrganizationSession; + const search = new URLSearchParams(); + + switch (result.__typename) { + case "PasswordRequired": + search.set("organization-id", organizationId); + search.set( + "redirect-path", + searchParams.get("redirect-path") ?? window.location.pathname + window.location.search, + ); + + void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() }); + break; + case "SAMLAuthenticationRequired": + window.location.href = result.redirectUrl; + break; + default: + void navigate(searchParams.get("redirect-path") ?? window.location.pathname + window.location.search); + } + }, + }); + }, [organizationId, navigate, assumeOrganizationSession, searchParams]); return ( diff --git a/apps/console/vite.config.ts b/apps/console/vite.config.ts index 914cff4a2..d51401b4d 100644 --- a/apps/console/vite.config.ts +++ b/apps/console/vite.config.ts @@ -9,7 +9,6 @@ export default defineConfig({ plugins: [ react({ exclude: [ - "src/hooks/iam/**/*", "src/pages/iam/**/*", "src/components/connectors/**/*", ], @@ -27,7 +26,6 @@ export default defineConfig({ }), react({ include: [ - "src/hooks/iam/**/*", "src/pages/iam/**/*", "src/components/connectors/**/*", ],