diff --git a/package.json b/package.json index 2c6576d..df31c44 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "core-js": "^3.6.5", "plyr": "^3.6.2", "register-service-worker": "^1.7.1", + "retrobus": "^1.0.1", "vue": "^2.6.11", "vue-i18n": "^8.17.3", "vue-router": "^3.2.0", diff --git a/src/App.vue b/src/App.vue index cdc5f4e..e67e274 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,10 +1,23 @@ + + diff --git a/src/hooks/useTimer.ts b/src/hooks/useTimer.ts index eee8243..ffc8e4f 100644 --- a/src/hooks/useTimer.ts +++ b/src/hooks/useTimer.ts @@ -1,4 +1,10 @@ -import { ref, computed, watch } from '@vue/composition-api' +import { + ref, + computed, + watch, + onMounted, + onUnmounted +} from '@vue/composition-api' import { StopwatchState } from '@/types/StopwatchState' import { notify } from '@/utils/notification' import { useInterval } from './useInterval' @@ -84,6 +90,23 @@ export const useTimer = () => { isDev1Turn.value = true } + const handleSpacebar = (event: KeyboardEvent) => { + if (event.target !== document.body) { + return + } + if (event.charCode === 32) { + timeState.value === 'started' ? pause() : start() + } + } + + onMounted(() => { + window.addEventListener('keypress', handleSpacebar) + }) + + onUnmounted(() => { + window.removeEventListener('keypress', handleSpacebar) + }) + return { interval, start, diff --git a/src/registerServiceWorker.ts b/src/registerServiceWorker.ts index e3429ba..b9fef2e 100644 --- a/src/registerServiceWorker.ts +++ b/src/registerServiceWorker.ts @@ -3,14 +3,13 @@ import { register } from 'register-service-worker' import { setNotificationInstance, notify } from './utils/notification' import i18n from '@/i18n' +import { emit } from 'retrobus' if (process.env.NODE_ENV === 'production') { register(`${process.env.BASE_URL}service-worker.js`, { ready(sw) { sw.getNotifications().then((notifs) => { - notifs - // .filter((notif) => notif.tag === 'new-version') - .forEach((notif) => notif.close()) + notifs.forEach((notif) => notif.close()) }) console.log('App is being served from cache by a service worker') setNotificationInstance((title, options?) => @@ -35,6 +34,7 @@ if (process.env.NODE_ENV === 'production') { body: i18n.t('notification.update.body').toString(), tag: 'new-version' }) + emit('new-version') }, offline() { console.log( diff --git a/src/utils/spotify.ts b/src/utils/spotify.ts index 9f6f79a..2eb8bd9 100644 --- a/src/utils/spotify.ts +++ b/src/utils/spotify.ts @@ -1,10 +1,4 @@ -const PLAYLIST_LINKS = [ - 'spotify:playlist:0vvXsWCC9xrXsKd4FyS8kM', - 'spotify:playlist:5G1gAuXjmJHgEoI7XzrXOP', - 'spotify:playlist:5OJs7eATLrvZ2Ea9als3lK', - 'spotify:playlist:5NKv3Ucc0kqe1Me7CEdWty', - 'spotify:playlist:0jGkhFr4yxLlHnpRg2Njj4' -] +let PLAYLIST_LINKS: string[] | null = null interface SpotifyTracks { items: Array<{ track: { name: string } }> @@ -40,6 +34,15 @@ export const redirectToSpotifyConnect = () => { } export const playOnSpotify = async (token: string, deviceId: string) => { + if (process.env.VUE_APP_PLAYLIST_URL && !PLAYLIST_LINKS) { + const playlistResult = await fetch(process.env.VUE_APP_PLAYLIST_URL) + PLAYLIST_LINKS = (await playlistResult.json()).playlists + } + + if (!PLAYLIST_LINKS) { + return + } + const playlistId = PLAYLIST_LINKS[random(PLAYLIST_LINKS.length - 1)] .split(':') .pop() diff --git a/yarn.lock b/yarn.lock index 567442d..b8b33fc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7418,6 +7418,11 @@ ret@~0.1.10: resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg== +retrobus@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/retrobus/-/retrobus-1.0.1.tgz#69bd7192b363d0aa4e47350e209bc1b07439aab3" + integrity sha512-TLaSvnycUsro1+L62C+J+bmD3Y/BWnOZ4L/4qMBJvkfecFodo7QgtVWLXU8Zs4tAOoPMUcu62dSy2CsP4QyuGA== + retry@^0.12.0: version "0.12.0" resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"