index.tsx
⎇
Raw
1/* @refresh reload */
2import { render } from "solid-js/web";
3import "./style.css";
4import "./components/Dialog.css";
5
6import App from "./App";
7import { loadDataFromDB } from "./offline";
8
9const root = document.getElementById("root");
10if (!(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
16function 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
26if (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
46const currentVersion = "1";
47const version = localStorage.getItem("app-version");
48if (version && version !== currentVersion) {
49 root.innerHTML = "";
50 render(
51 () => (
52 <div style={{ display: "flex", height: "100%", "align-items": "center" }}>
53 <dialog open>
54 <div>
55 Your local version of this application is not compatible with the newly loaded one, click reset to delete
56 all stored settings, files and cached and refresh the page.
57 <button
58 onClick={async () => {
59 localStorage.clear();
60 if ("serviceWorker" in navigator) {
61 caches.keys().then((cacheNames) => {
62 for (const cacheName of cacheNames) {
63 caches.delete(cacheName);
64 }
65 });
66 }
67 indexedDB.deleteDatabase("music-files").onsuccess = () => {
68 location.hash = "";
69 location.reload();
70 };
71 }}
72 >
73 Reset
74 </button>
75 </div>
76 </dialog>
77 </div>
78 ),
79 root,
80 );
81} else {
82 if (!version) {
83 try {
84 localStorage.setItem("app-version", currentVersion);
85 } catch {}
86 }
87
88 loadDataFromDB()
89 .then(() => {
90 root.innerHTML = "";
91 render(() => <App />, root);
92 })
93 .catch((e) => {
94 root.innerHTML = e.toString();
95 });
96}
97