Openstatus www.openstatus.dev
at main 45 lines 1.3 kB view raw
1"use client"; 2 3import { Button } from "@/components/ui/button"; 4import { X } from "lucide-react"; 5import { useRouter, useSearchParams } from "next/navigation"; 6 7export function ButtonReset({ only }: { only?: string[] }) { 8 const searchParams = useSearchParams(); 9 const router = useRouter(); 10 11 // Determine if at least one parameter that should be reset is present (or any parameter if `only` is undefined) 12 const hasParamsToReset = only 13 ? only.some((key) => searchParams.has(key)) 14 : !!searchParams.toString(); 15 16 if (!hasParamsToReset) return null; 17 18 const handleClick = () => { 19 // Clone the current search params so we can mutate them 20 const params = new URLSearchParams(searchParams.toString()); 21 22 if (only && only.length > 0) { 23 // Remove only the specified keys 24 only.forEach((key) => params.delete(key)); 25 const query = params.toString(); 26 router.push( 27 query 28 ? `${window.location.pathname}?${query}` 29 : window.location.pathname, 30 ); 31 } else { 32 // No `only` prop provided – remove all query parameters 33 router.push(window.location.pathname); 34 } 35 }; 36 37 if (!hasParamsToReset) return null; 38 39 return ( 40 <Button variant="ghost" size="sm" onClick={handleClick}> 41 <X /> 42 Reset 43 </Button> 44 ); 45}