index.tsx
| 1 | /* @refresh reload */ |
| 2 | import { render } from "solid-js/web"; |
| 3 | import "./style.css"; |
| 4 | import "./components/Dialog.css"; |
| 5 | |
| 6 | import App from "./App"; |
| 7 | import { loadDataFromDB } from "./offline"; |
| 8 | |
| 9 | const root = document.getElementById("root"); |
| 10 | if (!(root instanceof HTMLElement)) { |
| 11 | throw new Error( |
| 12 | "Root element not found. Did you forget to add it to your index.html? Or maybe the id attribute got misspelled?", |
| 13 | ); |
| 14 | } |
| 15 | |
| 16 | function hasTouch() { |
| 17 | return ( |
| 18 | "ontouchstart" in document.documentElement || |
| 19 | navigator.maxTouchPoints > 0 || |
| 20 | ("msMaxTouchPoints" in navigator && |
| 21 | typeof navigator.msMaxTouchPoints === "number" && |
| 22 | navigator.msMaxTouchPoints > 0) |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | if (hasTouch()) { |
| 27 | // remove all the :hover stylesheets |
| 28 | try { |
| 29 | // prevent exception on browsers not supporting DOM styleSheets properly |
| 30 | for (const si in document.styleSheets) { |
| 31 | const styleSheet = document.styleSheets[si]; |
| 32 | if (!styleSheet.cssRules) continue; |
| 33 | |
| 34 | for (let ri = styleSheet.cssRules.length - 1; ri >= 0; ri--) { |
| 35 | const rule = styleSheet.cssRules[ri]; |
| 36 | if (!("selectorText" in rule) || typeof rule.selectorText !== "string" || !rule.selectorText) continue; |
| 37 | |
| 38 | if (rule.selectorText.match(":hover")) { |
| 39 | styleSheet.deleteRule(ri); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } catch {} |
| 44 | } |
| 45 | |
| 46 | const currentVersion = "1"; |
| 47 | const version = localStorage.getItem("app-version"); |
| 48 | if (version && version !== currentVersion) { |
| 49 | //TODO: add a way to at least export playlists before resetting |
| 50 | root.innerHTML = ""; |
| 51 | render( |
| 52 | () => ( |
| 53 | <div style={{ display: "flex", height: "100%", "align-items": "center" }}> |
| 54 | <dialog open> |
| 55 | <div> |
| 56 | Your local version of this application is not compatible with the newly loaded one, click "Reset" to delete |
| 57 | all stored settings, files and cached and refresh the page. |
| 58 | <button |
| 59 | style={{ margin: "auto" }} |
| 60 | onClick={async () => { |
| 61 | localStorage.clear(); |
| 62 | if ("serviceWorker" in navigator) { |
| 63 | caches.keys().then((cacheNames) => { |
| 64 | for (const cacheName of cacheNames) { |
| 65 | caches.delete(cacheName); |
| 66 | } |
| 67 | }); |
| 68 | } |
| 69 | indexedDB.deleteDatabase("music-files").onsuccess = () => { |
| 70 | location.hash = ""; |
| 71 | location.reload(); |
| 72 | }; |
| 73 | }} |
| 74 | > |
| 75 | Reset |
| 76 | </button> |
| 77 | </div> |
| 78 | </dialog> |
| 79 | </div> |
| 80 | ), |
| 81 | root, |
| 82 | ); |
| 83 | } else { |
| 84 | if (!version) { |
| 85 | try { |
| 86 | localStorage.setItem("app-version", currentVersion); |
| 87 | } catch {} |
| 88 | } |
| 89 | |
| 90 | loadDataFromDB() |
| 91 | .then(() => { |
| 92 | root.innerHTML = ""; |
| 93 | render(() => <App />, root); |
| 94 | }) |
| 95 | .catch((e) => { |
| 96 | root.innerHTML = e.toString(); |
| 97 | }); |
| 98 | } |
| 99 |