diff --git a/components/instantsearch-view.tsx b/components/instantsearch-view.tsx index eaad1b7..8f25921 100644 --- a/components/instantsearch-view.tsx +++ b/components/instantsearch-view.tsx @@ -20,14 +20,20 @@ interface InstantSearchViewProps extends Partial; } +const sortOptions = { + "year:desc": CalendarArrowUp, + "year:asc": CalendarArrowDown, + "title:asc": ArrowDownAZ, +}; + export function InstantSearchView(props: InstantSearchViewProps): ReactNode { const t = useTranslations("InstantSearch"); - // TODO encode current state in URL + // TODO encode current view state in URL const [view, setView] = useState<"covers" | "detail">("covers"); return ( - +
{props.children}
@@ -47,7 +53,7 @@ export function InstantSearchView(props: InstantSearchViewProps): ReactNode { ); }) : null} - + { diff --git a/components/instantsearch/sortby.tsx b/components/instantsearch/sortby.tsx index 67e2c2e..1b09a40 100644 --- a/components/instantsearch/sortby.tsx +++ b/components/instantsearch/sortby.tsx @@ -1,4 +1,4 @@ -import { ArrowDownAZ, ArrowDownUp, CalendarArrowDown, CalendarArrowUp } from "lucide-react"; +import { ArrowUpDown, type LucideIcon } from "lucide-react"; import { type MessageKeys, useTranslations } from "next-intl"; import type { ReactNode } from "react"; import { Label } from "react-aria-components"; @@ -9,27 +9,13 @@ import { collectionName } from "@/lib/data"; import { Select, SelectContent, SelectItem, SelectPopover, SelectTrigger } from "../ui/select"; interface InstantSearchSortByProps { - sortOptions: Array; -} - -function SortIcon(props: { field: string | undefined; size: number }): ReactNode { - switch (props.field?.split("/")[2]) { - case "year:desc": - return ; // alternatively: ClockArrow? - case "year:asc": - return ; - case "title:asc": - return ; - case undefined: - default: - return ; - } + sortOptions: Record; } export function InstantSearchSortBy(props: InstantSearchSortByProps): ReactNode { const t = useTranslations("InstantSearch"); - const sortByItems = props.sortOptions.map((field) => { + const sortByItems = Object.keys(props.sortOptions).map((field) => { return { // eslint-disable-next-line @typescript-eslint/no-explicit-any label: t(`sort.${field}` as MessageKeys), @@ -40,6 +26,12 @@ export function InstantSearchSortBy(props: InstantSearchSortByProps): ReactNode const { currentRefinement, options, refine } = useSortBy({ items: sortByItems, }); + + const getIcon = (value: string): LucideIcon => { + return props.sortOptions[value.split("/")[2]!] ?? ArrowUpDown; + }; + + const SelectedIcon = getIcon(currentRefinement); return (