2025-09-27 14:30:16 -04:00
|
|
|
|
// components/portal/SettingsSwitcher.tsx
|
|
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
2025-09-28 01:38:08 -04:00
|
|
|
|
// Existing canonical pages
|
2025-09-27 15:28:02 -04:00
|
|
|
|
const FiberPanel = dynamic(() => import("@/app/settings/fiber/page"), { ssr: false });
|
|
|
|
|
|
const UVPanel = dynamic(() => import("@/app/settings/uv/page"), { ssr: false });
|
|
|
|
|
|
const CO2GalvoPanel = dynamic(() => import("@/app/settings/co2-galvo/page"), { ssr: false });
|
2025-09-27 14:30:16 -04:00
|
|
|
|
const CO2GantryPanel = dynamic(() => import("@/app/settings/co2-gantry/page"), { ssr: false });
|
|
|
|
|
|
|
2025-09-28 01:38:08 -04:00
|
|
|
|
// NEW: embed the submission form in the "Add" tab
|
|
|
|
|
|
const SettingsSubmit = dynamic(
|
2025-09-29 14:15:13 -04:00
|
|
|
|
() => import("@/components/forms/SettingsSubmit"),
|
2025-09-28 01:38:08 -04:00
|
|
|
|
{ ssr: false }
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
type DataTab = "fiber" | "uv" | "co2-gantry" | "co2-galvo";
|
|
|
|
|
|
type Tab = DataTab | "add";
|
|
|
|
|
|
|
|
|
|
|
|
const TABS: { key: Tab; label: string }[] = [
|
2025-09-27 15:28:02 -04:00
|
|
|
|
{ key: "fiber", label: "Fiber" },
|
|
|
|
|
|
{ key: "uv", label: "UV" },
|
2025-09-27 14:30:16 -04:00
|
|
|
|
{ key: "co2-gantry", label: "CO₂ Gantry" },
|
2025-09-27 15:28:02 -04:00
|
|
|
|
{ key: "co2-galvo", label: "CO₂ Galvo" },
|
2025-09-28 01:38:08 -04:00
|
|
|
|
{ key: "add", label: "Add Setting" },
|
2025-09-27 14:30:16 -04:00
|
|
|
|
];
|
|
|
|
|
|
|
2025-09-28 01:38:08 -04:00
|
|
|
|
const isDataTab = (k: string): k is DataTab =>
|
|
|
|
|
|
k === "fiber" || k === "uv" || k === "co2-gantry" || k === "co2-galvo";
|
|
|
|
|
|
|
|
|
|
|
|
const tabToTarget: Record<DataTab,
|
|
|
|
|
|
"settings_fiber" | "settings_uv" | "settings_co2gan" | "settings_co2gal"
|
|
|
|
|
|
> = {
|
|
|
|
|
|
fiber: "settings_fiber",
|
|
|
|
|
|
uv: "settings_uv",
|
|
|
|
|
|
"co2-gantry": "settings_co2gan",
|
|
|
|
|
|
"co2-galvo": "settings_co2gal",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function Panel({ tab, lastDataTab }: { tab: Tab; lastDataTab: DataTab }) {
|
2025-09-27 14:30:16 -04:00
|
|
|
|
switch (tab) {
|
2025-09-27 15:28:02 -04:00
|
|
|
|
case "fiber": return <FiberPanel />;
|
|
|
|
|
|
case "uv": return <UVPanel />;
|
|
|
|
|
|
case "co2-galvo": return <CO2GalvoPanel />;
|
2025-09-27 14:30:16 -04:00
|
|
|
|
case "co2-gantry": return <CO2GantryPanel />;
|
2025-09-27 15:28:02 -04:00
|
|
|
|
case "add":
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="rounded-md border p-4 space-y-3">
|
|
|
|
|
|
<h3 className="font-semibold">Add Setting</h3>
|
2025-09-28 01:38:08 -04:00
|
|
|
|
<SettingsSubmit initialTarget={tabToTarget[lastDataTab]} />
|
2025-09-27 15:28:02 -04:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
default:
|
|
|
|
|
|
return <FiberPanel />;
|
2025-09-27 14:30:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function SettingsSwitcher() {
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
const sp = useSearchParams();
|
|
|
|
|
|
|
2025-09-28 01:38:08 -04:00
|
|
|
|
const activeRaw = (sp.get("t") || "fiber").toLowerCase();
|
|
|
|
|
|
const active: Tab = (TABS.some(t => t.key === activeRaw) ? activeRaw : "fiber") as Tab;
|
|
|
|
|
|
|
|
|
|
|
|
// last data tab is taken from ?last=… (or fallback to current active if it’s a data tab)
|
|
|
|
|
|
const lastParam = (sp.get("last") || (isDataTab(active) ? active : "fiber")).toLowerCase();
|
|
|
|
|
|
const lastDataTab: DataTab = isDataTab(lastParam) ? lastParam : "fiber";
|
|
|
|
|
|
|
|
|
|
|
|
function setTab(nextKey: Tab) {
|
2025-09-27 14:30:16 -04:00
|
|
|
|
const q = new URLSearchParams(sp.toString());
|
|
|
|
|
|
q.set("t", nextKey);
|
2025-09-28 01:38:08 -04:00
|
|
|
|
|
|
|
|
|
|
// keep track of last data tab so the Add tab knows which target to preselect
|
|
|
|
|
|
if (nextKey === "add") {
|
|
|
|
|
|
q.set("last", isDataTab(active) ? active : lastDataTab);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
q.set("last", nextKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// optional: clear detail id if switching away from a data tab you don’t want to show
|
|
|
|
|
|
// (leaving as-is preserves your existing behavior)
|
2025-09-27 14:30:16 -04:00
|
|
|
|
router.replace(`/portal/laser-settings?${q.toString()}`, { scroll: false });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div className="mb-4 flex flex-wrap items-center gap-2">
|
|
|
|
|
|
{TABS.map(({ key, label }) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={key}
|
|
|
|
|
|
onClick={() => setTab(key)}
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"rounded-md border px-3 py-1.5 text-sm transition",
|
|
|
|
|
|
active === key ? "bg-primary text-primary-foreground" : "hover:bg-muted"
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{label}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="rounded-md border p-4">
|
2025-09-28 01:38:08 -04:00
|
|
|
|
<Panel tab={active} lastDataTab={lastDataTab} />
|
2025-09-27 14:30:16 -04:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|