2021-02-05 21:32:38 -05:00
|
|
|
import React, { useState, useEffect, useContext } from "react";
|
2020-11-25 23:24:33 -05:00
|
|
|
|
2021-03-17 23:02:13 -04:00
|
|
|
import ErrorBanner from "../components/banner/ErrorBanner";
|
2020-05-03 05:52:01 -04:00
|
|
|
|
|
|
|
import { getDatabase } from "../database";
|
2020-05-03 04:22:09 -04:00
|
|
|
|
|
|
|
const DatabaseContext = React.createContext();
|
|
|
|
|
|
|
|
export function DatabaseProvider({ children }) {
|
|
|
|
const [database, setDatabase] = useState();
|
|
|
|
const [databaseStatus, setDatabaseStatus] = useState("loading");
|
2020-11-25 23:24:33 -05:00
|
|
|
const [databaseError, setDatabaseError] = useState();
|
2020-05-03 04:22:09 -04:00
|
|
|
|
|
|
|
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 () {
|
2020-05-03 04:22:09 -04:00
|
|
|
testDBRequest.result.close();
|
2020-10-23 07:16:18 -04:00
|
|
|
let db = getDatabase({ autoOpen: false });
|
2020-05-03 04:22:09 -04:00
|
|
|
setDatabase(db);
|
2020-10-23 07:16:18 -04:00
|
|
|
db.on("ready", () => {
|
|
|
|
setDatabaseStatus("loaded");
|
|
|
|
});
|
|
|
|
await db.open();
|
2020-05-03 04:22:09 -04:00
|
|
|
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 });
|
2020-05-03 04:22:09 -04:00
|
|
|
setDatabase(db);
|
2020-10-23 07:16:18 -04:00
|
|
|
db.on("ready", () => {
|
|
|
|
setDatabaseStatus("disabled");
|
|
|
|
});
|
|
|
|
await db.open();
|
2020-05-03 04:22:09 -04:00
|
|
|
window.indexedDB.deleteDatabase("__test");
|
|
|
|
};
|
2020-11-25 23:24:33 -05:00
|
|
|
|
|
|
|
function handleDatabaseError(event) {
|
2021-02-08 16:07:40 -05:00
|
|
|
if (event.reason?.name === "QuotaExceededError") {
|
2020-11-25 23:24:33 -05:00
|
|
|
event.preventDefault();
|
|
|
|
setDatabaseError({
|
|
|
|
name: event.reason.name,
|
|
|
|
message: "Storage Quota Exceeded Please Clear Space and Try Again.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener("unhandledrejection", handleDatabaseError);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("unhandledrejection", handleDatabaseError);
|
|
|
|
};
|
2020-05-03 04:22:09 -04:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
database,
|
|
|
|
databaseStatus,
|
2020-11-25 23:24:33 -05:00
|
|
|
databaseError,
|
2020-05-03 04:22:09 -04:00
|
|
|
};
|
|
|
|
return (
|
|
|
|
<DatabaseContext.Provider value={value}>
|
2020-11-25 23:24:33 -05:00
|
|
|
<>
|
|
|
|
{children}
|
2021-03-17 23:02:13 -04:00
|
|
|
<ErrorBanner
|
|
|
|
error={databaseError}
|
2020-11-25 23:24:33 -05:00
|
|
|
onRequestClose={() => setDatabaseError()}
|
2021-03-17 23:02:13 -04:00
|
|
|
/>
|
2020-11-25 23:24:33 -05:00
|
|
|
</>
|
2020-05-03 04:22:09 -04:00
|
|
|
</DatabaseContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-05 21:32:38 -05:00
|
|
|
export function useDatabase() {
|
|
|
|
const context = useContext(DatabaseContext);
|
|
|
|
if (context === undefined) {
|
|
|
|
throw new Error("useDatabase must be used within a DatabaseProvider");
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2020-05-03 04:22:09 -04:00
|
|
|
export default DatabaseContext;
|