refactor: split hooks types and utils by domain

This commit is contained in:
Tom Boullay
2026-04-30 11:49:18 +02:00
parent 9ac5844182
commit b1187b68ae
65 changed files with 83 additions and 84 deletions
+52
View File
@@ -0,0 +1,52 @@
import type { MapNode, SceneData } from "@/types/editor/editor";
import { parseMapNodes } from "@/utils/map/mapNodeValidation";
const MAP_JSON_PATH = "/map.json";
const MODEL_FILE_NAMES = ["model.glb", "model.gltf"];
const HTML_CONTENT_TYPE = "text/html";
type ModelEntry = [modelName: string, modelUrl: string];
export async function loadMapSceneData(): Promise<SceneData | null> {
const response = await fetch(MAP_JSON_PATH);
if (!response.ok) {
return null;
}
const mapNodes = parseMapNodes(await response.json());
return createSceneData(mapNodes);
}
async function createSceneData(mapNodes: MapNode[]): Promise<SceneData> {
const models = await loadMapModelUrls(mapNodes);
return { mapNodes, models };
}
async function loadMapModelUrls(
mapNodes: MapNode[],
): Promise<Map<string, string>> {
const uniqueModelNames = [...new Set(mapNodes.map((node) => node.name))];
const modelEntries = await Promise.all(
uniqueModelNames.map((modelName) => loadModelEntry(modelName)),
);
return new Map(modelEntries.filter((entry) => entry !== null));
}
async function loadModelEntry(modelName: string): Promise<ModelEntry | null> {
for (const fileName of MODEL_FILE_NAMES) {
const modelUrl = `/models/${modelName}/${fileName}`;
try {
const response = await fetch(modelUrl, { method: "HEAD" });
const contentType = response.headers.get("content-type") ?? "";
if (response.ok && !contentType.includes(HTML_CONTENT_TYPE)) {
return [modelName, modelUrl];
}
} catch {
continue;
}
}
return null;
}
+32
View File
@@ -0,0 +1,32 @@
import type { MapNode } from "@/types/editor/editor";
function isVector3Tuple(value: unknown): value is [number, number, number] {
return (
Array.isArray(value) &&
value.length === 3 &&
value.every((item) => typeof item === "number" && Number.isFinite(item))
);
}
export function isMapNode(value: unknown): value is MapNode {
if (typeof value !== "object" || value === null) {
return false;
}
const node = value as Record<string, unknown>;
return (
typeof node.name === "string" &&
typeof node.type === "string" &&
isVector3Tuple(node.position) &&
isVector3Tuple(node.rotation) &&
isVector3Tuple(node.scale)
);
}
export function parseMapNodes(value: unknown): MapNode[] {
if (!Array.isArray(value) || !value.every(isMapNode)) {
throw new Error("Invalid map node data");
}
return value;
}