Skip to content

Commit

Permalink
fix identity identifier in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Raspopov committed Oct 2, 2024
1 parent 76d148a commit 946f954
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
14 changes: 12 additions & 2 deletions ui/src/components/shared/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Schemas } from "src/components/schemas/Schemas";
import { NotFound } from "src/components/shared/NotFound";
import { useIdentityContext } from "src/contexts/Identity";
import { Layout, ROUTES, RouteID } from "src/routes";
import { ROOT_PATH } from "src/utils/constants";
import { IDENTIFIER_SEARCH_PARAM, ROOT_PATH } from "src/utils/constants";

const COMPONENTS: Record<RouteID, ComponentType> = {
connectionDetails: ConnectionDetails,
Expand Down Expand Up @@ -61,7 +61,17 @@ export function Router() {
<Routes>
{identifier ? (
<>
<Route element={<Navigate to={ROUTES.schemas.path} />} path={ROOT_PATH} />
<Route
element={
<Navigate
to={{
pathname: ROUTES.schemas.path,
search: `${IDENTIFIER_SEARCH_PARAM}=${identifier}`,
}}
/>
}
path={ROOT_PATH}
/>

<Route element={<FullWidthLayout />}>{getLayoutRoutes("fullWidth")}</Route>

Expand Down
27 changes: 20 additions & 7 deletions ui/src/contexts/Identity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useContext,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
Expand Down Expand Up @@ -48,6 +49,7 @@ export function IdentityProvider(props: PropsWithChildren) {
status: "pending",
});
const [identifier, setIdentifier] = useState<Identifier>("");
const previousIdentifier = useRef<Identifier | null>(null);
const [searchParams, setSearchParams] = useSearchParams();

const identity =
Expand Down Expand Up @@ -108,15 +110,26 @@ export function IdentityProvider(props: PropsWithChildren) {
);

useEffect(() => {
if (identifier) {
setSearchParams((previousParams) => {
const params = new URLSearchParams(previousParams);
params.set(IDENTIFIER_SEARCH_PARAM, identifier);
return params;
});
if (
identifierParam &&
identifier !== identifierParam &&
isAsyncTaskDataAvailable(identitiesList) &&
identitiesList.data.some((identity) => identity.identifier === identifierParam)
) {
setIdentifier(identifierParam);
} else if (identifier && identifier !== previousIdentifier.current) {
setSearchParams(
(previousParams) => {
const params = new URLSearchParams(previousParams);
params.set(IDENTIFIER_SEARCH_PARAM, identifier);
return params;
},
{ replace: true }
);
setStorageByKey({ key: IDENTIFIER_LOCAL_STORAGE_KEY, value: identifier });
previousIdentifier.current = identifier;
}
}, [identifier, setSearchParams]);
}, [identifier, identifierParam, identitiesList, setSearchParams]);

useEffect(() => {
const { aborter } = makeRequestAbortable(fetchIdentities);
Expand Down

0 comments on commit 946f954

Please sign in to comment.