diff --git a/thoughts-frontend/components/filters-sorting-panel.tsx b/thoughts-frontend/components/filters-sorting-panel.tsx new file mode 100644 index 0000000..20e1b58 --- /dev/null +++ b/thoughts-frontend/components/filters-sorting-panel.tsx @@ -0,0 +1,127 @@ +"use client"; + +import { useRouter, useSearchParams } from "next/navigation"; +import { FeedSortOption } from "@/lib/api"; + +const SORT_OPTIONS: { value: FeedSortOption; label: string }[] = [ + { value: "newest", label: "Newest first" }, + { value: "oldest", label: "Oldest first" }, + { value: "most_liked", label: "Most liked" }, + { value: "most_boosted", label: "Most boosted" }, + { value: "most_discussed", label: "Most discussed" }, +]; + +export function FiltersSortingPanel() { + const router = useRouter(); + const searchParams = useSearchParams(); + + const currentSort = (searchParams.get("sort") ?? "newest") as FeedSortOption; + const originalsOnly = searchParams.get("originals_only") === "true"; + const repliesOnly = searchParams.get("replies_only") === "true"; + const localOnly = searchParams.get("local_only") === "true"; + const hideSensitive = searchParams.get("hide_sensitive") === "true"; + + function update(key: string, value: string | null) { + const params = new URLSearchParams(searchParams.toString()); + params.delete("page"); + if (value === null) { + params.delete(key); + } else { + params.set(key, value); + } + router.push(`/?${params.toString()}`); + } + + function setSort(value: FeedSortOption) { + update("sort", value === "newest" ? null : value); + } + + function toggleFilter(key: string, current: boolean) { + update(key, current ? null : "true"); + } + + return ( +