lens display fix

This commit is contained in:
makearmy 2025-09-22 14:18:08 -04:00
parent 0e34a6433b
commit 048045bcb5

View file

@ -1,57 +1,98 @@
// app/app/api/options/lens/route.ts // app/api/options/lens/route.ts
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { directusFetch } from "@/lib/directus"; import { directusFetch } from "@/lib/directus";
// Decide which collection to read based on target /**
function lensCollectionForTarget(target?: string) { * Lenses endpoint
if (target === "settings_co2gan") return "laser_focus_lens"; // gantry * - Fiber/UV/CO2 Galvo -> laser_scan_lens (has field_size, focal_length)
// fiber, uv, co2gal → scan lenses * - CO2 Gantry -> laser_focus_lens (usually focal_length only)
return "laser_scan_lens"; * Returns [{ id, label }] using submission_id || id.
*/
function collectionForTarget(
target?: string
): "laser_scan_lens" | "laser_focus_lens" | null {
switch (target) {
case "settings_fiber":
case "settings_uv":
case "settings_co2gal":
return "laser_scan_lens";
case "settings_co2gan":
return "laser_focus_lens";
default:
return null;
}
} }
export async function GET(req: Request) { export async function GET(req: Request) {
try { try {
const { searchParams } = new URL(req.url); const { searchParams } = new URL(req.url);
const target = searchParams.get("target") || undefined; const target = searchParams.get("target") || undefined;
const q = (searchParams.get("q") || "").trim().toLowerCase(); const q = (searchParams.get("q") || "").toLowerCase().trim();
const limit = Number(searchParams.get("limit") || "500"); const limit = Number(searchParams.get("limit") || "500");
const coll = lensCollectionForTarget(target); const coll = collectionForTarget(target);
if (!coll) return NextResponse.json({ data: [] });
// Request both possible PK fields, plus name & focal_length for labels // Ask only for fields that exist on each collection
const url = `/items/${coll}?fields=submission_id,id,name,focal_length&limit=${limit}`; const fields =
const { data } = await directusFetch<{ data: any[] }>(url); coll === "laser_scan_lens"
const list = Array.isArray(data) ? data : []; ? "submission_id,id,name,field_size,focal_length"
: "submission_id,id,name,focal_length";
const url = `/items/${coll}?fields=${encodeURIComponent(
fields
)}&limit=${limit}`;
const res = await directusFetch<{ data: any[] }>(url);
const list = Array.isArray(res?.data) ? res.data : [];
const mapped = list.map((x) => { const mapped = list.map((x) => {
const id = String(x.submission_id ?? x.id); const id = String(x?.submission_id ?? x?.id);
const label =
(x.name && String(x.name)) || const fieldSizeRaw = x?.field_size;
(x.focal_length != null ? `F${x.focal_length} mm` : id); const fieldSize =
const key = fieldSizeRaw !== null && fieldSizeRaw !== undefined
(x.focal_length != null ? String(x.focal_length).padStart(6, "0") : "") + ? String(fieldSizeRaw).trim()
" " + : "";
label.toLowerCase();
return { id, label, key, focal: Number(x.focal_length ?? NaN) }; const fnum = Number(x?.focal_length);
const focalTxt = Number.isFinite(fnum) ? `F${fnum} mm` : "";
// Label: field_size THEN focal_length (requested order).
// Fall back to name, then id.
let label = [fieldSize, focalTxt].filter(Boolean).join(" — ");
if (!label) label = (x?.name ? String(x.name) : "").trim();
if (!label) label = id;
// Sort by focal length if available; else by label
const sortKey: number | string = Number.isFinite(fnum)
? fnum
: label.toLowerCase();
return {
id,
label,
sortKey,
_search: `${id} ${label} ${fieldSize} ${x?.name ?? ""} ${
x?.focal_length ?? ""
}`.toLowerCase(),
};
}); });
// Optional text filter const filtered = q ? mapped.filter((m) => m._search.includes(q)) : mapped;
const filtered = q
? mapped.filter((m) => m.label.toLowerCase().includes(q))
: mapped;
// Prefer numeric focal_length ordering, fallback to label
filtered.sort((a, b) => { filtered.sort((a, b) => {
const aNum = Number.isFinite(a.focal); if (typeof a.sortKey === "number" && typeof b.sortKey === "number")
const bNum = Number.isFinite(b.focal); return a.sortKey - b.sortKey;
if (aNum && bNum) return a.focal - b.focal; return String(a.sortKey).localeCompare(String(b.sortKey));
if (aNum) return -1;
if (bNum) return 1;
return a.key.localeCompare(b.key);
}); });
return NextResponse.json({ data: filtered.map(({ id, label }) => ({ id, label })) }); return NextResponse.json({
data: filtered.map(({ id, label }) => ({ id, label })),
});
} catch (e: any) { } catch (e: any) {
return NextResponse.json({ error: e?.message || "Failed to load lens options" }, { status: 500 }); console.error("[options/lens] error:", e?.message || e);
// Fail soft to keep UI responsive
return NextResponse.json({ data: [] });
} }
} }