Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom sort for upload concepts/filters dropdown #2826

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions frontend/src/js/ui-components/InputSelect/InputSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ import {
} from "./InputSelectComponents";
import { optionMatchesQuery } from "./optionMatchesQuery";

interface Props {
label?: string;
disabled?: boolean;
options: SelectOptionT[];
tooltip?: string;
indexPrefix?: number;
placeholder?: string;
clearable?: boolean;
smallMenu?: boolean;
className?: string;
dataTestId?: string;
value: SelectOptionT | null;
optional?: boolean;
onChange: (value: SelectOptionT | null) => void;
function filterOptions(
options: SelectOptionT[],
query: string,
sortOptions?: (a: SelectOptionT, b: SelectOptionT, query: string) => number,
) {
const filtered = options.filter((option) =>
optionMatchesQuery(option, query),
);

return sortOptions
? filtered.sort((a, b) => sortOptions(a, b, query))
: filtered;
}

const InputSelect = ({
Expand All @@ -54,7 +52,23 @@ const InputSelect = ({
optional,
smallMenu,
onChange,
}: Props) => {
sortOptions,
}: {
label?: string;
disabled?: boolean;
options: SelectOptionT[];
tooltip?: string;
indexPrefix?: number;
placeholder?: string;
clearable?: boolean;
smallMenu?: boolean;
className?: string;
dataTestId?: string;
value: SelectOptionT | null;
optional?: boolean;
onChange: (value: SelectOptionT | null) => void;
sortOptions?: (a: SelectOptionT, b: SelectOptionT, query: string) => number;
}) => {
const { t } = useTranslation();
const previousValue = usePrevious(value);
const previousOptions = usePrevious(options);
Expand Down Expand Up @@ -190,28 +204,24 @@ const InputSelect = ({
if (inputValue === value?.label) {
setFilteredOptions(options);
} else {
setFilteredOptions(
options.filter((option) => optionMatchesQuery(option, inputValue)),
);
setFilteredOptions(filterOptions(options, inputValue, sortOptions));
}
}
},
[inputValue, value, options, previousOptions],
[inputValue, value, options, previousOptions, sortOptions],
);

useEffect(
function filterOptionsOnChangingInput() {
if (exists(inputValue) && inputValue !== previousInputValue) {
if (inputValue !== selectedItem?.label) {
setFilteredOptions(
options.filter((option) => optionMatchesQuery(option, inputValue)),
);
setFilteredOptions(filterOptions(options, inputValue, sortOptions));
} else {
setFilteredOptions(options);
}
}
},
[inputValue, previousInputValue, options, selectedItem],
[inputValue, previousInputValue, options, selectedItem, sortOptions],
);

const Select = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const SxPrimaryButton = styled(PrimaryButton)`
flex-shrink: 0;
`;
const SxInputSelect = styled(InputSelect)`
width: 650px;
width: 60vw;
max-width: 900px;
`;

const useUnresolvedItemsCount = (
Expand Down Expand Up @@ -468,6 +469,51 @@ const UploadConceptListModal = ({
],
);

// Pretty custom sorting logic, tested with many combinations of
// filters and concepts to "work well".
// Tries to
// - keep concept and its filters together (in asc order of the filterIdx)
// - tries to prioritize concepts that are matched exactly by search query
// But less "set in stone" than it looks
const sortOptions = useCallback(
(a: SelectOptionT, b: SelectOptionT, query: string) => {
const aDetails = selectOptionsDetails[a.value as string];
const bDetails = selectOptionsDetails[b.value as string];

if (aDetails.type === "concept" && bDetails.type === "concept") {
return a.label.localeCompare(b.label);
}

if (aDetails.type === "filter" && bDetails.type === "filter") {
const sameConcept = aDetails.conceptId === bDetails.conceptId;

if (sameConcept) {
return aDetails.filterIdx - bDetails.filterIdx;
}
}

if (aDetails.concept.label === bDetails.concept.label) {
return aDetails.type === "concept" ? -1 : 1;
}

const aConceptLabel = aDetails.concept.label.toLowerCase();
const bConceptLabel = bDetails.concept.label.toLowerCase();
const queryLower = query.toLowerCase();

const aConceptLabelEqual = aConceptLabel === queryLower;
const bConceptLabelEqual = bConceptLabel === queryLower;

if (aConceptLabelEqual) {
return -1;
} else if (bConceptLabelEqual) {
return 1;
}

return aDetails.concept.label.localeCompare(bDetails.concept.label);
},
[selectOptionsDetails],
);

return (
<Modal
closeIcon
Expand All @@ -482,6 +528,7 @@ const UploadConceptListModal = ({
}
onChange={onChange}
options={selectOptions}
sortOptions={sortOptions}
/>
{(!!resolvedFilters || !!resolvedConcepts) &&
!hasResolvedItems &&
Expand Down