Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
DEVPROD-754: Add /distros redirect route (#2196)
Browse files Browse the repository at this point in the history
  • Loading branch information
minnakt authored Dec 18, 2023
1 parent c765193 commit 36b2db9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
8 changes: 8 additions & 0 deletions cypress/integration/distroSettings/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ describe("using the distro dropdown", () => {
});
});
});

describe("/distros redirect route", () => {
it("should redirect to the first distro available", () => {
cy.visit("/distros");
cy.location("pathname").should("not.contain", "/distros");
cy.location("pathname").should("eq", "/distro/localhost/settings/general");
});
});
5 changes: 5 additions & 0 deletions src/components/Content/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Route, Routes, Navigate } from "react-router-dom";
import {
DistroSettingsRedirect,
ProjectSettingsRedirect,
UserPatchesRedirect,
WaterfallCommitsRedirect,
Expand Down Expand Up @@ -45,6 +46,10 @@ export const Content: React.FC = () => (
<Route path={`${routes.distroSettings}/*`} element={<Distro />}>
<Route path={tab} element={null} />
</Route>
<Route
path={redirectRoutes.distroSettings}
element={<DistroSettingsRedirect />}
/>
<Route path={routes.host} element={<Host />} />
<Route path={routes.hosts} element={<Hosts />} />
<Route path={routes.jobLogs} element={<JobLogs />}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Header/AuxiliaryDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const AuxiliaryDropdown: React.FC<AuxiliaryDropdownProps> = ({
projectIdentifier,
}) => {
const { sendEvent } = useNavbarAnalytics();
const distro = useFirstDistro();
const { distro } = useFirstDistro();

const menuItems = [
{
Expand Down
19 changes: 19 additions & 0 deletions src/components/Redirects/DistroSettingsRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Navigate } from "react-router-dom";
import {
getDistroSettingsRoute,
DistroSettingsTabRoutes,
} from "constants/routes";
import { useFirstDistro } from "hooks";

export const DistroSettingsRedirect: React.FC = () => {
const { distro, loading } = useFirstDistro();

if (loading) {
return null;
}
return (
<Navigate
to={getDistroSettingsRoute(distro, DistroSettingsTabRoutes.General)}
/>
);
};
2 changes: 2 additions & 0 deletions src/components/Redirects/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DistroSettingsRedirect } from "./DistroSettingsRedirect";
import { ProjectSettingsRedirect } from "./ProjectSettingsRedirect";
import { UserPatchesRedirect } from "./UserPatchesRedirect";
import { WaterfallCommitsRedirect } from "./WaterfallCommitsRedirect";

export {
DistroSettingsRedirect,
ProjectSettingsRedirect,
UserPatchesRedirect,
WaterfallCommitsRedirect,
Expand Down
2 changes: 2 additions & 0 deletions src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const paths = {
commits: "/commits",
container: "/container",
distro: "/distro",
distros: "/distros",
host: "/host",
hosts: "/hosts",
jobLogs: "/job-logs",
Expand All @@ -74,6 +75,7 @@ const paths = {
waterfall: "/waterfall",
};
export const redirectRoutes = {
distroSettings: paths.distros,
projectSettings: paths.projects,
userPatches: `${paths.user}/:id`,
waterfall: `${paths.waterfall}/:projectIdentifier`,
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useFirstDistro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { DISTROS } from "gql/queries";
/**
* `useFirstDistro` returns the alphabetically first distro from Evergreen's list of distros.
* This can be used to generate a general link to distro settings.
* @returns the distro ID
* @returns an object containing the distro ID (string) and loading state (boolean)
*/
export const useFirstDistro = () => {
const { data } = useQuery<DistrosQuery, DistrosQueryVariables>(DISTROS, {
variables: {
onlySpawnable: false,
},
});
const { data, loading } = useQuery<DistrosQuery, DistrosQueryVariables>(
DISTROS,
{
variables: {
onlySpawnable: false,
},
}
);

return data?.distros?.[0]?.name ?? "ubuntu2204-large";
return { distro: data?.distros?.[0]?.name ?? "ubuntu2204-large", loading };
};

0 comments on commit 36b2db9

Please sign in to comment.