Skip to content

Commit

Permalink
Merge pull request #2768 from weaveworks/pods-on-details-pages
Browse files Browse the repository at this point in the history
Fix including pods in details pages
  • Loading branch information
Robin Sonefors authored Sep 21, 2022
2 parents 2cabcc7 + da33a4b commit bcaea22
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
45 changes: 45 additions & 0 deletions ui/hooks/__tests__/flux.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { flattenChildren } from "../flux";

describe("flattenChildren", () => {
it("supports empty", () => {
const empty = [];
expect(flattenChildren(empty)).toEqual([]);
});

it("handles nested", () => {
const nested = [
{
name: "1",
children: [{ name: "2", children: [{ name: "3", children: [] }] }],
},
];
const flattened = flattenChildren(nested);
expect(flattened[0]).toMatchObject({ name: "1" });
expect(flattened[1]).toMatchObject({ name: "2" });
expect(flattened[2]).toMatchObject({ name: "3" });
});

it("handles multiple", () => {
const multiple = [
{ name: "1", children: [] },
{
name: "2",
children: [
{ name: "3", children: [] },
{ name: "4", children: [] },
],
},
];
const flattened = flattenChildren(multiple);
expect(flattened[0]).toMatchObject({ name: "1" });
expect(flattened[1]).toMatchObject({ name: "2" });
expect(flattened[2]).toMatchObject({ name: "3" });
expect(flattened[3]).toMatchObject({ name: "4" });
});

it("breaks if the format changes", () => {
// No children property suggests API has changed
const invalid = [{ name: "1" }];
expect(() => flattenChildren(invalid)).toThrow(TypeError);
});
});
20 changes: 18 additions & 2 deletions ui/hooks/flux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
GroupVersionKind,
UnstructuredObject,
} from "../lib/api/core/types.pb";
import { getChildren } from "../lib/graph";
import { getChildren, UnstructuredObjectWithChildren } from "../lib/graph";
import {
DefaultCluster,
NoNamespace,
Expand Down Expand Up @@ -48,6 +48,12 @@ export function useListFluxCrds(clusterName = DefaultCluster) {
);
}

export function flattenChildren(children: UnstructuredObjectWithChildren[]) {
return children.flatMap((child) =>
[child].concat(flattenChildren(child.children))
);
}

export function useGetReconciledObjects(
name: string,
namespace: string,
Expand All @@ -63,7 +69,17 @@ export function useGetReconciledObjects(

return useQuery<UnstructuredObject[], RequestError>(
["reconciled_objects", { name, namespace, type, kinds }],
() => getChildren(api, name, namespace, type, kinds, clusterName),
async () => {
const childrenTrees = await getChildren(
api,
name,
namespace,
type,
kinds,
clusterName
);
return flattenChildren(childrenTrees);
},
opts
);
}
Expand Down

0 comments on commit bcaea22

Please sign in to comment.