Skip to content

Commit

Permalink
feat: show only available change types in dropdown for config changes
Browse files Browse the repository at this point in the history
Fixes #1642
  • Loading branch information
mainawycliffe committed Apr 30, 2024
1 parent 78a1e61 commit a3835cb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import {
ConfigChangesTypeItem,
getConfigsChangesTypesFilter
} from "@flanksource-ui/api/services/configs";
import { ChangeIcon } from "@flanksource-ui/components/Icon/ChangeIcon";
import TristateReactSelect, {
TriStateOptions
} from "@flanksource-ui/ui/Dropdowns/TristateReactSelect";
import { useQuery } from "@tanstack/react-query";
import { useCallback } from "react";
import { useMemo } from "react";
import { useSearchParams } from "react-router-dom";

type Props = {
onChange?: (value: string | undefined) => void;
searchParamKey?: string;
paramsToReset?: string[];
changeTypes?: Record<string, number>;
isLoading?: boolean;
};

export function ChangesTypesDropdown({
onChange = () => {},
searchParamKey = "changeType",
paramsToReset = []
changeTypes,
paramsToReset = [],
isLoading
}: Props) {
const [searchParams, setSearchParams] = useSearchParams();

const { isLoading, data: configTypeOptions } = useQuery(
["db", "changes_types"],
getConfigsChangesTypesFilter,
{
select: useCallback((data: ConfigChangesTypeItem[] | null) => {
return data?.map(
({ change_type }) =>
({
id: change_type,
label: change_type,
value: change_type,
icon: <ChangeIcon change={{ change_type: change_type }} />
} satisfies TriStateOptions)
);
}, [])
}
);
const configTypeOptions = useMemo(() => {
return changeTypes
? Object.entries(changeTypes).map(([change_type]) => {
return {
id: change_type,
label: change_type,
value: change_type,
icon: <ChangeIcon change={{ change_type: change_type }} />
} satisfies TriStateOptions;
})
: [];
}, [changeTypes]);

const value = searchParams.get(searchParamKey) || undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import ConfigChangesDateRangeFilter from "./ConfigChangesDateRangeFIlter";
type ConfigChangeFiltersProps = React.HTMLProps<HTMLDivElement> & {
paramsToReset?: string[];
hideConfigTypeFilter?: boolean;
changeTypes?: Record<string, number>;
isLoading?: boolean;
};

export function ConfigChangeFilters({
className,
paramsToReset,
hideConfigTypeFilter = false,
changeTypes,
isLoading = false,
...props
}: ConfigChangeFiltersProps) {
return (
Expand All @@ -23,7 +27,11 @@ export function ConfigChangeFilters({
label="Config Type"
/>
)}
<ChangesTypesDropdown paramsToReset={paramsToReset} />
<ChangesTypesDropdown
changeTypes={changeTypes}
isLoading={isLoading}
paramsToReset={paramsToReset}
/>
<ConfigChangeSeverity paramsToReset={paramsToReset} />
<ConfigChangesDateRangeFilter paramsToReset={paramsToReset} />
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/config/ConfigChangesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ export function ConfigChangesPage() {
<InfoMessage message={errorMessage} />
) : (
<>
<ConfigChangeFilters paramsToReset={["pageIndex", "pageSize"]} />
<ConfigChangeFilters
isLoading={isLoading}
changeTypes={data?.summary}
paramsToReset={["pageIndex", "pageSize"]}
/>
<ConfigChangeHistory
data={changes}
isLoading={isLoading}
Expand Down

0 comments on commit a3835cb

Please sign in to comment.