From d029ce516b4b43e65c53b7785dd40919a6465e1c Mon Sep 17 00:00:00 2001 From: makearmy Date: Mon, 22 Sep 2025 14:51:28 -0400 Subject: [PATCH] lens bugfix --- app/api/options/lens/route.ts | 43 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/app/api/options/lens/route.ts b/app/api/options/lens/route.ts index 60c8fc2c..060d053e 100644 --- a/app/api/options/lens/route.ts +++ b/app/api/options/lens/route.ts @@ -3,11 +3,12 @@ import { NextResponse } from "next/server"; import { directusFetch } from "@/lib/directus"; /** - * Lenses endpoint - * - Fiber/UV/CO2 Galvo -> laser_scan_lens (has field_size, focal_length) - * - CO2 Gantry -> laser_focus_lens (usually focal_length only) - * Returns [{ id, label }] using submission_id || id. + * Lens options: + * - Fiber / UV / CO2 Galvo -> laser_scan_lens (field_size, focal_length) + * - CO2 Gantry -> laser_focus_lens (focal_length) + * Returns [{ id, label }] using submission_id if present, otherwise id. */ + function collectionForTarget( target?: string ): "laser_scan_lens" | "laser_focus_lens" | null { @@ -33,38 +34,35 @@ export async function GET(req: Request) { const coll = collectionForTarget(target); if (!coll) return NextResponse.json({ data: [] }); - // Ask only for fields that exist on each collection + // Important: don't request fields that don't exist. const fields = coll === "laser_scan_lens" - ? "submission_id,id,name,field_size,focal_length" - : "submission_id,id,name,focal_length"; + ? "submission_id,id,field_size,focal_length" + : "submission_id,id,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 { data } = await directusFetch<{ data: any[] }>(url); + const list = Array.isArray(data) ? data : []; - const mapped = list.map((x) => { + const items = list.map((x) => { const id = String(x?.submission_id ?? x?.id); - const fieldSizeRaw = x?.field_size; const fieldSize = - fieldSizeRaw !== null && fieldSizeRaw !== undefined - ? String(fieldSizeRaw).trim() + x?.field_size !== null && x?.field_size !== undefined + ? String(x.field_size).trim() : ""; 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. + // Label: field_size first, then focal length (requested order). 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 + // Sort numerically by focal length when available, else alpha by label. const sortKey: number | string = Number.isFinite(fnum) ? fnum : label.toLowerCase(); @@ -73,17 +71,16 @@ export async function GET(req: Request) { id, label, sortKey, - _search: `${id} ${label} ${fieldSize} ${x?.name ?? ""} ${ - x?.focal_length ?? "" - }`.toLowerCase(), + _search: `${id} ${label} ${fieldSize} ${x?.focal_length ?? ""}`.toLowerCase(), }; }); - const filtered = q ? mapped.filter((m) => m._search.includes(q)) : mapped; + const filtered = q ? items.filter((m) => m._search.includes(q)) : items; filtered.sort((a, b) => { - if (typeof a.sortKey === "number" && typeof b.sortKey === "number") + if (typeof a.sortKey === "number" && typeof b.sortKey === "number") { return a.sortKey - b.sortKey; + } return String(a.sortKey).localeCompare(String(b.sortKey)); }); @@ -92,7 +89,7 @@ export async function GET(req: Request) { }); } catch (e: any) { console.error("[options/lens] error:", e?.message || e); - // Fail soft to keep UI responsive + // Fail-soft so the UI doesn’t hang return NextResponse.json({ data: [] }); } }