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