Skip to content

Commit

Permalink
Add table filter side panel for regular tables. Closes #670.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBurgess committed Jan 14, 2025
1 parent 418f39d commit fb6970a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 42 deletions.
29 changes: 23 additions & 6 deletions ui/dashboard/src/components/dashboards/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,17 @@ const CellValue = ({
return href ? (
<ExternalLink
to={href}
className="link-highlight"
className={classNames(baseClasses, "link-highlight")}
title={showTitle ? `${column.title}=null` : undefined}
>
<>null</>
null
</ExternalLink>
) : (
<span
className="text-foreground-lightest"
className={classNames(baseClasses, "text-foreground-lightest")}
title={showTitle ? `${column.title}=null` : undefined}
>
<>null</>
null
</span>
);
}
Expand Down Expand Up @@ -891,11 +891,28 @@ const TableViewVirtualizedRows = (props: TableProps) => {
const tableColumnChooser = customControls.find(
(c) => c.key === "table-select-columns",
);
if (tableColumnChooser) {
const tableFilter = customControls.find((c) => c.key === "table-filter");
if (tableColumnChooser && tableFilter) {
return;
}
setCustomControls([
...customControls,
...(!props.isDetectionTable
? [
{
key: "table-filter",
title: "Filter",
icon: "filter_alt",
action: async () =>
selectSidePanel({
panel: props,
context: {
mode: "filter",
},
}),
},
]
: []),
{
key: "table-select-columns",
title: "Select table columns",
Expand All @@ -911,7 +928,7 @@ const TableViewVirtualizedRows = (props: TableProps) => {
},
},
]);
}, [customControls, setCustomControls]);
}, [props.isDetectionTable, customControls, setCustomControls]);

const { rows } = table.getRowModel();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import useFilterConfig from "@powerpipe/hooks/useFilterConfig";
import { validateFilter } from "@powerpipe/components/dashboards/grouping/FilterEditor";
import { ReactNode } from "react";

