diff --git a/src/app/components/mii-list.tsx b/src/app/components/mii-list.tsx index 0e76baf..42d2d26 100644 --- a/src/app/components/mii-list.tsx +++ b/src/app/components/mii-list.tsx @@ -6,6 +6,7 @@ import { prisma } from "@/lib/prisma"; import Carousel from "./carousel"; import LikeButton from "./like-button"; +import SortSelect from "./sort-select"; interface Props { searchParams: Promise<{ [key: string]: string | string[] | undefined }>; @@ -18,18 +19,16 @@ export default async function MiiList({ searchParams, userId, where }: Props) { const session = await auth(); const resolvedSearchParams = await searchParams; - // sort search param - const orderBy: { createdAt?: Prisma.SortOrder; likes?: Prisma.SortOrder } = {}; + // Sort search param + // Defaults to newest + const orderBy: Prisma.MiiOrderByWithRelationInput = + resolvedSearchParams.sort === "newest" + ? { createdAt: "desc" } + : resolvedSearchParams.sort === "likes" + ? { likedBy: { _count: "desc" } } + : { createdAt: "desc" }; - if (resolvedSearchParams.sort === "newest") { - orderBy.createdAt = "desc"; - } else if (resolvedSearchParams.sort === "likes") { - orderBy.likes = "desc"; - } else { - orderBy.createdAt = "desc"; // Default to newest if no valid sort is provided - } - - // tag search param + // Tag search param const rawTags = resolvedSearchParams.tags; const tagFilter = typeof rawTags === "string" @@ -115,13 +114,7 @@ export default async function MiiList({ searchParams, userId, where }: Props) { todo -
- - -
+ diff --git a/src/app/components/sort-select.tsx b/src/app/components/sort-select.tsx new file mode 100644 index 0000000..ff4d2e1 --- /dev/null +++ b/src/app/components/sort-select.tsx @@ -0,0 +1,54 @@ +"use client"; + +import { Icon } from "@iconify/react"; +import { useSelect } from "downshift"; +import { redirect, useSearchParams } from "next/navigation"; + +type Sort = "likes" | "newest"; + +const items = ["likes", "newest"]; + +export default function SortSelect() { + const searchParams = useSearchParams(); + + const currentSort = (searchParams.get("sort") as Sort) || "newest"; + + const { isOpen, getToggleButtonProps, getMenuProps, getItemProps, highlightedIndex, selectedItem } = useSelect({ + items, + selectedItem: currentSort, + onSelectedItemChange: ({ selectedItem }) => { + if (!selectedItem) return; + redirect(`?sort=${selectedItem}`); + }, + }); + + return ( +
+ {/* Toggle button to open the dropdown */} + + + {/* Dropdown menu */} + +
+ ); +}