hawk / files / public / js / main.js
main.js
a95a53d1 2023-10-17 Pavel Safronov | 1 | // set favicon based on environment |
c188f134 2023-10-16 Pavel Safronov | 2 | const isLocal = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"; |
| 3 | if (isLocal) { | |
| 4 | document.querySelector("link[rel='icon']").href = "images/favicon-local.png"; | |
| 5 | } | |
| 6 | ||
a95a53d1 2023-10-17 Pavel Safronov | 7 | // set version and updated info |
1e82c571 2023-10-17 Pavel Safronov | 8 | const params = { |
| 9 | version: hawkInfo.version, | |
| 10 | updated: hawkInfo.updated | |
| 11 | }; | |
| 12 | document.getElementById("info").innerHTML = createTemplatedHtml('app-info', params) | |
187c6bc2 2023-10-13 Pavel Safronov | 13 | |
a95a53d1 2023-10-17 Pavel Safronov | 14 | // handle redirect from Spotify |
601035fb 2023-10-12 Pavel Safronov | 15 | const urlParams = new URLSearchParams(window.location.search); |
| 16 | const code = urlParams.get('code'); | |
| 17 | const error = urlParams.get('error'); | |
| 18 | if (code) { | |
| 19 | getToken(code).then(() => { | |
| 20 | window.history.replaceState({}, document.title, '/'); | |
0e0eaeff 2023-10-13 Pavel Safronov | 21 | window.location = getSpotifyRedirectURL(); |
601035fb 2023-10-12 Pavel Safronov | 22 | }) |
| 23 | } else if (error) { | |
| 24 | console.error(error); | |
| 25 | window.history.replaceState({}, document.title, '/'); | |
0e0eaeff 2023-10-13 Pavel Safronov | 26 | window.location = getSpotifyRedirectURL(); |
601035fb 2023-10-12 Pavel Safronov | 27 | } |
| 28 | ||
a95a53d1 2023-10-17 Pavel Safronov | 29 | // determine if the user is signed in or not |
| 30 | // hide and show appropriate elements, based on class names | |
601035fb 2023-10-12 Pavel Safronov | 31 | const accessToken = localStorage.getItem('access_token'); |
a95a53d1 2023-10-17 Pavel Safronov | 32 | const userIsSignedIn = !accessToken |
| 33 | if (userIsSignedIn) { | |
731cddfd 2023-10-14 Pavel Safronov | 34 | hideOrShow('signedin', false); |
| 35 | hideOrShow('signedout', true); | |
601035fb 2023-10-12 Pavel Safronov | 36 | } else { |
731cddfd 2023-10-14 Pavel Safronov | 37 | hideOrShow('signedin', true); |
| 38 | hideOrShow('signedout', false); | |
cc1af884 2023-10-13 Pavel Safronov | 39 | setCurrentPlayingSong(); |
| 40 | setInterval(setCurrentPlayingSong, 5 * 1000); | |
2c1835af 2023-10-19 Pavel Safronov | 41 | } |
| 42 | ||
| 43 | // periodically lookup the active device | |
660845f9 2023-10-20 Pavel Safronov | 44 | configureDevicesList(); |
| 45 | ||
| 46 | // configure times | |
| 47 | configureTimesList(); |