diff --git a/packages/concordia-app/src/index.jsx b/packages/concordia-app/src/index.jsx index 4f21227..a29f530 100644 --- a/packages/concordia-app/src/index.jsx +++ b/packages/concordia-app/src/index.jsx @@ -1,3 +1,4 @@ +import './utils/indexedDB/patchIndexedDB'; import './utils/wdyr'; import React, { Suspense } from 'react'; import { render } from 'react-dom'; diff --git a/packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx b/packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx index 11524eb..975c40c 100644 --- a/packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx +++ b/packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx @@ -5,6 +5,7 @@ import { useHistory } from 'react-router'; import { useSelector } from 'react-redux'; import AppContext from '../../../components/AppContext'; import appLogo from '../../../assets/images/app_logo.png'; +import purgeIndexedDBs from '../../../utils/indexedDB/indexedDBUtils'; const MainLayoutMenu = () => { const hasSignedUp = useSelector((state) => state.user.hasSignedUp); @@ -56,6 +57,16 @@ const MainLayoutMenu = () => { {t('topbar.button.register')} )} + { + await purgeIndexedDBs(); + }} + > + Purge + )} diff --git a/packages/concordia-app/src/options/breezeOptions.js b/packages/concordia-app/src/options/breezeOptions.js index 41984c0..3f3a15f 100644 --- a/packages/concordia-app/src/options/breezeOptions.js +++ b/packages/concordia-app/src/options/breezeOptions.js @@ -10,6 +10,7 @@ const REACT_APP_RENDEZVOUS_PORT = process.env.REACT_APP_RENDEZVOUS_PORT || REACT const breezeOptions = { ipfs: { + repo: 'concordia', config: { Addresses: { Swarm: [ diff --git a/packages/concordia-app/src/utils/indexedDB/indexedDBUtils.js b/packages/concordia-app/src/utils/indexedDB/indexedDBUtils.js new file mode 100644 index 0000000..a44fdfa --- /dev/null +++ b/packages/concordia-app/src/utils/indexedDB/indexedDBUtils.js @@ -0,0 +1,20 @@ +import { breeze } from '../../redux/store'; + +async function purgeIndexedDBs() { + const { ipfs, orbit } = breeze; + if (orbit) await orbit.stop(); + if (ipfs) await ipfs.stop(); + + const dbs = await indexedDB.databases(); + await Promise.all( + dbs.map((db) => new Promise( + (resolve, reject) => { + const request = indexedDB.deleteDatabase(db.name); + request.onsuccess = resolve; + request.onerror = reject; + }, + )), + ); +} + +export default purgeIndexedDBs; diff --git a/packages/concordia-app/src/utils/indexedDB/patchIndexedDB.js b/packages/concordia-app/src/utils/indexedDB/patchIndexedDB.js new file mode 100644 index 0000000..185ecda --- /dev/null +++ b/packages/concordia-app/src/utils/indexedDB/patchIndexedDB.js @@ -0,0 +1,46 @@ +/* Patches browsers that do not yet support indexedDB.databases() + (https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/databases) + See also https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-3449418) */ +if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') { + const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases'; + + // Store a key value map of databases + const getFromStorage = () => JSON.parse(window.localStorage[LOCALSTORAGE_CACHE_KEY] || '{}'); + + // Write the database to local storage + const writeToStorage = (value) => { window.localStorage[LOCALSTORAGE_CACHE_KEY] = JSON.stringify(value); }; + + IDBFactory.prototype.databases = () => Promise.resolve( + Object.entries(getFromStorage()).reduce((acc, [name, version]) => { + acc.push({ name, version }); + return acc; + }, []), + ); + + // Intercept the existing open handler to write our DBs names + // and versions to localStorage + const { open } = IDBFactory.prototype; + + // eslint-disable-next-line func-names + IDBFactory.prototype.open = function (...args) { + const dbName = args[0]; + const version = args[1] || 1; + const existing = getFromStorage(); + writeToStorage({ ...existing, [dbName]: version }); + return open.apply(this, args); + }; + + // Intercept the existing deleteDatabase handler remove our + // dbNames from localStorage + const { deleteDatabase } = IDBFactory.prototype; + + // eslint-disable-next-line func-names + IDBFactory.prototype.deleteDatabase = function (...args) { + const dbName = args[0]; + const existing = getFromStorage(); + delete existing[dbName]; + writeToStorage(existing); + return deleteDatabase.apply(this, args); + }; + console.debug('IndexedDB patched successfully!'); +}