"use client"; import { useMemo } from "react"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; /** * PlatformSwitcher * * Works on BOTH: * - /parts/[partRole] (default browse route) * - /parts/[platform]/[partRole] (canonical route) * * Behavior: * - If you're on /parts/[partRole], switching platform navigates to /parts/[platform]/[partRole] * (so your URL becomes explicit/shareable). * - If you're already on /parts/[platform]/[partRole], it swaps the platform segment. */ export default function PlatformSwitcher(props: { currentPlatform: string; partRole: string; // If true, we keep query params when switching (nice for sort/paging) preserveQuery?: boolean; }) { const { currentPlatform, partRole, preserveQuery = true } = props; const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const queryString = useMemo(() => { if (!preserveQuery) return ""; const q = searchParams?.toString(); return q ? `?${q}` : ""; }, [searchParams, preserveQuery]); function go(platform: string) { // We always navigate to canonical to keep it simple & consistent router.push(`/parts/${encodeURIComponent(platform)}/${encodeURIComponent(partRole)}${queryString}`); } return (