const CustomizeViewSummary = ({ panelName }) => {
const CustomizeViewSummary = ({
panelName,
title = "Filter & Group",
}: {
panelName: string;
title: ReactNode;
}) => {
const { filter: filterConfig } = useFilterConfig(panelName);

const filterCount = filterConfig?.expressions?.length
? filterConfig.expressions.filter(validateFilter).length
: 0;

return <span>Filter & Group{!!filterCount ? ": On" : null}</span>;
return (
<span>
{title}
{!!filterCount ? ": On" : null}
</span>
);
};

export default CustomizeViewSummary;
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ type FilterValueSelectProps = {
index: number;
item: Filter;
type: FilterType;
onChange: (
// value:
// | { value: string; title?: string }
// | { value: string; title?: string }[],
value: string | string[],
) => void;
onChange: (value: string | string[]) => void;
value: string | string[] | undefined;
};

Expand Down Expand Up @@ -111,7 +106,6 @@ const FilterTypeSelect = ({
dynamicKey,
onChange,
}: FilterTypeSelectProps) => {
// const [currentType, setCurrentType] = useState<FilterType>(type);
const { context: filterValues } = useDashboardControls();

const allFilters = useMemo(
Expand Down Expand Up @@ -263,20 +257,6 @@ const FilterValueSelect = ({
value,
onChange,
}: FilterValueSelectProps) => {
// const [currentValue, setCurrentValue] = useState<
// | {
// value: any;
// title?: string;
// }
// | {
// value: any;
// title?: string;
// }[]
// >(
// item.operator === "in" || item.operator === "not_in"
// ? []
// : { value, title: item.title },
// );
const { context: filterValues } = useDashboardControls();
const values = useMemo(() => {
if (!type) {
Expand Down Expand Up @@ -368,20 +348,10 @@ const FilterValueSelect = ({
value: any;
title?: string;
}>) || []
).map((t) => {
// return {
// value: (t as SelectOption).value,
// title: (t as SelectOption).label as string,
// };
return t.value;
}),
).map((t) => t.value),
);
return;
}
// onChange({
// value: (t as SelectOption).value,
// title: (t as SelectOption).label as string,
// });
onChange(
(
t as SingleValue<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const Dashboard = ({
</div>
)}
<VerticalSplitPane
defaultRightPanelSize={selectedRightPanelType === "table" ? 400 : 600}
defaultRightPanelSize={selectedRightPanelType === "table" ? 520 : 600}
minRightPanelSize={selectedRightPanelType === "table" ? 300 : 400}
maxRightPanelSize={selectedRightPanelType === "table" ? 800 : 1000}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const FilterAndGroupSidePanel = ({ panelName }: { panelName: string }) => {
const { closeSidePanel } = useDashboardPanelDetail();
return (
<>
<div className="flex items-center justify-between p-4 min-w-[500px] max-w-[1000px]">
<div className="flex items-center justify-between p-4 min-w-[520px] max-w-[1000px]">
<h3>Filter & Group</h3>
<Icon
className="w-5 h-5 text-foreground cursor-pointer hover:text-foreground-light shrink-0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import FilterConfig from "@powerpipe/components/dashboards/grouping/FilterConfig";
import Icon from "@powerpipe/components/Icon";
import { useDashboardControls } from "@powerpipe/components/dashboards/layout/Dashboard/DashboardControlsProvider";
import { useDashboardPanelDetail } from "@powerpipe/hooks/useDashboardPanelDetail";
import { useDashboardState } from "@powerpipe/hooks/useDashboardState";
import { useEffect } from "react";

const TableFilterSidePanel = ({ panelName }: { panelName: string }) => {
const { panelsMap } = useDashboardState();
const { closeSidePanel } = useDashboardPanelDetail();
const { setContext } = useDashboardControls();
const panel = panelsMap[panelName];

useEffect(() => {
if (!panel.data) {
return;
}
const filterValues = { dimension: { key: {}, value: {} } };
for (const column of panel.data?.columns || []) {
for (const row of panel.data?.rows || []) {
if (!filterValues.dimension.key[column.name]) {
filterValues.dimension.key[column.name] = {};
}
const rowValue = row[column.name];
if (!filterValues.dimension.key[column.name][rowValue]) {
filterValues.dimension.key[column.name][rowValue] = 1;
} else {
filterValues.dimension.key[column.name][rowValue] += 1;
}

if (!filterValues.dimension.value[rowValue]) {
filterValues.dimension.value[rowValue] = {};
}
if (!filterValues.dimension.value[rowValue][column.name]) {
filterValues.dimension.value[rowValue][column.name] = 1;
} else {
filterValues.dimension.value[rowValue][column.name] += 1;
}
}
}
setContext(() => filterValues);
}, [panel.data]);

return (
<>
<div className="flex items-center justify-between p-4 min-w-[520px] max-w-[1000px]">
<h3>Filter</h3>
<Icon
className="w-5 h-5 text-foreground cursor-pointer hover:text-foreground-light shrink-0"
icon="close"
onClick={closeSidePanel}
title="Close"
/>
</div>
<div className="w-full max-h-full border-t border-divide overflow-y-scroll">
<div className="p-4 space-y-3">
<FilterConfig panelName={panelName} />
</div>
</div>
</>
);
};

export default TableFilterSidePanel;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FilterAndGroupSidePanel from "@powerpipe/components/dashboards/layout/DashboardSidePanel/FilterAndGroupSidePanel";
import TableFilterSidePanel from "@powerpipe/components/dashboards/layout/DashboardSidePanel/TableFilterSidePanel";
import TableRowSidePanel from "@powerpipe/components/dashboards/layout/DashboardSidePanel/TableRowSidePanel";
import TableSettingsSidePanel from "@powerpipe/components/dashboards/layout/DashboardSidePanel/TableSettingsSidePanel";
import { classNames } from "@powerpipe/utils/styles";
Expand Down Expand Up @@ -31,6 +32,10 @@ const DashboardSidePanel = ({
sidePanel.panel.panel_type === "detection") && (
<FilterAndGroupSidePanel panelName={sidePanel.panel.name} />
)}
{sidePanel.panel.panel_type === "table" &&
sidePanel.context.mode === "filter" && (
<TableFilterSidePanel panelName={sidePanel.panel.name} />
)}{" "}
{sidePanel.panel.panel_type === "table" &&
sidePanel.context.mode === "row" && (
<TableRowSidePanel
Expand Down

0 comments on commit fb6970a

Please sign in to comment.