From f25307cb475ddb770f430c2413bdaa03f2d225f9 Mon Sep 17 00:00:00 2001 From: Nico Date: Thu, 4 Jun 2026 17:14:50 -0700 Subject: [PATCH] refactor: extract map draft state helpers --- .../src/hooks/useLocationMapDraftState.js | 42 ++++--------------- .../src/lib/locationMapDraftStateUtils.js | 39 +++++++++++++++++ 2 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 frontend/src/lib/locationMapDraftStateUtils.js diff --git a/frontend/src/hooks/useLocationMapDraftState.js b/frontend/src/hooks/useLocationMapDraftState.js index a73bb43..08e3f3b 100644 --- a/frontend/src/hooks/useLocationMapDraftState.js +++ b/frontend/src/hooks/useLocationMapDraftState.js @@ -1,20 +1,8 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { getLocationMap } from "../api/locationMaps"; import getApiErrorMessage from "../lib/getApiErrorMessage"; -import { - DEFAULT_MAP_FILTERS, - DEFAULT_MAP_SIZE, - getObjectKey, - mapForMode, - normalizeMapObject, - objectsForMode, -} from "../lib/locationMapUtils"; - -const DEFAULT_DRAFT_MAP = { - name: "Store Map", - width: DEFAULT_MAP_SIZE.width, - height: DEFAULT_MAP_SIZE.height, -}; +import { getDefaultMapDraft, getMapDraftSnapshot } from "../lib/locationMapDraftStateUtils"; +import { DEFAULT_MAP_FILTERS } from "../lib/locationMapUtils"; export default function useLocationMapDraftState({ activeHousehold, @@ -31,7 +19,7 @@ export default function useLocationMapDraftState({ const [mode, setMode] = useState("setup"); const [editorTool, setEditorTool] = useState("pan"); const [previewDraft, setPreviewDraft] = useState(false); - const [mapDraft, setMapDraft] = useState(DEFAULT_DRAFT_MAP); + const [mapDraft, setMapDraft] = useState(getDefaultMapDraft); const [objects, setObjects] = useState([]); const [selectedObjectKey, setSelectedObjectKey] = useState(null); const [filters, setFilters] = useState(DEFAULT_MAP_FILTERS); @@ -50,28 +38,16 @@ export default function useLocationMapDraftState({ setHistory([]); setFuture([]); setHasUnsavedChanges(false); - setMapDraft(DEFAULT_DRAFT_MAP); + setMapDraft(getDefaultMapDraft()); }, []); const syncMapDraftFromState = useCallback((nextState, nextMode, nextPreviewDraft, options = {}) => { - const nextMap = mapForMode(nextState, nextMode, nextPreviewDraft); - const nextObjects = objectsForMode(nextState, nextMode, nextPreviewDraft); - if (!nextMap) return; - const normalizedObjects = nextObjects.map(normalizeMapObject); - const preserveSelectedIndex = Number.isInteger(options.preserveSelectedIndex) - ? options.preserveSelectedIndex - : -1; - const preservedSelectedObject = preserveSelectedIndex >= 0 - ? normalizedObjects[preserveSelectedIndex] - : null; + const snapshot = getMapDraftSnapshot(nextState, nextMode, nextPreviewDraft, options); + if (!snapshot) return; - setMapDraft({ - name: nextMap.name || "Store Map", - width: nextMap.width || DEFAULT_MAP_SIZE.width, - height: nextMap.height || DEFAULT_MAP_SIZE.height, - }); - setObjects(normalizedObjects); - setSelectedObjectKey(preservedSelectedObject ? getObjectKey(preservedSelectedObject) : null); + setMapDraft(snapshot.mapDraft); + setObjects(snapshot.objects); + setSelectedObjectKey(snapshot.selectedObjectKey); setHistory([]); setFuture([]); setHasUnsavedChanges(false); diff --git a/frontend/src/lib/locationMapDraftStateUtils.js b/frontend/src/lib/locationMapDraftStateUtils.js new file mode 100644 index 0000000..31ca3a3 --- /dev/null +++ b/frontend/src/lib/locationMapDraftStateUtils.js @@ -0,0 +1,39 @@ +import { + DEFAULT_MAP_SIZE, + getObjectKey, + mapForMode, + normalizeMapObject, + objectsForMode, +} from "./locationMapUtils"; + +export function getDefaultMapDraft() { + return { + name: "Store Map", + width: DEFAULT_MAP_SIZE.width, + height: DEFAULT_MAP_SIZE.height, + }; +} + +export function getMapDraftSnapshot(nextState, nextMode, nextPreviewDraft, options = {}) { + const nextMap = mapForMode(nextState, nextMode, nextPreviewDraft); + const nextObjects = objectsForMode(nextState, nextMode, nextPreviewDraft); + if (!nextMap) return null; + + const normalizedObjects = nextObjects.map(normalizeMapObject); + const preserveSelectedIndex = Number.isInteger(options.preserveSelectedIndex) + ? options.preserveSelectedIndex + : -1; + const preservedSelectedObject = preserveSelectedIndex >= 0 + ? normalizedObjects[preserveSelectedIndex] + : null; + + return { + mapDraft: { + name: nextMap.name || "Store Map", + width: nextMap.width || DEFAULT_MAP_SIZE.width, + height: nextMap.height || DEFAULT_MAP_SIZE.height, + }, + objects: normalizedObjects, + selectedObjectKey: preservedSelectedObject ? getObjectKey(preservedSelectedObject) : null, + }; +}