Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBurgess committed Dec 19, 2024
1 parent c70769b commit 97aff70
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,23 @@ const Inner = ({ withTitle }) => {
}
};

type BenchmarkProps = PanelDefinition & {
type BenchmarkProps = {
definition: PanelDefinition;
benchmarkChildren?: PanelDefinition[] | undefined;
showControls: boolean;
withTitle: boolean;
};

const BenchmarkWrapper = (props: BenchmarkProps) => {
return (
<GroupingProvider definition={props}>
<PanelControlsProvider definition={props} enabled={props.showControls}>
<GroupingProvider
definition={props.definition}
benchmarkChildren={props.benchmarkChildren}
>
<PanelControlsProvider
definition={props.definition}
enabled={props.showControls}
>
<Inner withTitle={props.withTitle} />
</PanelControlsProvider>
</GroupingProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,23 @@ const Inner = ({ showControls, withTitle }) => {
}
};

type DetectionBenchmarkWrapperProps = PanelDefinition & {
type DetectionBenchmarkWrapperProps = {
definition: PanelDefinition;
benchmarkChildren?: PanelDefinition[] | undefined;
showControls: boolean;
withTitle: boolean;
};

const DetectionBenchmarkWrapper = (props: DetectionBenchmarkWrapperProps) => {
return (
<GroupingProvider definition={props}>
<PanelControlsProvider definition={props} enabled={props.showControls}>
<GroupingProvider
definition={props.definition}
benchmarkChildren={props.benchmarkChildren}
>
<PanelControlsProvider
definition={props.definition}
enabled={props.showControls}
>
<Inner showControls={props.showControls} withTitle={props.withTitle} />
</PanelControlsProvider>
</GroupingProvider>
Expand Down
10 changes: 6 additions & 4 deletions ui/dashboard/src/components/dashboards/layout/Child/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ const Child = ({
const DetectionBenchmark = getComponent("detection_benchmark");
return (
<DetectionBenchmark
{...(layoutDefinition as PanelDefinition)}
definition={panelDefinition}
benchmarkChildren={layoutDefinition.children}
showControls={showPanelControls}
/>
);
} else {
const Benchmark = getComponent("benchmark");
return (
<Benchmark
{...(layoutDefinition as PanelDefinition)}
definition={panelDefinition}
benchmarkChildren={layoutDefinition.children}
showControls={showPanelControls}
/>
);
Expand All @@ -46,15 +48,15 @@ const Child = ({
const Benchmark = getComponent("benchmark");
return (
<Benchmark
{...(layoutDefinition as PanelDefinition)}
definition={panelDefinition}
showControls={showPanelControls}
/>
);
case "detection":
const DetectionBenchmark = getComponent("detection_benchmark");
return (
<DetectionBenchmark
{...(layoutDefinition as PanelDefinition)}
definition={panelDefinition}
showControls={showPanelControls}
/>
);
Expand Down
30 changes: 16 additions & 14 deletions ui/dashboard/src/hooks/useBenchmarkGrouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ const reducer = (state: CheckGroupNodeStates, action) => {
type CheckGroupingProviderProps = {
children: null | JSX.Element | JSX.Element[];
definition: PanelDefinition;
benchmarkChildren?: PanelDefinition[] | undefined;
};

function recordFilterValues(
Expand Down Expand Up @@ -749,6 +750,7 @@ const includeResult = (result: CheckResult, filterConfig: Filter): boolean => {

const useGroupingInternal = (
definition: PanelDefinition | null,
benchmarkChildren: PanelDefinition[] | undefined,
panelsMap: PanelsMap | undefined,
groupingConfig: CheckDisplayGroup[],
skip = false,
Expand All @@ -772,24 +774,21 @@ const useGroupingInternal = (
}

// @ts-ignore
const nestedBenchmarks = definition.children?.filter(
const nestedBenchmarks = benchmarkChildren?.filter(
(child) => child.panel_type === "benchmark",
);
const nestedControls =
definition.panel_type === "control"
? [definition]
: // @ts-ignore
definition.children?.filter(
(child) => child.panel_type === "control",
);
benchmarkChildren?.filter((child) => child.panel_type === "control");

const rootBenchmarkPanel = panelsMap[definition.name];
const b = new BenchmarkType(
"0",
rootBenchmarkPanel.name,
rootBenchmarkPanel.title,
rootBenchmarkPanel.description,
rootBenchmarkPanel.documentation,
definition.name,
definition.title,
definition.description,
definition.documentation,
nestedBenchmarks,
nestedControls,
panelsMap,
Expand Down Expand Up @@ -836,7 +835,6 @@ const useGroupingInternal = (

return [
b,
{ ...rootBenchmarkPanel, children: definition.children },
results,
firstChildSummaries,
checkNodeStates,
Expand All @@ -848,6 +846,7 @@ const useGroupingInternal = (
const GroupingProvider = ({
children,
definition,
benchmarkChildren,
}: CheckGroupingProviderProps) => {
const { panelsMap } = useDashboardState();
const { setContext: setDashboardControlsContext } = useDashboardControls();
Expand All @@ -856,12 +855,16 @@ const GroupingProvider = ({

const [
benchmark,
panelDefinition,
grouping,
firstChildSummaries,
tempNodeStates,
filterValues,
] = useGroupingInternal(definition, panelsMap, groupingConfig);
] = useGroupingInternal(
definition,
benchmarkChildren,
panelsMap,
groupingConfig,
);

const previousGroupings = usePrevious({ groupingConfig });

Expand All @@ -888,8 +891,7 @@ const GroupingProvider = ({
<GroupingContext.Provider
value={{
benchmark,
// @ts-ignore
definition: panelDefinition,
definition,
dispatch,
firstChildSummaries,
grouping,
Expand Down
28 changes: 16 additions & 12 deletions ui/dashboard/src/hooks/useDetectionGrouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ const reducer = (state: CheckGroupNodeStates, action) => {
type DetectionGroupingProviderProps = {
children: null | JSX.Element | JSX.Element[];
definition: PanelDefinition;
benchmarkChildren: PanelDefinition[] | undefined;
};

function recordFilterValues(
Expand Down Expand Up @@ -647,6 +648,7 @@ const includeResult = (

const useGroupingInternal = (
definition: PanelDefinition | null,
benchmarkChildren: PanelDefinition[] | undefined,
panelsMap: PanelsMap | undefined,
groupingConfig: DetectionDisplayGroup[],
skip = false,
Expand All @@ -667,24 +669,23 @@ const useGroupingInternal = (
}

// @ts-ignore
const nestedDetectionBenchmarks = definition.children?.filter(
const nestedDetectionBenchmarks = benchmarkChildren?.filter(
(child) => child.panel_type === "benchmark",
);
const nestedDetections =
definition.panel_type === "detection"
? [definition]
: // @ts-ignore
definition.children?.filter(
benchmarkChildren?.filter(
(child) => child.panel_type === "detection",
);

const rootBenchmarkPanel = panelsMap[definition.name];
const b = new DetectionBenchmarkType(
"0",
rootBenchmarkPanel.name,
rootBenchmarkPanel.title,
rootBenchmarkPanel.description,
rootBenchmarkPanel.documentation,
definition.name,
definition.title,
definition.description,
definition.documentation,
nestedDetectionBenchmarks,
nestedDetections,
panelsMap,
Expand Down Expand Up @@ -740,7 +741,6 @@ const useGroupingInternal = (

return [
b,
{ ...rootBenchmarkPanel, children: definition.children },
results,
firstChildSummaries,
hasSeverityResults,
Expand All @@ -753,6 +753,7 @@ const useGroupingInternal = (
const GroupingProvider = ({
children,
definition,
benchmarkChildren,
}: DetectionGroupingProviderProps) => {
const { panelsMap } = useDashboardState();
const { setContext: setDashboardControlsContext } = useDashboardControls();
Expand All @@ -761,13 +762,17 @@ const GroupingProvider = ({

const [
benchmark,
panelDefinition,
grouping,
firstChildSummaries,
hasSeverityResults,
tempNodeStates,
filterValues,
] = useGroupingInternal(definition, panelsMap, groupingConfig);
] = useGroupingInternal(
definition,
benchmarkChildren,
panelsMap,
groupingConfig,
);

const previousGroupings = usePrevious({ groupingConfig });

Expand All @@ -794,8 +799,7 @@ const GroupingProvider = ({
<GroupingContext.Provider
value={{
benchmark,
// @ts-ignore
definition: panelDefinition,
definition,
dispatch,
firstChildSummaries,
hasSeverityResults,
Expand Down

0 comments on commit 97aff70

Please sign in to comment.