"use client"; import { useRouter, useSearchParams } from "next/navigation"; import { useTransition } from "react"; import { RectangleVertical, RectangleHorizontal } from "lucide-react"; import { cn } from "@/lib/utils"; export type LibraryView = "portrait" | "landscape"; export function ViewToggle({ current }: { current: LibraryView }) { const router = useRouter(); const params = useSearchParams(); const [, start] = useTransition(); function set(view: LibraryView) { if (view === current) return; const sp = new URLSearchParams(params.toString()); if (view === "portrait") sp.set("view", "portrait"); else sp.delete("view"); const y = typeof window !== "undefined" ? window.scrollY : 0; start(() => { const qs = sp.toString(); router.push(qs ? `?${qs}` : `?`, { scroll: false }); requestAnimationFrame(() => window.scrollTo({ top: y, left: 0, behavior: "instant" as ScrollBehavior })); }); } return (