diff --git a/components/instantsearch-sortby.tsx b/components/instantsearch-sortby.tsx new file mode 100644 index 0000000..87e777f --- /dev/null +++ b/components/instantsearch-sortby.tsx @@ -0,0 +1,62 @@ +import { type MessageKeys, useTranslations } from "next-intl"; +import type { ReactNode } from "react"; +import { Label } from "react-aria-components"; +import { useSortBy } from "react-instantsearch"; + +import { collectionName } from "@/lib/data"; + +import { Select, SelectContent, SelectItem, SelectPopover, SelectTrigger } from "./ui/select"; + +interface InstantSearchSortByProps { + sortOptions: Array; +} + +export function InstantSearchSortBy(props: InstantSearchSortByProps): ReactNode { + const t = useTranslations("SearchPage"); + + const sortByItems = props.sortOptions.map((field) => { + return { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + label: t(`sort.${field}` as MessageKeys), + value: `${collectionName}/sort/${field}`, + }; + }); + + const { currentRefinement, options, refine } = useSortBy({ + items: sortByItems, + }); + // enforce initial sort + if (currentRefinement === collectionName) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + refine(`${collectionName}/sort/${props.sortOptions[0]!}`); + } + + return ( + + ); +} diff --git a/components/instantsearch.tsx b/components/instantsearch.tsx index 6cbe642..da198ad 100644 --- a/components/instantsearch.tsx +++ b/components/instantsearch.tsx @@ -3,19 +3,17 @@ import type { UiState } from "instantsearch.js"; import { useTranslations } from "next-intl"; import { type ReactNode, useEffect, useRef } from "react"; -import { Configure, SearchBox, SortBy, useInfiniteHits } from "react-instantsearch"; +import { Configure, SearchBox, useInfiniteHits } from "react-instantsearch"; import { InstantSearchNext } from "react-instantsearch-nextjs"; import TypesenseInstantSearchAdapter, { type SearchClient } from "typesense-instantsearch-adapter"; import { collectionName } from "@/lib/data"; import type { Publication } from "@/lib/model"; +import { InstantSearchSortBy } from "./instantsearch-sortby"; import { InstantSearchStats } from "./instantsearch-stats"; import { PublicationGrid } from "./publication-grid"; -// TODO put into props -const sortOptions = ["year:desc", "year:asc", "title:asc"]; - interface InstantSearchProps { queryArgsToRefinementFields: Record; children?: ReactNode; @@ -54,8 +52,8 @@ function InfiniteScroll(): ReactNode { const observer = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting && !isLastPage) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - showMore && showMore(); + showMore(); + // showMore && showMore(); } }); }); @@ -145,16 +143,7 @@ export function InstantSearch(props: InstantSearchProps): ReactNode {
- sort by{" "} - { - return { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - label: t(`sort.${field}` as MessageKeys), - value: `${collectionName}/sort/${field}`, - }; - })} - /> +
diff --git a/messages/en.json b/messages/en.json index e019474..014a815 100644 --- a/messages/en.json +++ b/messages/en.json @@ -90,9 +90,9 @@ "query_placeholder": "search publications", "sort_by": "sort by", "sort": { - "year:asc": "oldest to newest", - "year:desc": "newest to oldest", - "title:asc": "publication title" + "title:asc": "alphabetically", + "year:asc": "oldest first", + "year:desc": "newest first" } }, "TranslatorPage": {