grungnet/src/contexts/DatabaseContext.js

122 lines
3.5 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useContext } from "react";
2021-04-22 21:46:52 -04:00
// eslint-disable-next-line no-unused-vars
import Dexie from "dexie";
import * as Comlink from "comlink";
2020-11-25 23:24:33 -05:00
import ErrorBanner from "../components/banner/ErrorBanner";
import { getDatabase } from "../database";
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
2021-04-22 21:46:52 -04:00
/**
* @typedef DatabaseContext
* @property {Dexie|undefined} database
* @property {any} worker
* @property {string} databaseStatus
* @property {Error|undefined} databaseError
*/
/**
* @type {React.Context<undefined|DatabaseContext>}
*/
const DatabaseContext = React.createContext();
const worker = Comlink.wrap(new DatabaseWorker());
export function DatabaseProvider({ children }) {
const [database, setDatabase] = useState();
// "loading" | "disabled" | "upgrading" | "loaded"
const [databaseStatus, setDatabaseStatus] = useState("loading");
2020-11-25 23:24:33 -05:00
const [databaseError, setDatabaseError] = useState();
useEffect(() => {
// Create a test database and open it to see if indexedDB is enabled
let testDBRequest = window.indexedDB.open("__test");
2020-10-23 07:16:18 -04:00
testDBRequest.onsuccess = async function () {
testDBRequest.result.close();
let db = getDatabase(
{ autoOpen: false },
undefined,
undefined,
true,
2021-06-25 03:43:43 -04:00
() => {
setDatabaseStatus("upgrading");
}
);
setDatabase(db);
2020-10-23 07:16:18 -04:00
db.on("ready", () => {
setDatabaseStatus("loaded");
});
2021-06-23 23:06:00 -04:00
db.on("versionchange", () => {
// When another tab loads a new version of the database refresh the page
window.location.reload();
});
2020-10-23 07:16:18 -04:00
await db.open();
window.indexedDB.deleteDatabase("__test");
};
// If indexedb disabled create an in memory database
testDBRequest.onerror = async function () {
console.warn("Database is disabled, no state will be saved");
const indexedDB = await import("fake-indexeddb");
const IDBKeyRange = await import("fake-indexeddb/lib/FDBKeyRange");
2020-10-23 07:16:18 -04:00
let db = getDatabase({ indexedDB, IDBKeyRange, autoOpen: false });
setDatabase(db);
2020-10-23 07:16:18 -04:00
db.on("ready", () => {
setDatabaseStatus("disabled");
});
await db.open();
window.indexedDB.deleteDatabase("__test");
};
2020-11-25 23:24:33 -05:00
function handleDatabaseError(event) {
event.preventDefault();
if (event?.reason?.message?.startsWith("QuotaExceededError")) {
2020-11-25 23:24:33 -05:00
setDatabaseError({
name: event?.reason?.name,
2020-11-25 23:24:33 -05:00
message: "Storage Quota Exceeded Please Clear Space and Try Again.",
});
} else {
setDatabaseError({
name: event?.reason?.name,
message: "Something went wrong, please refresh your browser.",
});
2020-11-25 23:24:33 -05:00
}
console.error(event?.reason);
2020-11-25 23:24:33 -05:00
}
window.addEventListener("unhandledrejection", handleDatabaseError);
return () => {
window.removeEventListener("unhandledrejection", handleDatabaseError);
};
}, []);
const value = {
database,
databaseStatus,
2020-11-25 23:24:33 -05:00
databaseError,
worker,
};
return (
<DatabaseContext.Provider value={value}>
2020-11-25 23:24:33 -05:00
<>
{children}
<ErrorBanner
error={databaseError}
2020-11-25 23:24:33 -05:00
onRequestClose={() => setDatabaseError()}
/>
2020-11-25 23:24:33 -05:00
</>
</DatabaseContext.Provider>
);
}
export function useDatabase() {
const context = useContext(DatabaseContext);
if (context === undefined) {
throw new Error("useDatabase must be used within a DatabaseProvider");
}
return context;
}
export default DatabaseContext;