Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,4 @@ describe("Repo Settings", () => {
cy.dataCy("command-input").eq(1).should("have.value", "command 1");
});
});

describe("redirects", () => {
it("redirects to repo page from project settings if it's a repo", () => {
cy.visit(getProjectSettingsRoute(repo));
cy.location("pathname").should("equal", `/${getRepoSettingsRoute(repo)}`);
});
});
});
93 changes: 37 additions & 56 deletions apps/spruce/src/hooks/useProjectRedirect/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from "react";
import { useQuery } from "@apollo/client/react";
import { useParams, useLocation, useNavigate } from "react-router-dom";
import { skipToken, useQuery } from "@apollo/client/react";
import { useParams } from "react-router-dom";
import { useErrorToast, useQueryCompleted } from "@evg-ui/lib/hooks";
import { slugs } from "constants/routes";
import { ProjectQuery, ProjectQueryVariables } from "gql/generated/types";
import { PROJECT } from "gql/queries";
Expand All @@ -9,76 +9,57 @@ import { validators } from "utils";
const { validateObjectId } = validators;

interface UseProjectRedirectProps {
sendAnalyticsEvent: (projectId: string, projectIdentifier: string) => void;
shouldRedirect?: boolean;
onError?: (repoId: string) => void;
onRedirect?: (projectId: string, projectIdentifier: string) => void;
}

/**
* useProjectRedirect will replace the project id with the project identifier in the URL.
* @param props - Object containing the following:
* @param props.sendAnalyticsEvent - analytics event to send upon redirect
* @param props.shouldRedirect - boolean to indicate if a redirect should be attempted
* @param props.onError - function to call if an error occurs during the redirect
* @returns isRedirecting - boolean to indicate if a redirect is in progress
* @param props.onRedirect - callback to call when a redirect is about to occur
* @returns
* - redirectIdentifier: the project identifier to redirect to
* - needsRedirect: boolean indicating whether a redirect is needed
* - loading: boolean indicating whether the query is still loading
* - error: any error that occurred during the query
*/
export const useProjectRedirect = ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use this on more pages 👀

onError,
sendAnalyticsEvent = () => {},
shouldRedirect,
}: UseProjectRedirectProps) => {
onRedirect,
}: UseProjectRedirectProps = {}) => {
const { [slugs.projectIdentifier]: projectIdentifier = "" } = useParams();
const navigate = useNavigate();
const location = useLocation();

const needsRedirect = validateObjectId(projectIdentifier) && shouldRedirect;

const [attemptedRedirect, setAttemptedRedirect] = useState(false);
const hasRedirected = useRef(false);
const needsRedirect = validateObjectId(projectIdentifier);

const { data, error, loading } = useQuery<
ProjectQuery,
ProjectQueryVariables
>(PROJECT, {
skip: !needsRedirect,
variables: {
idOrIdentifier: projectIdentifier,
},
});
>(
PROJECT,
needsRedirect
? {
variables: {
idOrIdentifier: projectIdentifier,
},
}
: skipToken,
);

// Reset redirect flag when project changes
useEffect(() => {
hasRedirected.current = false;
setAttemptedRedirect(false);
}, [projectIdentifier]);
const redirectIdentifier = data?.project?.identifier;

// Handle successful redirect
useEffect(() => {
if (data?.project && !hasRedirected.current) {
hasRedirected.current = true;
const { identifier } = data.project;
const currentUrl = location.pathname.concat(location.search);
const redirectPathname = currentUrl.replace(
projectIdentifier,
identifier,
);
sendAnalyticsEvent(projectIdentifier, identifier);
navigate(redirectPathname, { replace: true });
setAttemptedRedirect(true);
const onRedirectCallback = () => {
if (onRedirect && redirectIdentifier) {
onRedirect(projectIdentifier, redirectIdentifier);
}
}, [data, location, navigate, projectIdentifier, sendAnalyticsEvent]);
};

// Handle error
useEffect(() => {
if (error && !hasRedirected.current) {
hasRedirected.current = true;
setAttemptedRedirect(true);
onError?.(projectIdentifier ?? "");
}
}, [error, onError, projectIdentifier]);
useErrorToast(
error,
`Failed to redirect to project identifier for project '${projectIdentifier}'`,
);
useQueryCompleted(loading, onRedirectCallback);

return {
isRedirecting: needsRedirect && loading,
attemptedRedirect,
redirectIdentifier,
needsRedirect,
loading,
error,
};
};
Loading