|
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import { parse as parseCookie } from 'cookie'; |
| 4 | + |
| 5 | +import BootWarning from '../components/boot-warning'; |
| 6 | + |
| 7 | +const hasLocalStorage = () => { |
| 8 | + try { |
| 9 | + localStorage.setItem('__localStorageSentinel__', 'present'); |
| 10 | + localStorage.removeItem('__localStorageSentinel__'); |
| 11 | + return true; |
| 12 | + } catch (e) { |
| 13 | + return false; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +const hasIndexedDB = () => { |
| 18 | + try { |
| 19 | + const opener = indexedDB.open('simplenote_sentinel'); |
| 20 | + if (!(opener instanceof IDBOpenDBRequest)) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + const closer = () => indexedDB.deleteDatabase('simplenote_sentinel'); |
| 24 | + opener.onerror = closer; |
| 25 | + opener.onsuccess = closer; |
| 26 | + return true; |
| 27 | + } catch (e) { |
| 28 | + return false; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +const hasCookies = () => { |
| 33 | + try { |
| 34 | + if (!navigator.cookieEnabled) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + const cookie = document.cookie; |
| 39 | + |
| 40 | + const now = Date.now().toString(); |
| 41 | + document.cookie = `__now=${now};`; |
| 42 | + const parsed = parseCookie(document.cookie); |
| 43 | + const didSet = parsed.__now === now; |
| 44 | + document.cookie = cookie; |
| 45 | + |
| 46 | + return didSet; |
| 47 | + } catch (e) { |
| 48 | + return false; |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +const deps = [ |
| 53 | + ['localStorage', hasLocalStorage()], |
| 54 | + ['indexedDB', hasIndexedDB()], |
| 55 | + ['cookies', hasCookies()], |
| 56 | +]; |
| 57 | + |
| 58 | +const missingDeps = deps.filter(([, hasIt]) => !hasIt).map(([name]) => name); |
| 59 | + |
| 60 | +if (missingDeps.length) { |
| 61 | + ReactDOM.render( |
| 62 | + <BootWarning> |
| 63 | + <p> |
| 64 | + Simplenote depends on a few web technologies to operate. Please make |
| 65 | + sure that you have all of the following enabled in your browser. |
| 66 | + </p> |
| 67 | + <ul> |
| 68 | + {deps.map(([name, hasIt]) => ( |
| 69 | + <li key={name}> |
| 70 | + {hasIt ? '✅' : '⚠️'} {name} - {hasIt ? 'working' : 'missing'} |
| 71 | + </li> |
| 72 | + ))} |
| 73 | + </ul> |
| 74 | + <p> |
| 75 | + Many browsers disable some of these features in Private Mode. Simplenote |
| 76 | + does not currently support running in Private Mode. |
| 77 | + </p> |
| 78 | + </BootWarning>, |
| 79 | + document.getElementById('root') |
| 80 | + ); |
| 81 | + throw new Error( |
| 82 | + `Simplenote is missing required dependencies: ${missingDeps.join(', ')}` |
| 83 | + ); |
| 84 | +} |
0 commit comments