2021-02-05 21:32:38 -05:00
|
|
|
import React, { useState, useEffect, useContext } from "react";
|
2021-04-22 21:46:52 -04:00
|
|
|
import Dexie from "dexie";
|
2021-03-19 00:32:17 -04:00
|
|
|
import * as Comlink from "comlink";
|
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
|
|
|
|
2021-06-03 01:31:18 -04:00
|
|
|
//@ts-ignore
|
2021-03-19 00:32:17 -04:00
|
|
|
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
|
2021-07-16 00:55:33 -04:00
|
|
|
import { DatabaseWorkerService } from "../workers/DatabaseWorker";
|
|
|
|
|
|
|
|
export type DatabaseStatus = "loading" | "disabled" | "upgrading" | "loaded";
|
2021-03-19 00:32:17 -04:00
|
|
|
|
2021-07-17 04:39:49 -04:00
|
|
|
type DatabaseContextValue = {
|
2021-07-02 01:54:54 -04:00
|
|
|
database: Dexie | undefined;
|
2021-07-16 00:55:33 -04:00
|
|
|
databaseStatus: DatabaseStatus;
|
2021-07-02 01:54:54 -04:00
|
|
|
databaseError: Error | undefined;
|
2021-07-16 00:55:33 -04:00
|
|
|
worker: Comlink.Remote<DatabaseWorkerService>;
|
2021-07-02 01:54:54 -04:00
|
|
|
};
|
2021-06-03 01:31:18 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
const DatabaseContext =
|
2021-07-17 04:39:49 -04:00
|
|
|
React.createContext<DatabaseContextValue | undefined>(undefined);
|
2020-05-03 04:22:09 -04:00
|
|
|
|
2021-07-16 00:55:33 -04:00
|
|
|
const worker: Comlink.Remote<DatabaseWorkerService> = Comlink.wrap(
|
|
|
|
new DatabaseWorker()
|
|
|
|
);
|
2021-03-19 00:32:17 -04:00
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
export function DatabaseProvider({ children }: { children: React.ReactNode }) {
|
|
|
|
const [database, setDatabase] = useState<Dexie>();
|
|
|
|
const [databaseStatus, setDatabaseStatus] =
|
2021-07-16 00:55:33 -04:00
|
|
|
useState<DatabaseStatus>("loading");
|
2021-07-02 01:54:54 -04:00
|
|
|
const [databaseError, setDatabaseError] = useState<Error>();
|
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();
|
2021-06-24 02:14:20 -04:00
|
|
|
let db = getDatabase(
|
|
|
|
{ autoOpen: false },
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
true,
|
2021-06-25 03:43:43 -04:00
|
|
|
() => {
|
2021-06-24 02:14:20 -04:00
|
|
|
setDatabaseStatus("upgrading");
|
|
|
|
}
|
|
|
|
);
|
2020-05-03 04:22:09 -04:00
|
|
|
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();
|
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
|
|
|
|
2021-07-02 01:54:54 -04:00
|
|
|
function handleDatabaseError(event: PromiseRejectionEvent) {
|
2021-06-27 01:14:26 -04:00
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
2021-06-27 18:24:48 -04:00
|
|
|
if (event.reason instanceof Dexie.DexieError) {
|
|
|
|
if (event.reason?.inner?.name === "QuotaExceededError") {
|
|
|
|
setDatabaseError({
|
|
|
|
name: event.reason?.name,
|
|
|
|
message:
|
|
|
|
"Storage Quota Exceeded Please Clear Space and Try Again.",
|
|
|
|
});
|
|
|
|
} else if (event.reason?.inner?.name === "DatabaseClosedError") {
|
|
|
|
setDatabaseError({
|
|
|
|
name: event.reason?.name,
|
|
|
|
message: "Database closed, please refresh your browser.",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setDatabaseError({
|
|
|
|
name: event.reason?.name,
|
|
|
|
message: "Something went wrong, please refresh your browser.",
|
|
|
|
});
|
|
|
|
}
|
2021-06-27 01:14:26 -04:00
|
|
|
} else {
|
|
|
|
setDatabaseError({
|
|
|
|
name: event.reason?.name,
|
|
|
|
message: "Something went wrong, please refresh your browser.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.error(event.reason);
|
2020-11-25 23:24:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2021-03-19 00:32:17 -04:00
|
|
|
worker,
|
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}
|
2021-06-03 01:31:18 -04:00
|
|
|
onRequestClose={() => setDatabaseError(undefined)}
|
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-07-17 04:39:49 -04:00
|
|
|
export function useDatabase() {
|
2021-02-05 21:32:38 -05:00
|
|
|
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;
|