2021-01-21 22:59:05 -05:00
|
|
|
import React, {
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
2021-07-02 01:54:54 -04:00
|
|
|
useMemo,
|
2021-01-21 22:59:05 -05:00
|
|
|
} from "react";
|
2021-07-02 01:54:54 -04:00
|
|
|
import { useLiveQuery } from "dexie-react-hooks";
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-02-05 21:32:38 -05:00
|
|
|
import { useDatabase } from "./DatabaseContext";
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-09 02:22:35 -04:00
|
|
|
import { Map } from "../types/Map";
|
|
|
|
import { MapState } from "../types/MapState";
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
import { removeGroupsItems } from "../helpers/group";
|
2021-06-03 01:31:18 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
import { Group } from "../types/Group";
|
|
|
|
|
|
|
|
export type AddMapEventHandler = (map: Map) => Promise<void>;
|
|
|
|
export type RemoveMapsEventHandler = (ids: string[]) => Promise<void>;
|
|
|
|
export type ResetMapEventHandler = (id: string) => Promise<MapState>;
|
|
|
|
export type UpdateMapEventHanlder = (
|
|
|
|
id: string,
|
|
|
|
update: Partial<Map>
|
|
|
|
) => Promise<void>;
|
|
|
|
export type UpdateMapStateEventHandler = (
|
|
|
|
id: string,
|
|
|
|
update: Partial<MapState>
|
|
|
|
) => Promise<void>;
|
|
|
|
export type GetMapStateEventHandler = (
|
|
|
|
id: string
|
|
|
|
) => Promise<MapState | undefined>;
|
|
|
|
export type GetMapEventHandler = (id: string) => Promise<Map | undefined>;
|
|
|
|
export type UpdateMapGroupsEventHandler = (groups: Group[]) => Promise<void>;
|
|
|
|
|
2021-07-17 04:39:49 -04:00
|
|
|
type MapDataContextValue = {
|
2021-07-02 01:54:54 -04:00
|
|
|
maps: Array<Map>;
|
|
|
|
mapStates: MapState[];
|
2021-07-12 18:59:28 -04:00
|
|
|
/** Adds a map to the database, also adds an assosiated state and group for that map */
|
|
|
|
addMap: AddMapEventHandler;
|
|
|
|
removeMaps: RemoveMapsEventHandler;
|
|
|
|
resetMap: ResetMapEventHandler;
|
|
|
|
updateMap: UpdateMapEventHanlder;
|
|
|
|
updateMapState: UpdateMapStateEventHandler;
|
|
|
|
getMapState: GetMapStateEventHandler;
|
|
|
|
getMap: GetMapEventHandler;
|
2021-07-02 01:54:54 -04:00
|
|
|
mapsLoading: boolean;
|
2021-07-12 18:59:28 -04:00
|
|
|
updateMapGroups: UpdateMapGroupsEventHandler;
|
2021-07-02 01:54:54 -04:00
|
|
|
mapsById: Record<string, Map>;
|
2021-07-12 18:59:28 -04:00
|
|
|
mapGroups: Group[];
|
2021-07-02 01:54:54 -04:00
|
|
|
};
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const MapDataContext =
|
2021-07-17 04:39:49 -04:00
|
|
|
React.createContext<MapDataContextValue | undefined>(undefined);
|
2020-09-11 02:56:40 -04:00
|
|
|
|
2021-07-16 00:55:33 -04:00
|
|
|
const defaultMapState: Omit<MapState, "mapId"> = {
|
2021-07-02 01:54:54 -04:00
|
|
|
tokens: {},
|
2021-08-05 18:21:19 -04:00
|
|
|
drawings: {},
|
|
|
|
fogs: {},
|
2021-08-10 19:16:07 -04:00
|
|
|
editFlags: ["drawing", "tokens", "notes"],
|
2021-07-12 18:59:28 -04:00
|
|
|
notes: {},
|
2020-05-19 02:21:01 -04:00
|
|
|
};
|
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
export function MapDataProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
const { database } = useDatabase();
|
2020-11-26 00:29:10 -05:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const mapsQuery = useLiveQuery<Map[]>(
|
|
|
|
() => database?.table("maps").toArray() || [],
|
|
|
|
[database]
|
|
|
|
);
|
|
|
|
const mapStatesQuery = useLiveQuery<MapState[]>(
|
|
|
|
() => database?.table("states").toArray() || [],
|
|
|
|
[database]
|
|
|
|
);
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const maps = useMemo(() => mapsQuery || [], [mapsQuery]);
|
|
|
|
const mapStates = useMemo(() => mapStatesQuery || [], [mapStatesQuery]);
|
|
|
|
const mapsLoading = useMemo(
|
|
|
|
() => !mapsQuery || !mapStatesQuery,
|
|
|
|
[mapsQuery, mapStatesQuery]
|
|
|
|
);
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const mapGroupQuery = useLiveQuery(
|
|
|
|
() => database?.table("groups").get("maps"),
|
|
|
|
[database]
|
|
|
|
);
|
2020-05-19 02:21:01 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const [mapGroups, setMapGroups] = useState<Group[]>([]);
|
2021-01-21 22:59:05 -05:00
|
|
|
useEffect(() => {
|
2021-07-02 01:54:54 -04:00
|
|
|
async function updateMapGroups() {
|
|
|
|
const group = await database?.table("groups").get("maps");
|
|
|
|
setMapGroups(group.items);
|
|
|
|
}
|
|
|
|
if (database && mapGroupQuery) {
|
|
|
|
updateMapGroups();
|
|
|
|
}
|
|
|
|
}, [mapGroupQuery, database]);
|
2020-10-01 08:32:21 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const getMap = useCallback<GetMapEventHandler>(
|
|
|
|
async (mapId) => {
|
|
|
|
let map = await database?.table("maps").get(mapId);
|
2021-01-21 22:59:05 -05:00
|
|
|
return map;
|
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
2020-09-11 02:56:40 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const getMapState = useCallback<GetMapStateEventHandler>(
|
2021-02-14 02:35:42 -05:00
|
|
|
async (mapId) => {
|
2021-07-12 18:59:28 -04:00
|
|
|
let mapState = await database?.table("states").get(mapId);
|
2021-02-14 02:35:42 -05:00
|
|
|
return mapState;
|
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const addMap = useCallback<AddMapEventHandler>(
|
2021-01-21 22:59:05 -05:00
|
|
|
async (map) => {
|
2021-07-02 01:54:54 -04:00
|
|
|
if (database) {
|
|
|
|
// Just update map database as react state will be updated with an Observable
|
|
|
|
const state = { ...defaultMapState, mapId: map.id };
|
|
|
|
await database.table("maps").add(map);
|
|
|
|
await database.table("states").add(state);
|
|
|
|
const group = await database.table("groups").get("maps");
|
|
|
|
await database.table("groups").update("maps", {
|
|
|
|
items: [{ id: map.id, type: "item" }, ...group.items],
|
|
|
|
});
|
2021-01-21 22:59:05 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
2020-05-19 08:15:08 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const removeMaps = useCallback<RemoveMapsEventHandler>(
|
2021-01-21 22:59:05 -05:00
|
|
|
async (ids) => {
|
2021-07-02 01:54:54 -04:00
|
|
|
if (database) {
|
|
|
|
const maps = await database.table("maps").bulkGet(ids);
|
|
|
|
// Remove assets linked with maps
|
|
|
|
let assetIds = [];
|
|
|
|
for (let map of maps) {
|
|
|
|
if (map.type === "file") {
|
|
|
|
assetIds.push(map.file);
|
|
|
|
assetIds.push(map.thumbnail);
|
|
|
|
for (let res of Object.values(map.resolutions)) {
|
|
|
|
assetIds.push(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const group = await database.table("groups").get("maps");
|
|
|
|
let items = removeGroupsItems(group.items, ids);
|
|
|
|
await database.table("groups").update("maps", { items });
|
|
|
|
|
|
|
|
await database.table("maps").bulkDelete(ids);
|
|
|
|
await database.table("states").bulkDelete(ids);
|
|
|
|
await database.table("assets").bulkDelete(assetIds);
|
|
|
|
}
|
2021-01-21 22:59:05 -05:00
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
2020-05-19 08:15:08 -04:00
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const resetMap = useCallback<ResetMapEventHandler>(
|
2021-01-21 22:59:05 -05:00
|
|
|
async (id) => {
|
2021-07-12 18:59:28 -04:00
|
|
|
const state: MapState = { ...defaultMapState, mapId: id };
|
2021-06-03 01:31:18 -04:00
|
|
|
await database?.table("states").put(state);
|
2021-01-21 22:59:05 -05:00
|
|
|
return state;
|
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const updateMap = useCallback<UpdateMapEventHanlder>(
|
2021-01-21 22:59:05 -05:00
|
|
|
async (id, update) => {
|
2021-07-02 01:54:54 -04:00
|
|
|
await database?.table("maps").update(id, update);
|
2021-01-21 22:59:05 -05:00
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const updateMapState = useCallback<UpdateMapStateEventHandler>(
|
2021-01-21 22:59:05 -05:00
|
|
|
async (id, update) => {
|
2021-06-03 01:31:18 -04:00
|
|
|
await database?.table("states").update(id, update);
|
2021-01-21 22:59:05 -05:00
|
|
|
},
|
|
|
|
[database]
|
|
|
|
);
|
|
|
|
|
2021-07-12 18:59:28 -04:00
|
|
|
const updateMapGroups = useCallback<UpdateMapGroupsEventHandler>(
|
2021-07-02 01:54:54 -04:00
|
|
|
async (groups) => {
|
|
|
|
// Update group state immediately to avoid animation delay
|
|
|
|
setMapGroups(groups);
|
|
|
|
await database?.table("groups").update("maps", { items: groups });
|
2021-01-21 22:59:05 -05:00
|
|
|
},
|
2021-07-02 01:54:54 -04:00
|
|
|
[database]
|
2021-01-21 22:59:05 -05:00
|
|
|
);
|
2020-08-28 03:06:13 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const [mapsById, setMapsById] = useState<Record<string, Map>>({});
|
2021-02-14 02:35:42 -05:00
|
|
|
useEffect(() => {
|
2021-07-02 01:54:54 -04:00
|
|
|
setMapsById(
|
|
|
|
maps.reduce((obj: Record<string, Map>, map) => {
|
|
|
|
obj[map.id] = map;
|
|
|
|
return obj;
|
|
|
|
}, {})
|
|
|
|
);
|
|
|
|
}, [maps]);
|
2020-05-19 08:15:08 -04:00
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
const value = {
|
|
|
|
maps,
|
|
|
|
mapStates,
|
2021-07-02 01:54:54 -04:00
|
|
|
mapGroups,
|
2020-05-19 02:21:01 -04:00
|
|
|
addMap,
|
2020-09-30 01:44:48 -04:00
|
|
|
removeMaps,
|
2020-05-19 02:21:01 -04:00
|
|
|
resetMap,
|
|
|
|
updateMap,
|
|
|
|
updateMapState,
|
2020-05-19 08:15:08 -04:00
|
|
|
getMap,
|
2020-11-26 00:29:10 -05:00
|
|
|
mapsLoading,
|
2021-07-02 01:54:54 -04:00
|
|
|
getMapState,
|
|
|
|
updateMapGroups,
|
|
|
|
mapsById,
|
2020-05-19 02:21:01 -04:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-05 21:32:38 -05:00
|
|
|
export function useMapData() {
|
|
|
|
const context = useContext(MapDataContext);
|
|
|
|
if (context === undefined) {
|
|
|
|
throw new Error("useMapData must be used within a MapDataProvider");
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:21:01 -04:00
|
|
|
export default MapDataContext;
|