Skip to content

Commit

Permalink
FIX: set main as current branch, when a branch is not found (#5685)
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalabbad authored Feb 6, 2025
1 parent 977fecd commit 9958b13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 1 addition & 6 deletions frontend/app/src/entities/branches/domain/get-branches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getBranchesFromApi } from "@/entities/branches/api/get-branches-from-api";
import { branchesState, currentBranchAtom } from "@/entities/branches/stores";
import { findSelectedBranch } from "@/entities/branches/utils";
import { branchesState } from "@/entities/branches/stores";
import { Branch } from "@/shared/api/graphql/generated/graphql";
import { store } from "@/shared/stores";

Expand All @@ -12,11 +11,7 @@ export const getBranches: GetBranches = async () => {
if (error) throw error;

const branches = data?.Branch ?? [];

const params = new URLSearchParams(window.location.search);
const currentBranch = findSelectedBranch(branches, params.get("branch"));
store.set(branchesState, branches);
store.set(currentBranchAtom, currentBranch);

return branches;
};
12 changes: 10 additions & 2 deletions frontend/app/src/entities/branches/ui/branches-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DEFAULT_BRANCH_NAME } from "@/config/constants";
import { QSP } from "@/config/qsp";
import { useGetBranches } from "@/entities/branches/domain/get-branches.query";
import { currentBranchAtom } from "@/entities/branches/stores";
import { findSelectedBranch } from "@/entities/branches/utils";
import ErrorScreen from "@/shared/components/errors/error-screen";
import { InfrahubLoading } from "@/shared/components/loading/infrahub-loading";
import { ALERT_TYPES, Alert } from "@/shared/components/ui/alert";
import { useSetAtom } from "jotai";
import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
import { StringParam, useQueryParam } from "use-query-params";

export const BranchesProvider = ({ children }: { children?: React.ReactNode }) => {
const { data: branches, isPending, error } = useGetBranches();
const setCurrentBranch = useSetAtom(currentBranchAtom);
const [branchInQueryString] = useQueryParam(QSP.BRANCH, StringParam);
const navigate = useNavigate();

Expand All @@ -23,15 +27,19 @@ export const BranchesProvider = ({ children }: { children?: React.ReactNode }) =
<Alert
type={ALERT_TYPES.ERROR}
message={
<div>
<>
Branch <b>{branchInQueryString}</b> not found, you have been redirected to the main
branch.
</div>
</>
}
/>
);
const mainBranch = findSelectedBranch(branches, DEFAULT_BRANCH_NAME);
setCurrentBranch(mainBranch);
navigate("/");
}

setCurrentBranch(selectedBranch);
}, [branches, branchInQueryString]);

if (isPending) {
Expand Down

0 comments on commit 9958b13

Please sign in to comment.