2020-05-19 02:21:01 -04:00
|
|
|
import React, { useEffect, useState, useContext } from "react";
|
2020-11-26 00:29:10 -05:00
|
|
|
import * as Comlink from "comlink";
|
2020-05-19 02:21:01 -04:00
|
|
|
|
|
|
|
import AuthContext from "./AuthContext";
|
|
|
|
import DatabaseContext from "./DatabaseContext";
|
|
|
|
|
2020-11-26 00:29:10 -05:00
|
|
|
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
import { maps as defaultMaps } from "../maps";
|
|
|
|
|
|
|
|
const MapDataContext = React.createContext();
|
|
|
|
|
2020-09-11 02:56:40 -04:00
|
|
|
// Maximum number of maps to keep in the cache
|
|
|
|
const cachedMapMax = 15;
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
const defaultMapState = {
|
|
|
|
tokens: {},
|
|
|
|
// An index into the draw actions array to which only actions before the
|
|
|
|
// index will be performed (used in undo and redo)
|
|
|
|
mapDrawActionIndex: -1,
|
|
|
|
mapDrawActions: [],
|
|
|
|
fogDrawActionIndex: -1,
|
|
|
|
fogDrawActions: [],
|
|
|
|
// Flags to determine what other people can edit
|
2020-11-05 00:21:52 -05:00
|
|
|
editFlags: ["drawing", "tokens", "notes"],
|
2020-11-03 23:02:56 -05:00
|
|
|
notes: {},
|
2020-05-19 02:21:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export function MapDataProvider({ children }) {
|
2020-10-23 07:16:18 -04:00
|
|
|
const { database, databaseStatus } = useContext(DatabaseContext);
|
2020-05-19 02:21:01 -04:00
|
|
|
const { userId } = useContext(AuthContext);
|
|
|
|
|
|
|
|
const [maps, setMaps] = useState([]);
|
|
|
|
const [mapStates, setMapStates] = useState([]);
|
2020-11-26 00:29:10 -05:00
|
|
|
const [mapsLoading, setMapsLoading] = useState(true);
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
// Load maps from the database and ensure state is properly setup
|
|
|
|
useEffect(() => {
|
2020-10-23 07:16:18 -04:00
|
|
|
if (!userId || !database || databaseStatus === "loading") {
|
2020-05-19 02:21:01 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
async function getDefaultMaps() {
|
|
|
|
const defaultMapsWithIds = [];
|
|
|
|
for (let i = 0; i < defaultMaps.length; i++) {
|
|
|
|
const defaultMap = defaultMaps[i];
|
|
|
|
const id = `__default-${defaultMap.name}`;
|
|
|
|
defaultMapsWithIds.push({
|
|
|
|
...defaultMap,
|
|
|
|
id,
|
|
|
|
owner: userId,
|
|
|
|
// Emulate the time increasing to avoid sort errors
|
|
|
|
created: Date.now() + i,
|
|
|
|
lastModified: Date.now() + i,
|
2020-05-31 02:25:05 -04:00
|
|
|
showGrid: false,
|
2020-08-06 22:28:50 -04:00
|
|
|
snapToGrid: true,
|
2020-10-01 01:05:30 -04:00
|
|
|
group: "default",
|
2020-05-19 02:21:01 -04:00
|
|
|
});
|
|
|
|
// Add a state for the map if there isn't one already
|
|
|
|
const state = await database.table("states").get(id);
|
|
|
|
if (!state) {
|
|
|
|
await database.table("states").add({ ...defaultMapState, mapId: id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultMapsWithIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadMaps() {
|
2020-11-26 00:29:10 -05:00
|
|
|
const worker = Comlink.wrap(new DatabaseWorker());
|
|
|
|
await worker.loadData("maps");
|
|
|
|
const storedMaps = await worker.data;
|
2020-05-19 02:21:01 -04:00
|
|
|
const sortedMaps = storedMaps.sort((a, b) => b.created - a.created);
|
|
|
|
const defaultMapsWithIds = await getDefaultMaps();
|
|
|
|
const allMaps = [...sortedMaps, ...defaultMapsWithIds];
|
|
|
|
setMaps(allMaps);
|
|
|
|
const storedStates = await database.table("states").toArray();
|
|
|
|
setMapStates(storedStates);
|
2020-11-26 00:29:10 -05:00
|
|
|
setMapsLoading(false);
|
2020-05-19 02:21:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMaps();
|
2020-10-23 07:16:18 -04:00
|
|
|
}, [userId, database, databaseStatus]);
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2020-09-11 02:56:40 -04:00
|
|
|
/**
|
|
|
|
* Adds a map to the database, also adds an assosiated state for that map
|
|
|
|
* @param {Object} map map to add
|
|
|
|
*/
|
2020-05-19 02:21:01 -04:00
|
|
|
async function addMap(map) {
|
|
|
|
await database.table("maps").add(map);
|
|
|
|
const state = { ...defaultMapState, mapId: map.id };
|
|
|
|
await database.table("states").add(state);
|
|
|
|
setMaps((prevMaps) => [map, ...prevMaps]);
|
|
|
|
setMapStates((prevStates) => [state, ...prevStates]);
|
2020-09-11 02:56:40 -04:00
|
|
|
if (map.owner !== userId) {
|
|
|
|
await updateCache();
|
|
|
|
}
|
2020-05-19 02:21:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeMap(id) {
|
|
|
|
await database.table("maps").delete(id);
|
|
|
|
await database.table("states").delete(id);
|
|
|
|
setMaps((prevMaps) => {
|
|
|
|
const filtered = prevMaps.filter((map) => map.id !== id);
|
|
|
|
return filtered;
|
|
|
|
});
|
|
|
|
setMapStates((prevMapsStates) => {
|
|
|
|
const filtered = prevMapsStates.filter((state) => state.mapId !== id);
|
|
|
|
return filtered;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-30 01:44:48 -04:00
|
|
|
async function removeMaps(ids) {
|
|
|
|
await database.table("maps").bulkDelete(ids);
|
|
|
|
await database.table("states").bulkDelete(ids);
|
|
|
|
setMaps((prevMaps) => {
|
|
|
|
const filtered = prevMaps.filter((map) => !ids.includes(map.id));
|
|
|
|
return filtered;
|
|
|
|
});
|
|
|
|
setMapStates((prevMapsStates) => {
|
|
|
|
const filtered = prevMapsStates.filter(
|
|
|
|
(state) => !ids.includes(state.mapId)
|
|
|
|
);
|
|
|
|
return filtered;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
async function resetMap(id) {
|
|
|
|
const state = { ...defaultMapState, mapId: id };
|
|
|
|
await database.table("states").put(state);
|
|
|
|
setMapStates((prevMapStates) => {
|
|
|
|
const newStates = [...prevMapStates];
|
|
|
|
const i = newStates.findIndex((state) => state.mapId === id);
|
|
|
|
if (i > -1) {
|
|
|
|
newStates[i] = state;
|
|
|
|
}
|
|
|
|
return newStates;
|
|
|
|
});
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateMap(id, update) {
|
2020-10-23 18:05:16 -04:00
|
|
|
// fake-indexeddb throws an error when updating maps in production.
|
|
|
|
// Catch that error and use put when it fails
|
2020-10-23 08:45:39 -04:00
|
|
|
try {
|
|
|
|
await database.table("maps").update(id, update);
|
|
|
|
} catch (error) {
|
2020-11-26 00:29:10 -05:00
|
|
|
// if (error.name !== "QuotaExceededError") {
|
2020-10-23 17:52:31 -04:00
|
|
|
const map = (await getMapFromDB(id)) || {};
|
|
|
|
await database.table("maps").put({ ...map, id, ...update });
|
2020-11-26 00:29:10 -05:00
|
|
|
// }
|
2020-10-23 08:45:39 -04:00
|
|
|
}
|
2020-05-19 02:21:01 -04:00
|
|
|
setMaps((prevMaps) => {
|
|
|
|
const newMaps = [...prevMaps];
|
|
|
|
const i = newMaps.findIndex((map) => map.id === id);
|
|
|
|
if (i > -1) {
|
2020-07-17 05:22:12 -04:00
|
|
|
newMaps[i] = { ...newMaps[i], ...update };
|
2020-05-19 02:21:01 -04:00
|
|
|
}
|
|
|
|
return newMaps;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:32:21 -04:00
|
|
|
async function updateMaps(ids, update) {
|
|
|
|
await Promise.all(
|
|
|
|
ids.map((id) => database.table("maps").update(id, update))
|
|
|
|
);
|
|
|
|
setMaps((prevMaps) => {
|
|
|
|
const newMaps = [...prevMaps];
|
|
|
|
for (let id of ids) {
|
|
|
|
const i = newMaps.findIndex((map) => map.id === id);
|
|
|
|
if (i > -1) {
|
|
|
|
newMaps[i] = { ...newMaps[i], ...update };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newMaps;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
async function updateMapState(id, update) {
|
|
|
|
await database.table("states").update(id, update);
|
|
|
|
setMapStates((prevMapStates) => {
|
|
|
|
const newStates = [...prevMapStates];
|
|
|
|
const i = newStates.findIndex((state) => state.mapId === id);
|
|
|
|
if (i > -1) {
|
|
|
|
newStates[i] = { ...newStates[i], ...update };
|
|
|
|
}
|
|
|
|
return newStates;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-11 02:56:40 -04:00
|
|
|
/**
|
|
|
|
* Adds a map to the database if none exists or replaces a map if it already exists
|
|
|
|
* Note: this does not add a map state to do that use AddMap
|
|
|
|
* @param {Object} map the map to put
|
|
|
|
*/
|
2020-05-19 08:15:08 -04:00
|
|
|
async function putMap(map) {
|
|
|
|
await database.table("maps").put(map);
|
|
|
|
setMaps((prevMaps) => {
|
|
|
|
const newMaps = [...prevMaps];
|
|
|
|
const i = newMaps.findIndex((m) => m.id === map.id);
|
|
|
|
if (i > -1) {
|
|
|
|
newMaps[i] = { ...newMaps[i], ...map };
|
|
|
|
} else {
|
|
|
|
newMaps.unshift(map);
|
|
|
|
}
|
|
|
|
return newMaps;
|
|
|
|
});
|
2020-09-11 02:56:40 -04:00
|
|
|
if (map.owner !== userId) {
|
|
|
|
await updateCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep up to cachedMapMax amount of maps that you don't own
|
|
|
|
* Sorted by when they we're last used
|
|
|
|
*/
|
|
|
|
async function updateCache() {
|
|
|
|
const cachedMaps = await database
|
|
|
|
.table("maps")
|
|
|
|
.where("owner")
|
|
|
|
.notEqual(userId)
|
|
|
|
.sortBy("lastUsed");
|
|
|
|
if (cachedMaps.length > cachedMapMax) {
|
|
|
|
const cacheDeleteCount = cachedMaps.length - cachedMapMax;
|
|
|
|
const idsToDelete = cachedMaps
|
|
|
|
.slice(0, cacheDeleteCount)
|
|
|
|
.map((map) => map.id);
|
|
|
|
database.table("maps").where("id").anyOf(idsToDelete).delete();
|
|
|
|
setMaps((prevMaps) => {
|
|
|
|
return prevMaps.filter((map) => !idsToDelete.includes(map.id));
|
|
|
|
});
|
|
|
|
}
|
2020-05-19 08:15:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMap(mapId) {
|
|
|
|
return maps.find((map) => map.id === mapId);
|
|
|
|
}
|
|
|
|
|
2020-08-28 03:06:13 -04:00
|
|
|
async function getMapFromDB(mapId) {
|
2020-10-23 17:23:06 -04:00
|
|
|
let map = await database.table("maps").get(mapId);
|
|
|
|
return map;
|
2020-08-28 03:06:13 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 08:15:08 -04:00
|
|
|
const ownedMaps = maps.filter((map) => map.owner === userId);
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
const value = {
|
|
|
|
maps,
|
2020-05-19 08:15:08 -04:00
|
|
|
ownedMaps,
|
2020-05-19 02:21:01 -04:00
|
|
|
mapStates,
|
|
|
|
addMap,
|
|
|
|
removeMap,
|
2020-09-30 01:44:48 -04:00
|
|
|
removeMaps,
|
2020-05-19 02:21:01 -04:00
|
|
|
resetMap,
|
|
|
|
updateMap,
|
2020-10-01 08:32:21 -04:00
|
|
|
updateMaps,
|
2020-05-19 02:21:01 -04:00
|
|
|
updateMapState,
|
2020-05-19 08:15:08 -04:00
|
|
|
putMap,
|
|
|
|
getMap,
|
2020-08-28 03:06:13 -04:00
|
|
|
getMapFromDB,
|
2020-11-26 00:29:10 -05:00
|
|
|
mapsLoading,
|
2020-05-19 02:21:01 -04:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MapDataContext;
|