makearmy-app/app/settings/uv/page.tsx

264 lines
8 KiB
TypeScript
Raw Normal View History

2025-09-22 10:37:53 -04:00
"use client";
import { useEffect, useState, useMemo } from "react";
import { useSearchParams } from "next/navigation";
import Link from "next/link";
import Image from "next/image";
2025-09-27 23:49:35 -04:00
type Owner = {
id?: string | number;
first_name?: string | null;
last_name?: string | null;
email?: string | null;
};
2025-09-22 10:37:53 -04:00
export default function UVSettingsPage() {
const searchParams = useSearchParams();
const initialQuery = searchParams.get("query") || "";
const [query, setQuery] = useState(initialQuery);
const [debouncedQuery, setDebouncedQuery] = useState(initialQuery);
const [settings, setSettings] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
2025-09-27 16:52:05 -04:00
const detailHref = (id: string | number) => `/settings/uv/${id}`;
2025-09-22 10:37:53 -04:00
useEffect(() => {
2025-09-27 23:34:35 -04:00
const t = setTimeout(() => setDebouncedQuery(query), 300);
return () => clearTimeout(t);
2025-09-22 10:37:53 -04:00
}, [query]);
useEffect(() => {
2025-09-27 23:34:35 -04:00
const url =
2025-09-27 23:49:35 -04:00
`${process.env.NEXT_PUBLIC_API_BASE_URL}/items/settings_uv` +
`?fields=` +
2025-09-27 23:34:35 -04:00
[
"submission_id",
"setting_title",
"uploader",
2025-09-27 23:49:35 -04:00
// owner (M2O) minimal, safe fields
"owner.id",
2025-09-27 23:34:35 -04:00
"owner.first_name",
"owner.last_name",
"owner.email",
"photo.id",
"photo.title",
"mat.name",
"mat_coat.name",
"source.model",
"lens.field_size",
].join(",") +
2025-09-27 23:49:35 -04:00
`&limit=-1`;
2025-09-27 23:34:35 -04:00
fetch(url, { cache: "no-store" })
2025-09-27 23:49:35 -04:00
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
})
.then((data) => setSettings(data?.data || []))
.catch((e) => {
console.error("UV settings fetch failed:", e);
setSettings([]);
2025-09-27 16:52:05 -04:00
})
2025-09-27 23:49:35 -04:00
.finally(() => setLoading(false));
2025-09-22 10:37:53 -04:00
}, []);
2025-09-27 23:49:35 -04:00
const ownerLabel = (o?: Owner) => {
if (!o) return "—";
const name = [o.first_name, o.last_name].filter(Boolean).join(" ").trim();
return name || o.email || "—";
2025-09-27 23:34:35 -04:00
};
2025-09-27 16:52:05 -04:00
const highlight = (text?: string) => {
if (!debouncedQuery) return text || "";
2025-09-22 10:37:53 -04:00
const regex = new RegExp(`(${debouncedQuery})`, "gi");
2025-09-27 16:52:05 -04:00
return (text || "").replace(regex, "<mark>$1</mark>");
2025-09-22 10:37:53 -04:00
};
const filtered = useMemo(() => {
const q = debouncedQuery.toLowerCase();
return settings.filter((entry) => {
const fieldsToSearch = [
entry.setting_title,
2025-09-27 23:49:35 -04:00
ownerLabel(entry.owner),
2025-09-27 23:34:35 -04:00
entry.uploader,
entry.mat?.name,
entry.mat_coat?.name,
entry.source?.model,
2025-09-27 23:49:35 -04:00
entry.lens?.field_size,
2025-09-22 10:37:53 -04:00
];
2025-09-27 16:52:05 -04:00
return fieldsToSearch
.filter(Boolean)
.some((field: string) => String(field).toLowerCase().includes(q));
2025-09-22 10:37:53 -04:00
});
}, [settings, debouncedQuery]);
2025-09-27 23:49:35 -04:00
const total = settings.length;
2025-09-27 16:52:05 -04:00
const uniqueMaterials = new Set(settings.map((s) => s.mat?.name).filter(Boolean)).size;
2025-09-27 23:49:35 -04:00
const lensCounts = settings.reduce((acc: Record<string, number>, cur) => {
const v = cur.lens?.field_size;
if (!v) return acc;
acc[v] = (acc[v] || 0) + 1;
2025-09-22 10:37:53 -04:00
return acc;
}, {});
2025-09-27 16:52:05 -04:00
const mostCommonLens =
2025-09-27 23:49:35 -04:00
Object.entries(lensCounts).sort((a, b) => (Number(b[1]) || 0) - (Number(a[1]) || 0))[0]?.[0] ||
2025-09-27 23:34:35 -04:00
"—";
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
const srcCounts = settings.reduce((acc: Record<string, number>, cur) => {
const v = cur.source?.model;
if (!v) return acc;
acc[v] = (acc[v] || 0) + 1;
2025-09-27 23:34:35 -04:00
return acc;
}, {});
const mostCommonSource =
2025-09-27 23:49:35 -04:00
Object.entries(srcCounts).sort((a, b) => (Number(b[1]) || 0) - (Number(a[1]) || 0))[0]?.[0] ||
2025-09-27 23:34:35 -04:00
"—";
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
const recent = [...settings]
2025-09-27 23:34:35 -04:00
.sort((a, b) => Number(b.submission_id) - Number(a.submission_id))
.slice(0, 5);
2025-09-22 10:37:53 -04:00
2025-09-27 23:34:35 -04:00
return (
<div className="p-6 max-w-7xl mx-auto">
<style jsx global>{`
mark {
background: #ffde59;
color: #242424;
padding: 0 2px;
border-radius: 2px;
}
`}</style>
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
{/* Header / Search */}
2025-09-27 23:34:35 -04:00
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4 mb-6">
<div className="card bg-card text-card-foreground p-4">
<h1 className="text-2xl font-bold mb-2">UV Laser Settings</h1>
<input
type="search"
value={query}
onChange={(e) => setQuery(e.target.value)}
2025-09-27 23:49:35 -04:00
placeholder="Search by material, owner, uploader, model, lens…"
2025-09-27 23:34:35 -04:00
className="w-full mb-4 dark:bg-background border border-border rounded-md p-2"
/>
<p className="text-sm text-muted-foreground">
2025-09-27 23:49:35 -04:00
View and explore detailed UV laser settings with context.
2025-09-27 23:34:35 -04:00
</p>
</div>
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
{/* How to use */}
2025-09-27 23:34:35 -04:00
<div className="card bg-card text-card-foreground p-4">
<h2 className="text-lg font-semibold mb-2">How to Use</h2>
<p className="text-sm">
2025-09-27 23:49:35 -04:00
Browse community UV settings. Use search to narrow results. Click a row to view full configuration,
notes, and photos.
2025-09-27 23:34:35 -04:00
</p>
</div>
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
{/* Stats */}
2025-09-27 23:34:35 -04:00
<div className="card bg-card text-card-foreground p-4">
<h2 className="text-lg font-semibold mb-2">Stats Summary</h2>
<ul className="text-sm space-y-1">
2025-09-27 23:49:35 -04:00
<li>Total Settings: {total}</li>
2025-09-27 23:34:35 -04:00
<li>Unique Materials: {uniqueMaterials}</li>
<li>Most Common Lens: {mostCommonLens}</li>
<li>Most Used Source: {mostCommonSource}</li>
</ul>
</div>
2025-09-22 10:37:53 -04:00
2025-09-27 23:49:35 -04:00
{/* Recently Added */}
<div className="card bg-card text-card-foreground p-4 xl:col-span-3">
2025-09-27 23:34:35 -04:00
<h2 className="text-lg font-semibold mb-2">Recently Added</h2>
<ul className="text-sm space-y-1">
2025-09-27 23:49:35 -04:00
{recent.map((s) => (
2025-09-27 23:34:35 -04:00
<li key={s.submission_id}>
<Link href={detailHref(s.submission_id)} className="underline text-accent">
{s.setting_title || "Untitled"}
</Link>{" "}
<span className="text-muted-foreground">
2025-09-27 23:49:35 -04:00
by {ownerLabel(s.owner)}{s.uploader ? ` (uploader: ${s.uploader})` : ""}
2025-09-27 23:34:35 -04:00
</span>
</li>
))}
</ul>
</div>
</div>
2025-09-22 10:37:53 -04:00
2025-09-27 23:34:35 -04:00
{/* Table */}
{loading ? (
<p className="text-muted">Loading settings...</p>
) : filtered.length === 0 ? (
<p className="text-muted">No UV settings found.</p>
) : (
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr>
<th className="px-2 py-2 text-left">Photo</th>
<th className="px-2 py-2 text-left">Title</th>
<th className="px-2 py-2 text-left">Owner</th>
<th className="px-2 py-2 text-left">Uploader</th>
<th className="px-2 py-2 text-left">Material</th>
<th className="px-2 py-2 text-left">Coating</th>
<th className="px-2 py-2 text-left">Source</th>
<th className="px-2 py-2 text-left">Lens</th>
</tr>
</thead>
<tbody>
2025-09-27 23:49:35 -04:00
{filtered.map((s) => (
<tr key={s.submission_id} className="border-t border-border">
<td className="px-2 py-2">
{s.photo?.id ? (
<Image
src={`https://forms.lasereverything.net/assets/${s.photo.id}`}
alt={s.photo.title || "laser preview"}
width={64}
height={64}
className="rounded-md"
2025-09-27 16:52:05 -04:00
/>
2025-09-27 23:49:35 -04:00
) : (
"—"
)}
</td>
<td className="px-2 py-2 whitespace-nowrap">
<Link
href={detailHref(s.submission_id)}
className="text-accent underline"
dangerouslySetInnerHTML={{ __html: highlight(s.setting_title || "—") }}
/>
</td>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(ownerLabel(s.owner)) }}
/>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(s.uploader || "—") }}
/>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(s.mat?.name || "—") }}
/>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(s.mat_coat?.name || "—") }}
/>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(s.source?.model || "—") }}
/>
<td
className="px-2 py-2 whitespace-nowrap"
dangerouslySetInnerHTML={{ __html: highlight(s.lens?.field_size || "—") }}
/>
</tr>
))}
2025-09-27 23:34:35 -04:00
</tbody>
</table>
2025-09-27 16:52:05 -04:00
</div>
2025-09-27 23:34:35 -04:00
)}
</div>
);
2025-09-22 10:37:53 -04:00
}