'use client'; import { useRouter } from 'next/navigation'; import { useTranslations } from 'next-intl'; import { useTransition } from 'react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { Button } from '@/components/ui/button'; import { ArrowsUpDownIcon, CheckIcon } from '@heroicons/react/24/outline'; import { cn } from '@/lib/utils'; const sortOptions = [ { labelKey: 'sortNewestFirst', value: 'created_desc' }, { labelKey: 'sortOldestFirst', value: 'created_asc' }, { labelKey: 'sortRecentlyUpdated', value: 'updated_desc' }, { labelKey: 'sortLongestUnchanged', value: 'updated_asc' }, { labelKey: 'sortTitleAsc', value: 'title_asc' }, { labelKey: 'sortTitleDesc', value: 'title_desc' }, { labelKey: 'sortFavoritesFirst', value: 'favorite_desc' }, ]; type SortSelectorProps = { sortingParam?: string; namespace: string; basePath: string; onSortChange: (sort: string) => Promise; }; export default function SortSelector({ sortingParam, namespace, basePath, onSortChange, }: SortSelectorProps) { const t = useTranslations(namespace); const router = useRouter(); const [isPending, startTransition] = useTransition(); const currentSort = sortingParam || 'created_desc'; const currentLabel = t(sortOptions.find((opt) => opt.value === currentSort)?.labelKey || 'sort'); const handleSort = async (sort: string) => { if (sort === currentSort) return; startTransition(async () => { await onSortChange(sort); router.push(`${basePath}?sort=${sort}`, { scroll: false }); }); }; return ( {sortOptions.map((option) => ( handleSort(option.value)} className={cn( 'rounded-xl flex items-center justify-between py-2 cursor-pointer focus:bg-primary/5', currentSort === option.value && 'bg-primary/5 text-primary font-bold', )} > {t(option.labelKey)} {currentSort === option.value && } ))} ); }