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

feat: change search bar sorting order (TT-1812) #61

Merged
merged 3 commits into from
Jan 8, 2025
Merged
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
26 changes: 25 additions & 1 deletion src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,39 @@ import {Key} from 'react';
import {searchNewspaperTitlesInCatalog} from '@/services/catalog.data';
import {CatalogTitle} from '@/models/CatalogTitle';
import ActiveLabel from '@/components/ActiveLabel';
import {FaSearch} from 'react-icons/fa';

export default function SearchBar(props: {inHeader: boolean}) {
const router = useRouter();

function sortTitlesByFilterMatch(titles: CatalogTitle[], filter: string): CatalogTitle[] {
return [...titles].sort((a, b) => {
const aIndex = a.name.toLowerCase().indexOf(filter.toLowerCase());
const bIndex = b.name.toLowerCase().indexOf(filter.toLowerCase());
if (aIndex === 0 && bIndex !== 0) {
return -1;
}
if (aIndex !== 0 && bIndex === 0) {
return 1;
}
if (!a.endDate && b.endDate) {
return -1;
}
if (a.endDate && !b.endDate) {
return 1;
}
return a.name.localeCompare(b.name);
});
}

const titles = useAsyncList<CatalogTitle>({
async load({signal, filterText}) {
if (!filterText) {
return {items: []};
}
const data = await searchNewspaperTitlesInCatalog(filterText, signal);
let data = await searchNewspaperTitlesInCatalog(filterText, signal);
data = sortTitlesByFilterMatch(data, filterText);

return { items: data };
}
});
Expand Down Expand Up @@ -56,6 +79,7 @@ export default function SearchBar(props: {inHeader: boolean}) {
e.continuePropagation();
}
}}
endContent={<FaSearch className='opacity-85' />}
>
{(title: CatalogTitle) =>
<AutocompleteItem
Expand Down