Skip to content

Commit

Permalink
DEVPROD-946 Update BuildVariantSelector to trim search terms (evergre…
Browse files Browse the repository at this point in the history
  • Loading branch information
khelif96 authored Feb 27, 2024
1 parent 51becdb commit 9784a5e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/pages/taskHistory/BuildVariantSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback, useState } from "react";
import { useQuery } from "@apollo/client";
import styled from "@emotion/styled";
import { Combobox, ComboboxOption } from "@leafygreen-ui/combobox";
Expand All @@ -24,7 +25,7 @@ const BuildVariantSelector: React.FC<BuildVariantSelectorProps> = ({
HistoryQueryParams.VisibleColumns,
[],
);

const [filteredOptions, setFilteredOptions] = useState<string[]>([]);
const { data, loading } = useQuery<
BuildVariantsForTaskNameQuery,
BuildVariantsForTaskNameQueryVariables
Expand All @@ -35,16 +36,39 @@ const BuildVariantSelector: React.FC<BuildVariantSelectorProps> = ({
},
});

/**
* `onChange` is a callback function that is called when the user selects a build variant
* @param selectedBuildVariants - an array of build variants that the user has selected
*/
const onChange = (selectedBuildVariants: string[]) => {
sendEvent({
name: "Filter by build variant",
});

setVisibleColumns(selectedBuildVariants);
};

const { buildVariantsForTaskName } = data || {};

/**
* `onFilter` is a callback function that is called when the user types in the search bar
*/
const onFilter = useCallback(
(value: string) => {
setFilteredOptions(
(buildVariantsForTaskName || [])
.filter(({ buildVariant, displayName }) => {
const trimmedValue = value.toLowerCase().trim();
return (
buildVariant.toLowerCase().includes(trimmedValue) ||
displayName.toLowerCase().includes(trimmedValue)
);
})
.map((option) => option.buildVariant) || [],
);
},
[buildVariantsForTaskName],
);

return (
<Container>
<Combobox
Expand All @@ -56,6 +80,8 @@ const BuildVariantSelector: React.FC<BuildVariantSelectorProps> = ({
onChange={onChange}
disabled={loading}
overflow="scroll-x"
onFilter={onFilter}
filteredOptions={filteredOptions}
>
{buildVariantsForTaskName?.map((option) => (
<ComboboxOption
Expand Down

0 comments on commit 9784a5e

Please sign in to comment.