diff --git a/src/app/(payload)/admin/importMap.js b/src/app/(payload)/admin/importMap.js index 7b501a57..71df4d78 100644 --- a/src/app/(payload)/admin/importMap.js +++ b/src/app/(payload)/admin/importMap.js @@ -34,6 +34,7 @@ import { default as default_701da9bef3b3cc3d8567adf41fa08aa3 } from '@/component import { default as default_4b88a624e9f0b376cffcc6814d459adb } from '@/components/admin/assignments/AssignmentMembersField' import { default as default_0edde40a6ccd30c893b1227360f69360 } from '@/components/static/TickDurationDescription' import { default as default_2d468cc5cf0b647ffca3201592c20825 } from '@/components/static/WarningMessage' +import { default as default_8e873aa39584420378774e6c9c4ac54b } from '@/components/admin/VehicleLibraryPicker' import { AudioPreviewField as AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2 } from '@/components/admin/voiceover/AudioPreviewField' import { default as default_7fa52484aec46e988e9cfb5e86c0c759 } from '@/components/admin/narrative-flow/NarrativeFlowEditor' import { default as default_b7f86fb8e028bca592f57e6d387a0380 } from '@/components/admin/minigames/PeriodCloseControl' @@ -89,6 +90,7 @@ export const importMap = { "@/components/admin/assignments/AssignmentMembersField#default": default_4b88a624e9f0b376cffcc6814d459adb, "@/components/static/TickDurationDescription#default": default_0edde40a6ccd30c893b1227360f69360, "@/components/static/WarningMessage#default": default_2d468cc5cf0b647ffca3201592c20825, + "@/components/admin/VehicleLibraryPicker#default": default_8e873aa39584420378774e6c9c4ac54b, "@/components/admin/voiceover/AudioPreviewField#AudioPreviewField": AudioPreviewField_6bccef9bf4ea41ed573ab9f36fa579b2, "@/components/admin/narrative-flow/NarrativeFlowEditor#default": default_7fa52484aec46e988e9cfb5e86c0c759, "@/components/admin/minigames/PeriodCloseControl#default": default_b7f86fb8e028bca592f57e6d387a0380, diff --git a/src/collections/logistics/Vehicles.ts b/src/collections/logistics/Vehicles.ts index 8cb881d8..1a367917 100644 --- a/src/collections/logistics/Vehicles.ts +++ b/src/collections/logistics/Vehicles.ts @@ -19,6 +19,17 @@ export const Vehicles: CollectionConfig = { delete: requireLogisticsPermission("vehicles:delete"), }, fields: [ + { + // D11b: searchable imported-library picker (name, classname, thumbnail) + // that copies an imported entry into this form on click. + name: "libraryPicker", + type: "ui", + admin: { + components: { + Field: "@/components/admin/VehicleLibraryPicker", + }, + }, + }, { label: "Identity", type: "group", diff --git a/src/components/admin/VehicleLibraryPicker.tsx b/src/components/admin/VehicleLibraryPicker.tsx new file mode 100644 index 00000000..1be7c2ab --- /dev/null +++ b/src/components/admin/VehicleLibraryPicker.tsx @@ -0,0 +1,135 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { SearchIcon, TruckIcon } from "lucide-react"; +import { toast, useField } from "@payloadcms/ui"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; + +/** + * Imported library picker for vehicle templates (D11b). Searches the vehicles + * collection (the rows importArmaLibrary maintains) by display name or + * classname with thumbnails, and copies the chosen entry's name + classname + * into the open document so officers never hand-type classnames. Applies only + * on explicit click; nothing is written automatically. + */ + +interface LibraryEntry { + id: number; + name?: string | null; + classname?: string | null; + transportMode?: string | null; + image?: { id?: number; url?: string | null } | string | number | null; +} + +export function VehicleLibraryPicker() { + const nameField = useField({ path: "name" }); + const classnameField = useField({ path: "classname" }); + const imageField = useField({ path: "image" }); + const [query, setQuery] = useState(""); + const [results, setResults] = useState([]); + const [searching, setSearching] = useState(false); + + useEffect(() => { + const q = query.trim(); + if (q.length < 2) { + setResults([]); + return; + } + let cancelled = false; + setSearching(true); + const where = encodeURIComponent( + JSON.stringify({ or: [{ name: { like: q } }, { classname: { like: q } }] }), + ); + const timer = setTimeout(async () => { + try { + const res = await fetch(`/api/vehicles?where=${where}&limit=12&depth=1`, { + credentials: "include", + }); + const json = (await res.json()) as { docs?: LibraryEntry[] }; + if (!cancelled) setResults(Array.isArray(json.docs) ? json.docs : []); + } catch { + if (!cancelled) setResults([]); + } finally { + if (!cancelled) setSearching(false); + } + }, 250); + return () => { + cancelled = true; + clearTimeout(timer); + }; + }, [query]); + + function apply(entry: LibraryEntry) { + nameField.setValue(entry.name ?? ""); + classnameField.setValue(entry.classname ?? ""); + // Fill the thumbnail only when this template has none yet. + if (imageField.value == null && entry.image != null && entry.image !== "") { + const mediaId = + typeof entry.image === "object" ? entry.image.id : Number(entry.image); + if (mediaId != null && Number.isFinite(Number(mediaId))) { + imageField.setValue(Number(mediaId)); + } + } + toast.success(`Loaded ${entry.name || entry.classname} into this template.`); + } + + return ( +
+
+

Imported library

+ + Search imported Arma vehicle templates and copy them into this form + +
+
+ + setQuery(e.target.value)} + placeholder="Search by name or classname (2+ characters)..." + className="pl-8" + /> +
+ {searching &&

Searching...

} + {!searching && query.trim().length >= 2 && results.length === 0 && ( +

+ Nothing found. Run the catalog import to add it first. +

+ )} + {results.length > 0 && ( +
    + {results.map((entry) => { + const image = entry.image && typeof entry.image === "object" ? entry.image : null; + return ( +
  • + {image?.url ? ( + // eslint-disable-next-line @next/next/no-img-element + + ) : ( + + )} +
    +
    {entry.name || "(unnamed)"}
    +
    + {entry.classname} + {entry.transportMode ? ` · ${entry.transportMode}` : ""} +
    +
    + +
  • + ); + })} +
+ )} +
+ ); +} + +export default VehicleLibraryPicker; diff --git a/src/lib/arma-import/catalog.ts b/src/lib/arma-import/catalog.ts index efe643c3..16322449 100644 --- a/src/lib/arma-import/catalog.ts +++ b/src/lib/arma-import/catalog.ts @@ -12,8 +12,8 @@ * - For "vehicle" records `extra` is the transport mode ("air" | "sea" | * "ground"; anything else -> "ground"). * - For "magazine" records `extra` is the round count (magazine roundCapacity). - * - `mass` is accepted for contract completeness; no collection field maps to - * it yet, so it is not imported. + * - `mass` imports into `vehicles.maxMass` for vehicle records (physical mass + * from the export); asset records do not carry a matching field. * * Legacy field aliases (`class`, `category`, `name`) are still accepted. * @@ -56,7 +56,7 @@ export interface CatalogRecord { picture?: string; mod?: string; description?: string; - /** Accepted by the contract; no collection field maps to it (not imported). */ + /** Vehicles only: physical mass, imported into maxMass. */ mass?: number; /** Transport mode for vehicles, round count for magazines, otherwise "". */ extra?: number | string; @@ -82,6 +82,8 @@ export interface ImportPlan { assetType: AssetType | "vehicle"; /** Vehicles only. */ transportMode: TransportMode; + /** Vehicles only: physical mass from the export (written to maxMass). */ + mass?: number; /** Magazines only: round count from `extra`. */ roundCapacity?: number; /** Weapons only: compatibility references (classnames), linked in a second pass. */ @@ -181,6 +183,20 @@ function transportModeFromExtra(extra: number | string | undefined): TransportMo return "ground"; } +/** + * Default Vehicles.category derived from the transport mode. Only applied + * when a template has no category yet; officers refine it by hand (the export + * carries no doctrine classification). + */ +export function defaultCategoryForTransport(mode: TransportMode): "utility" | "transport" { + return mode === "ground" ? "utility" : "transport"; +} + +/** Positive finite mass from a catalog record, undefined otherwise. */ +function positiveMass(mass: number | undefined): number | undefined { + return typeof mass === "number" && Number.isFinite(mass) && mass > 0 ? mass : undefined; +} + /** * Plan one catalog record (pure — no filesystem or database access). * Returns undefined for records without a usable classname. @@ -196,6 +212,7 @@ export function planRecord(rec: CatalogRecord, lookups: PlanLookups = {}): Impor const pictureRef = lookupIcon(lookups.icons, className) || String(rec.picture ?? "").trim(); if (rawType === "vehicle" || (!ASSET_TYPE_SET.has(rawType) && looksLikeVehicle(className, rawType))) { + const mass = positiveMass(rec.mass); return { kind: "vehicle", className, @@ -203,6 +220,7 @@ export function planRecord(rec: CatalogRecord, lookups: PlanLookups = {}): Impor description, assetType: "vehicle", transportMode: transportModeFromExtra(rec.extra), + ...(mass !== undefined ? { mass } : {}), mod, pictureRef, media: { paaPath: "", pngPath: "", screenshotPath: "" }, @@ -484,9 +502,15 @@ async function upsertVehicle(payload: Payload, plan: ImportPlan, fuelId: number transportMode: plan.transportMode, }; if (plan.description) data.description = plaintextToLexical(plan.description); + // Physical mass is pure game data: refresh it on every import. + if (plan.mass !== undefined) data.maxMass = plan.mass; const imagePath = planImagePath(plan); // Same rule as assets: fill an empty image, never replace one that is set. - const current = existing.docs[0] as unknown as { image?: unknown }; + const current = existing.docs[0] as unknown as { image?: unknown; category?: unknown }; + // Category default only fills templates nobody has classified yet. + if (!Array.isArray(current.category) || current.category.length === 0) { + data.category = [defaultCategoryForTransport(plan.transportMode)]; + } if (imagePath && !current.image) { const media = await resolveMedia(payload, imagePath); if (media) data.image = media.id; @@ -502,6 +526,8 @@ async function upsertVehicle(payload: Payload, plan: ImportPlan, fuelId: number classname: plan.className, fuel: { fuelType: fuelId, fuelCapacity: 100, fuelConsumptionRate: 1 }, transportMode: plan.transportMode, + category: [defaultCategoryForTransport(plan.transportMode)], + ...(plan.mass !== undefined ? { maxMass: plan.mass } : {}), ...(plan.description ? { description: plaintextToLexical(plan.description) } : {}), ...(mediaId !== undefined ? { image: mediaId } : {}), };