Moved default maps to being stored in the database to stop overriding
This commit is contained in:
parent
10c259a6b3
commit
6f2e883e74
@ -22,33 +22,50 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
|
|||||||
const [imageLoading, setImageLoading] = useState(false);
|
const [imageLoading, setImageLoading] = useState(false);
|
||||||
|
|
||||||
const [currentMap, setCurrentMap] = useState(null);
|
const [currentMap, setCurrentMap] = useState(null);
|
||||||
const [maps, setMaps] = useState(Object.values(defaultMaps));
|
const [maps, setMaps] = useState([]);
|
||||||
// Load maps from the database and ensure state is properly setup
|
// Load maps from the database and ensure state is properly setup
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function loadMaps() {
|
async function loadDefaultMaps() {
|
||||||
let storedMaps = await db.table("maps").toArray();
|
const defaultMapsWithIds = [];
|
||||||
// reverse so maps are show in the order they were added
|
const defaultMapStates = [];
|
||||||
storedMaps.reverse();
|
// Store the default maps into the db in reverse so the whie map is first
|
||||||
for (let map of storedMaps) {
|
// in the UI
|
||||||
// Recreate image urls for each map
|
const defaultMapArray = Object.values(defaultMaps).reverse();
|
||||||
map.source = URL.createObjectURL(map.file);
|
for (let i = 0; i < defaultMapArray.length; i++) {
|
||||||
|
const defaultMap = defaultMapArray[i];
|
||||||
|
const id = `${defaultMap.id}--${shortid.generate()}`;
|
||||||
|
defaultMapsWithIds.push({
|
||||||
|
...defaultMap,
|
||||||
|
id,
|
||||||
|
timestamp: Date.now() + i,
|
||||||
|
});
|
||||||
|
defaultMapStates.push({ ...defaultMapState, mapId: id });
|
||||||
}
|
}
|
||||||
setMaps((prevMaps) => [...storedMaps, ...prevMaps]);
|
await db.table("maps").bulkAdd(defaultMapsWithIds);
|
||||||
|
await db.table("states").bulkAdd(defaultMapStates);
|
||||||
|
setMaps(defaultMapsWithIds.sort((a, b) => b.timestamp - a.timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setupDefaultMapStatesIfNeeded() {
|
async function loadMaps() {
|
||||||
for (let defaultMap of Object.values(defaultMaps)) {
|
let storedMaps = await db.table("maps").toArray();
|
||||||
let state = await db.table("states").get(defaultMap.id);
|
|
||||||
if (!state) {
|
// If we have no stored maps load the default maps
|
||||||
await db
|
if (storedMaps.length === 0) {
|
||||||
.table("states")
|
loadDefaultMaps();
|
||||||
.add({ ...defaultMapState, mapId: defaultMap.id });
|
} else {
|
||||||
|
// Sort maps by the time they were added
|
||||||
|
storedMaps.sort((a, b) => b.timestamp - a.timestamp);
|
||||||
|
for (let map of storedMaps) {
|
||||||
|
// Recreate image urls for file based maps
|
||||||
|
if (map.file) {
|
||||||
|
map.source = URL.createObjectURL(map.file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
setMaps(storedMaps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadMaps();
|
loadMaps();
|
||||||
setupDefaultMapStatesIfNeeded();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const [gridX, setGridX] = useState(defaultMapSize);
|
const [gridX, setGridX] = useState(defaultMapSize);
|
||||||
@ -87,6 +104,7 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
|
|||||||
height: image.height,
|
height: image.height,
|
||||||
source: url,
|
source: url,
|
||||||
id: shortid.generate(),
|
id: shortid.generate(),
|
||||||
|
timestamp: Date.now(),
|
||||||
});
|
});
|
||||||
setImageLoading(false);
|
setImageLoading(false);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user