"use client"; import { usePathname, useSearchParams } from "next/navigation"; import { useRouter } from "next/navigation"; import { useState, ReactNode } from "react"; interface ToggleOption { key: string; label: string; content: ReactNode | (() => ReactNode); } interface ToggleProps { options: ToggleOption[]; defaultView?: string; } export default function Toggle({ options, defaultView }: ToggleProps) { const router = useRouter(); const searchParams = useSearchParams(); const pathname = usePathname(); const [view, setView] = useState(defaultView || options[0].key); const handleClick = (key: string) => { setView(key); const params = new URLSearchParams(searchParams.toString()); params.set("key", key); params.delete("machine"); router.replace(`${pathname}?${params.toString()}`); }; return (
{options.map((opt) => ( ))}
{(() => { const active = options.find((opt) => opt.key === view)?.content; if (typeof active === "function") { return active(); } return active; })()}
); } //IO