refactor: extract map draft state helpers
This commit is contained in:
parent
f397f19e48
commit
f25307cb47
@ -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);
|
||||
|
||||
39
frontend/src/lib/locationMapDraftStateUtils.js
Normal file
39
frontend/src/lib/locationMapDraftStateUtils.js
Normal file
@ -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,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user