Files

64 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2025-09-03 20:41:25 -04:00
"use client";
2025-09-29 19:57:02 -06:00
import { usePathname, useSearchParams } from "next/navigation";
2025-09-28 01:59:16 -06:00
import { useRouter } from "next/navigation";
2025-09-03 20:41:25 -04:00
import { useState, ReactNode } from "react";
interface ToggleOption {
key: string;
label: string;
2026-02-25 19:27:04 -06:00
content: ReactNode | (() => ReactNode);
2026-06-03 19:55:29 -05:00
onClick?: () => void;
2025-09-03 20:41:25 -04:00
}
interface ToggleProps {
options: ToggleOption[];
defaultView?: string;
}
export default function Toggle({ options, defaultView }: ToggleProps) {
2025-09-28 01:59:16 -06:00
const router = useRouter();
const searchParams = useSearchParams();
2025-09-29 19:57:02 -06:00
const pathname = usePathname();
2025-09-03 20:41:25 -04:00
const [view, setView] = useState(defaultView || options[0].key);
2025-09-28 01:59:16 -06:00
const handleClick = (key: string) => {
setView(key);
const params = new URLSearchParams(searchParams.toString());
params.set("key", key);
2026-01-27 13:47:38 -06:00
params.delete("machine");
2025-09-28 01:59:16 -06:00
2025-09-29 19:57:02 -06:00
router.replace(`${pathname}?${params.toString()}`);
2025-09-28 01:59:16 -06:00
};
2025-10-14 14:27:03 -06:00
2025-09-03 20:41:25 -04:00
return (
<section className="toggleSection">
<div className="toggleGroup">
{options.map((opt) => (
<button
key={opt.key}
className={`toggleButton ${view === opt.key ? "active" : ""}`}
2025-09-28 01:59:16 -06:00
onClick={() => handleClick(opt.key)}
2025-09-03 20:41:25 -04:00
>
{opt.label}
</button>
))}
</div>
2025-10-14 14:27:03 -06:00
<div className="padding toggleContent">
2026-02-25 19:27:04 -06:00
{(() => {
const active = options.find((opt) => opt.key === view)?.content;
if (typeof active === "function") {
return active();
}
return active;
})()}
2025-09-03 20:41:25 -04:00
</div>
</section>
);
}
2025-10-14 14:27:03 -06:00
//IO