Skip to content

Commit 35396e2

Browse files
authored
ensure api query reacts to branch and date changes (#5771)
1 parent fbce413 commit 35396e2

38 files changed

+352
-259
lines changed

frontend/app/src/config/constants.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export const PROFILE_KIND = "CoreProfile";
1010

1111
export const TASK_TARGET = "CoreTaskTarget";
1212

13-
export const DATA_CHECK_OBJECT = "CoreDataCheck";
14-
1513
export const ACCOUNT_GENERIC_OBJECT = "CoreGenericAccount";
1614
export const ACCOUNT_OBJECT = "CoreAccount";
1715

frontend/app/src/entities/branches/ui/branches-provider.tsx

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,70 @@ import { QSP } from "@/config/qsp";
33
import { useGetBranches } from "@/entities/branches/domain/get-branches.query";
44
import { currentBranchAtom } from "@/entities/branches/stores";
55
import { findSelectedBranch } from "@/entities/branches/utils";
6+
import { Branch } from "@/shared/api/graphql/generated/graphql";
67
import ErrorScreen from "@/shared/components/errors/error-screen";
78
import { InfrahubLoading } from "@/shared/components/loading/infrahub-loading";
89
import { ALERT_TYPES, Alert } from "@/shared/components/ui/alert";
9-
import { useSetAtom } from "jotai";
10+
import { useAtom } from "jotai";
1011
import React, { useEffect } from "react";
1112
import { useNavigate } from "react-router";
1213
import { toast } from "react-toastify";
1314
import { StringParam, useQueryParam } from "use-query-params";
1415

16+
type BranchContext = {
17+
currentBranch: Branch;
18+
setCurrentBranch: (branch: Branch) => void;
19+
};
20+
21+
export const BranchContext = React.createContext<BranchContext | null>(null);
22+
23+
export function useCurrentBranch() {
24+
const context = React.use(BranchContext);
25+
if (!context) {
26+
throw new Error("useCurrentBranch must be used within a BranchesProvider.");
27+
}
28+
29+
return context;
30+
}
31+
1532
export const BranchesProvider = ({ children }: { children?: React.ReactNode }) => {
1633
const { data: branches, isPending, error } = useGetBranches();
17-
const setCurrentBranch = useSetAtom(currentBranchAtom);
34+
const [currentBranch, setCurrentBranch] = useAtom(currentBranchAtom);
1835
const [branchInQueryString] = useQueryParam(QSP.BRANCH, StringParam);
1936
const navigate = useNavigate();
2037

2138
useEffect(() => {
2239
if (isPending || error) return;
2340

2441
const selectedBranch = findSelectedBranch(branches, branchInQueryString);
25-
if (branchInQueryString && !selectedBranch) {
26-
toast(
27-
<Alert
28-
type={ALERT_TYPES.ERROR}
29-
message={
30-
<>
31-
Branch <b>{branchInQueryString}</b> not found, you have been redirected to the main
32-
branch.
33-
</>
34-
}
35-
/>
36-
);
37-
const mainBranch = findSelectedBranch(branches, DEFAULT_BRANCH_NAME);
38-
setCurrentBranch(mainBranch);
39-
navigate("/");
42+
if (selectedBranch) {
43+
setCurrentBranch(selectedBranch);
44+
return;
4045
}
4146

42-
setCurrentBranch(selectedBranch);
47+
toast(
48+
<Alert
49+
type={ALERT_TYPES.ERROR}
50+
message={
51+
<>
52+
Branch <b>{branchInQueryString}</b> not found, you have been redirected to the main
53+
branch.
54+
</>
55+
}
56+
/>
57+
);
58+
const mainBranch = findSelectedBranch(branches, DEFAULT_BRANCH_NAME);
59+
setCurrentBranch(mainBranch);
60+
navigate("/");
4361
}, [branches, branchInQueryString]);
4462

45-
if (isPending) {
63+
if (isPending || !currentBranch) {
4664
return <InfrahubLoading>loading branches...</InfrahubLoading>;
4765
}
4866

4967
if (error) {
5068
return <ErrorScreen message={error.message} />;
5169
}
5270

53-
return children;
71+
return <BranchContext value={{ currentBranch, setCurrentBranch }}>{children}</BranchContext>;
5472
};

frontend/app/src/entities/branches/ui/hooks/use-current-branch.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

frontend/app/src/entities/diff/api/get-diff-tree-from-api.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DiffTreeQueryFilters } from "@/shared/api/graphql/generated/graphql";
22
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
3+
import { PaginationParams } from "@/shared/api/types";
34
import { gql } from "@apollo/client";
45

56
export const DIFF_TREE_QUERY = gql`
@@ -127,11 +128,9 @@ export const DIFF_TREE_QUERY = gql`
127128
}
128129
`;
129130

130-
export type GetDiffTreeFromApiParams = {
131+
export type GetDiffTreeFromApiParams = PaginationParams & {
131132
branchName: string;
132133
filters?: DiffTreeQueryFilters;
133-
limit?: number;
134-
offset?: number;
135134
};
136135

137136
export const getDiffTreeFromApi = async ({

frontend/app/src/entities/diff/domain/get-diff-tree.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { getDiffTreeFromApi } from "@/entities/diff/api/get-diff-tree-from-api";
22
import { DiffTree, DiffTreeQueryFilters } from "@/shared/api/graphql/generated/graphql";
3+
import { PaginationParams } from "@/shared/api/types";
34
import { infiniteQueryOptions, useInfiniteQuery } from "@tanstack/react-query";
45

56
export const DIFF_TREE_PER_PAGE = 300;
67

7-
export type GetDiffTreeParams = {
8+
export type GetDiffTreeParams = PaginationParams & {
89
branchName: string;
910
filters?: DiffTreeQueryFilters;
10-
limit?: number;
11-
offset: number;
1211
};
1312

1413
export type GetDiffTree = (params: GetDiffTreeParams) => Promise<DiffTree>;

frontend/app/src/entities/diff/node-diff/conflict.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useAuth } from "@/entities/authentication/ui/useAuth";
2-
import { currentBranchAtom } from "@/entities/branches/stores";
2+
import { useCurrentBranch } from "@/entities/branches/ui/branches-provider";
33
import { resolveConflict } from "@/entities/diff/api/resolveConflict";
44
import { proposedChangedState } from "@/entities/proposed-changes/stores/proposedChanges.atom";
55
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
@@ -16,7 +16,7 @@ import { useState } from "react";
1616
import { toast } from "react-toastify";
1717

1818
export const Conflict = ({ conflict }: any) => {
19-
const currentBranch = useAtomValue(currentBranchAtom);
19+
const { currentBranch } = useCurrentBranch();
2020
const date = useAtomValue(datetimeAtom);
2121
const [isLoading, setIsLoading] = useState(false);
2222
const proposedChangesDetails = useAtomValue(proposedChangedState);

frontend/app/src/entities/ipam/ip-namespace-selector.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { currentBranchAtom } from "@/entities/branches/stores";
21
import { GET_IP_NAMESPACES } from "@/entities/ipam/api/ip-namespaces";
32
import { IpamNamespace } from "@/shared/api/graphql/generated/graphql";
43
import useQuery from "@/shared/api/graphql/useQuery";
@@ -11,7 +10,7 @@ import {
1110
ComboboxTrigger,
1211
} from "@/shared/components/ui/combobox";
1312
import { Icon } from "@iconify-icon/react";
14-
import { useAtomValue, useSetAtom } from "jotai";
13+
import { useSetAtom } from "jotai";
1514
import { useEffect, useId } from "react";
1615
import { useNavigate, useParams } from "react-router";
1716
import { StringParam, useQueryParam } from "use-query-params";
@@ -20,8 +19,7 @@ import { constructPathForIpam } from "./common/utils";
2019
import { IPAM_QSP, IPAM_ROUTE, IPAM_TABS, NAMESPACE_GENERIC } from "./constants";
2120

2221
export default function IpNamespaceSelector() {
23-
const currentBranchName = useAtomValue(currentBranchAtom);
24-
const { loading, data, error } = useQuery(GET_IP_NAMESPACES, { skip: !currentBranchName });
22+
const { loading, data, error } = useQuery(GET_IP_NAMESPACES);
2523

2624
if (loading) {
2725
return <Skeleton className="h-10 w-80" />;

frontend/app/src/entities/nodes/api/generateRelationshipListQuery.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PaginationParams } from "@/shared/api/types";
12
import { jsonToGraphQLQuery } from "json-to-graphql-query";
23

34
export const generateRelationshipListQuery = ({
@@ -7,11 +8,9 @@ export const generateRelationshipListQuery = ({
78
offset = 0,
89
search = "",
910
peerField,
10-
}: {
11+
}: PaginationParams & {
1112
peer: string;
1213
parent?: { name?: string; value?: string };
13-
limit?: number;
14-
offset?: number;
1514
search?: string;
1615
peerField?: string;
1716
}): string => {

frontend/app/src/entities/nodes/hierarchical-tree.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { currentBranchAtom } from "@/entities/branches/stores";
1+
import { useCurrentBranch } from "@/entities/branches/ui/branches-provider";
22
import { TREE_ROOT_ID } from "@/entities/ipam/constants";
33
import { EMPTY_TREE, PrefixNode, updateTreeData } from "@/entities/ipam/ipam-tree/utils";
44
import {
@@ -30,7 +30,7 @@ export type HierarchicalTreeProps = {
3030

3131
export const HierarchicalTree = ({ schema, currentNodeId, className }: HierarchicalTreeProps) => {
3232
const navigate = useNavigate();
33-
const currentBranch = useAtomValue(currentBranchAtom);
33+
const { currentBranch } = useCurrentBranch();
3434
const currentDate = useAtomValue(datetimeAtom);
3535
const [filters] = useFilters();
3636
const hasAutoGeneratedFiltered = filters.some(

frontend/app/src/entities/nodes/object/api/delete-object-from-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphqlClient from "@/shared/api/graphql/graphqlClientApollo";
2+
import { ContextParams } from "@/shared/api/types";
23
import { gql } from "@apollo/client";
34
import { jsonToGraphQLQuery } from "json-to-graphql-query";
45

@@ -22,7 +23,7 @@ export function deleteObjectFromApi({
2223
objectId,
2324
branchName,
2425
atDate,
25-
}: { objectKind: string; objectId: string; branchName: string; atDate: Date | null }) {
26+
}: ContextParams & { objectKind: string; objectId: string }) {
2627
return graphqlClient.mutate({
2728
mutation: gql(getDeleteObjectQuery(objectKind, objectId)),
2829
context: {

0 commit comments

Comments
 (0)