diff --git a/package-lock.json b/package-lock.json index 3ec73d8..ec205a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6561,6 +6561,11 @@ } } }, + "fast-average-color": { + "version": "6.4.2", + "resolved": "https://registry.npmjs.org/fast-average-color/-/fast-average-color-6.4.2.tgz", + "integrity": "sha512-aVtrSOl5m9E3dSzJTQZtm4rgAtTjTILhlLDK8UB+nseLr4Ss+rtDUVESPEGERiQ/tNckWCECQtxGvo1ickd6yA==" + }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -12540,6 +12545,11 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.9.tgz", "integrity": "sha512-nQTTcUu+ATDbrSD1BZHr5kgSD4oF8OFjxun8uAaL8RwPBacGBNPf/yAuVVdx17N8XNzRDMrZ9XcKZHCjPW+9ew==" }, + "react-ga": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/react-ga/-/react-ga-3.3.0.tgz", + "integrity": "sha512-o8RScHj6Lb8cwy3GMrVH6NJvL+y0zpJvKtc0+wmH7Bt23rszJmnqEQxRbyrqUzk9DTJIHoP42bfO5rswC9SWBQ==" + }, "react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", diff --git a/package.json b/package.json index f0d7b69..33589cf 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,11 @@ "@testing-library/jest-dom": "^5.14.1", "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^12.8.3", + "fast-average-color": "^6.4.2", "ky": "^0.28.5", "react": "^17.0.2", "react-dom": "^17.0.2", + "react-ga": "^3.3.0", "react-router-dom": "^5.2.1", "react-scripts": "4.0.3", "web-vitals": "^1.1.2" @@ -39,4 +41,4 @@ "last 1 safari version" ] } -} +} \ No newline at end of file diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000..857a981 Binary files /dev/null and b/public/favicon.ico differ diff --git a/public/manifest.json b/public/manifest.json index 3d6e67b..6432b8f 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -1,25 +1,26 @@ { - "short_name": "React App", - "name": "Create React App Sample", + "version": "1.3.0", + "short_name": "radio-rolnik", + "name": "Radio Rolnik Nysa", + "description": "", "icons": [ { - "src": "favicon.ico", - "sizes": "64x64 32x32 24x24 16x16", - "type": "image/x-icon" + "src": "/media/icon_512.png", + "type": "image/png", + "sizes": "512x512", + "purpose": "maskable" }, { - "src": "logo192.png", + "src": "/media/icon_256.png", "type": "image/png", - "sizes": "192x192" - }, - { - "src": "logo512.png", - "type": "image/png", - "sizes": "512x512" + "sizes": "256x256" } ], - "start_url": ".", + "start_url": "/", + "scope": "/", "display": "standalone", - "theme_color": "#000000", - "background_color": "#ffffff" + "orientation": "portrait", + "background_color": "#313131", + "theme_color": "#ca3e47", + "lang": "pl" } \ No newline at end of file diff --git a/public/media/icon.png b/public/media/icon.png new file mode 100644 index 0000000..bc601f0 Binary files /dev/null and b/public/media/icon.png differ diff --git a/public/media/icon_192.png b/public/media/icon_192.png new file mode 100644 index 0000000..88dbe5e Binary files /dev/null and b/public/media/icon_192.png differ diff --git a/public/media/icon_256.png b/public/media/icon_256.png new file mode 100644 index 0000000..fbac485 Binary files /dev/null and b/public/media/icon_256.png differ diff --git a/public/media/icon_512.png b/public/media/icon_512.png new file mode 100644 index 0000000..bc601f0 Binary files /dev/null and b/public/media/icon_512.png differ diff --git a/src/Cache.js b/src/Cache.js index 22c1305..5fa106c 100644 --- a/src/Cache.js +++ b/src/Cache.js @@ -2,17 +2,100 @@ import ky from 'ky' var tracks = {} +class Track { + constructor(tdata) { + Object.assign(this, tdata) + + this.stats = { + up: -1, + down: -1, + } + this.rank = -1 + this.cast = '' + + this.getVote() + } + + get listen() { + return `https://open.spotify.com/track/${this.id}` + } + + get share() { + return `https://${document.location.origin}/utwor/${this.id}` + } + + setVote(value) { + return new Promise((resolve, reject) => { + if (value === this.cast) return + ky.patch(`/api/vote/${this.id}`, { json: { uid: 'mathias', value } }) + .json() + .then((ok) => { + if (ok) { + this.cast = value + this.getStats() + } + resolve(ok) + }) + .catch((err) => reject(err)) + }) + } + + getVote() { + return new Promise((resolve, reject) => { + ky.get(`/api/vote/${this.id}/cast`, { searchParams: { uid: 'mathias' } }) + .text() + .then((cast) => { + this.cast = cast + resolve(cast) + }) + .catch((err) => { + console.error(err) + }) + }) + } + + getStats() { + return new Promise((resolve, reject) => { + getStats(this.id) + .then((stats) => { + this.stats = stats + resolve(stats) + }) + .catch((err) => { + console.error(err) + }) + }) + } +} + +function getStats(id) { + return new Promise((resolve, reject) => { + ky.get(`/api/vote/${id}`) + .json() + .then((stats) => { + resolve(stats) + }) + .catch((err) => reject(err)) + }) +} + function get(id) { return new Promise((resolve, reject) => { if (typeof tracks[id] === 'undefined') { - download(id).then((track) => resolve(track)) + download(id) + .then((track) => resolve(track)) + .catch((err) => reject(err)) } else resolve(tracks[id]) }) } function getMultiple(ids) { return new Promise((resolve, reject) => { - Promise.all(ids.map((id) => get(id))).then((tracks) => resolve(tracks)) + Promise.all(ids.map((id) => get(id))) + .then((tracks) => resolve(tracks)) + .catch((err) => { + console.error(err) + }) }) } @@ -21,8 +104,8 @@ function download(id) { ky.get('/api/track/' + id) .json() .then((track) => { - tracks[track.id] = track - resolve(track) + tracks[track.id] = new Track(track) + resolve(tracks[track.id]) }) .catch((err) => reject(err)) }) @@ -37,4 +120,4 @@ function search(query) { }) } -export { tracks, get, getMultiple, search } +export { tracks, get, getMultiple, search, getStats } diff --git a/src/Color.js b/src/Color.js new file mode 100644 index 0000000..29a2cff --- /dev/null +++ b/src/Color.js @@ -0,0 +1,23 @@ +import FastAverageColor from 'fast-average-color' + +var fac = new FastAverageColor(), + def = document.documentElement.style.getPropertyValue('--deg') + +function setColor(target, lvl) { + fac + .getColorAsync(target) + .then(({ value: [r, g, b] }) => { + let color = + Math.atan2(1.732050808 * (g - b), 2 * r - g - b) * 57.295779513 + + return color + }) + .catch((err) => { + return def + }) + .then((color) => { + document.documentElement.style.setProperty('--deg', color + 'deg') + }) +} + +export { setColor } diff --git a/src/components/History/History.css b/src/components/History/History.css index 59ee6d4..099ab2d 100644 --- a/src/components/History/History.css +++ b/src/components/History/History.css @@ -15,4 +15,15 @@ .history-section.now > .track { background-color: var(--main); box-shadow: 2px 2px 4px #6363637d; + color: #fffd; +} + +.history-section.now > .track .track-artist, +.history-section.now > .track .track-timestamp { + color: #fffa; +} + +.history-section.next > .track .track-timestamp, +.history-section.now > .track .track-timestamp { + font-size: 1.25em; } diff --git a/src/components/History/History.js b/src/components/History/History.js index a20f2b1..5c81b83 100644 --- a/src/components/History/History.js +++ b/src/components/History/History.js @@ -14,13 +14,13 @@ function History({ next, tracks }) { {next && (
Następny: - +
)}
Gra teraz: - +
@@ -30,7 +30,7 @@ function History({ next, tracks }) { ? new Date(track.timestamp).toISOString().slice(11, 16) : '-:-' - return + return })}
diff --git a/src/components/Home/Home.js b/src/components/Home/Home.js deleted file mode 100644 index 15c97f8..0000000 --- a/src/components/Home/Home.js +++ /dev/null @@ -1,5 +0,0 @@ -function Home() { - return
-} - -export default Home diff --git a/src/components/Info/Info.css b/src/components/Info/Info.css index 6f7500c..a738f7b 100644 --- a/src/components/Info/Info.css +++ b/src/components/Info/Info.css @@ -22,7 +22,6 @@ background: linear-gradient(180deg, white, transparent); height: 100%; padding: 1em; - padding-bottom: 6em; box-shadow: 0 -1px 11px 2px #6b6b6b52; } @@ -36,10 +35,12 @@ font-size: 0.75em; background-color: var(--light); margin: 1em 0; + display: flex; } .info-balance > div { display: inline-block; + min-width: 2em; } .info-balance > .up { @@ -61,13 +62,34 @@ padding: 0; } -.info-list > div > i { +.info-list > .buttons > i { cursor: pointer; width: 100%; font-size: 1.5em; padding: 0.25em; } +.info-list > .buttons > i[data-set='true'] { + background-color: var(--light); + color: var(--main); + cursor: default; +} + .info-list > div > i:hover { - background-color: #cccc; + /* background-color: #cccc; */ + color: var(--main); +} + +.info-data { + text-align: left; + font-size: 0.7em; + color: #6666; + padding: 0.1em 0 0 0.5em; + user-select: all; +} + +.info-meta { + padding-bottom: 0.3em; + font-size: 0.9em; + display: inline-block; } diff --git a/src/components/Info/Info.js b/src/components/Info/Info.js index cb52b1e..6e32b09 100644 --- a/src/components/Info/Info.js +++ b/src/components/Info/Info.js @@ -1,28 +1,75 @@ -import { useEffect, useState } from 'react' -import { useParams } from 'react-router-dom' +import { useContext, useEffect, useState } from 'react' +import { useParams, Redirect } from 'react-router-dom' import { get } from '../../Cache' +import PlayingContext from '../../contexts/Playing' + import './Info.css' +var last + function Info() { const { id } = useParams(), [track, setTrack] = useState({ - title: 'Tytuł', - votes: {}, + title: '-', + stats: {}, album: {}, - artists: ['Autor'], - }) + artists: ['-'], + }), + [meta, setMeta] = useState('...'), + playing = useContext(PlayingContext) + + if (!id) last = playing.id ?? '' + else last = id useEffect(() => { - get(id).then((track) => { - setTrack(track) - }) - }, [id]) + setMeta(`Pobieranie... ${last}`) + get(last) + .then((track) => { + track + .getStats() + .then((stats) => { + track + .getVote() + .then(() => { + setTrack(track) + setMeta(false) + }) + .catch((err) => console.error(err)) + }) + .catch((err) => console.error(err)) + }) + .catch((err) => { + setMeta(`Wystąpił błąd przy pobieraniu informacji o utworze`) + }) + }, [last]) // eslint-disable-line - const percent = (track.votes.up / (track.votes.up + track.votes.down)) * 100 + if (!id && last) return - document.title = `${track.title} | radio-rolnik` + function vote(value) { + track + .setVote(value) + .then((ok) => { + track + .getStats() + .then(() => { + ok + ? setMeta(`Zmieniono głos na ${{ up: '👍', down: '👎' }[value]}`) + : setMeta('Wystąpił bład przy zmienianiu głosu') + }) + .catch((err) => console.error(err)) + }) + .catch((err) => console.error(err)) + } + + const total = track.stats.up + track.stats.down, + percent = + total > 0 + ? (track.stats.up / (track.stats.up + track.stats.down)) * 100 + : 50 + + document.setTitle(track.title) return (
@@ -36,30 +83,81 @@ function Info() { {track.artists[0]}
- {track.votes.up}  + {track.stats.up} 
- {track.votes.down} + {track.stats.down}
-
-
- - + + {meta && {meta}} + + {track.id && ( +
+
+ vote('up')} + > + vote('down')} + > +
+ +
+ { + if (navigator.share) + navigator.share({ + url: track.share, + title: 'radio-rolnik', + text: `Sprawdź "${track.title}" na radio-rolnik`, + }) + }} + > + { + window.open(track.listen) + }} + > + { + window.confirm(`Napewno chcesz zgłosić ten utwór?`) && + track.setVote('report') + }} + > +
+ +
+ {track.artists.join(', ')} +
+
+ + {track.album.name ?? '-'} ({track.album.year ?? '-'}) + +
+ +
Liczba głosów: {total}
+ +
Miejsce: #{track.stats.rank}
+ + {track.banned && ( +
+ + Utwór zablokowany + +
+ )}
-
- - - -
-
{`${track.title} - ${track.artists.join(', ')} - ${ - track.album.name - } (${track.album.year})`}
-
Liczba głosów: {track.votes.up + track.votes.down}
-
Miejsce: {track.votes.rank}
-
Zablokowane: {track.banned ? 'Tak' : 'Nie'}
-
+ )}
+ {id}
) } diff --git a/src/components/Main/Main.css b/src/components/Main/Main.css index 3b2607f..bd9070d 100644 --- a/src/components/Main/Main.css +++ b/src/components/Main/Main.css @@ -5,7 +5,7 @@ justify-content: flex-end; background-color: var(--light); font-family: 'Quicksand'; - overflow: hidden auto; + overflow: hidden; } .wrapper { @@ -13,4 +13,5 @@ margin: auto; box-sizing: border-box; padding-bottom: 8em; + overflow-y: auto; } diff --git a/src/components/Main/Main.js b/src/components/Main/Main.js index 26310a1..ea45fde 100644 --- a/src/components/Main/Main.js +++ b/src/components/Main/Main.js @@ -5,7 +5,7 @@ import { Redirect, } from 'react-router-dom' import { useEffect, useState } from 'react' -import ky from 'ky' +import ga from 'react-ga' import Nav from '../Nav/Nav' import History from '../History/History' @@ -18,11 +18,21 @@ import { get, getMultiple } from '../../Cache' import { addEvent } from '../../socket' import PlayingContext from '../../contexts/Playing' +import GaContext from '../../contexts/Ga' import './Main.css' +ga.initialize('UA-150749288-3') + document.setTitle = (prefix, suffix) => { - document.title = `${prefix} | ${suffix ?? 'radio-rolnik'}` + const title = `${prefix} | ${suffix ?? 'radio-rolnik'}` + + // dont repeat same title + if (document.title === title) return + + document.title = title + + ga.pageview(document.location.pathname) } function Main() { @@ -35,74 +45,85 @@ function Main() { useEffect(() => { addEvent('status', ({ trackid, progress, paused }) => { - get(trackid).then((track) => { - setPlaying(track) - setElapsed(progress) - setPaused(paused) - }) + get(trackid) + .then((track) => { + setPlaying(track) + setElapsed(progress) + setPaused(paused) + }) + .catch((err) => { + setPaused(true) + }) }) addEvent('next', ({ trackid }) => { - get(trackid).then((track) => { - setNext(track) - }) + get(trackid) + .then((track) => { + setNext(track) + }) + .catch((err) => console.error(err)) }) addEvent('previous', ({ trackids, timestamps }) => { - getMultiple(trackids).then((tracks) => - // insert timestamps to tracks - setPrevious( - tracks.map((track, i) => ({ ...track, timestamp: timestamps[i] })) - ) - ) + getMultiple(trackids) + .then((tracks) => { + // insert timestamps to tracks + tracks.map((track, i) => (track.timestamp = timestamps[i])) + setPrevious(tracks) + }) + .catch((err) => console.error(err)) }) addEvent('top', ({ trackids }) => { - getMultiple(trackids).then((tracks) => setTop(tracks)) + getMultiple(trackids) + .then((tracks) => setTop(tracks)) + .catch((err) => console.error(err)) }) }, []) return ( - -
- - - - - + + +
+ + + + + - - - + + + - - - + + + - - - + + + - } /> - -
-
+ } /> +
+
+
+ ) } diff --git a/src/components/Nav/Nav.js b/src/components/Nav/Nav.js index a69bba0..a4e78a4 100644 --- a/src/components/Nav/Nav.js +++ b/src/components/Nav/Nav.js @@ -46,7 +46,7 @@ function Nav({ buttons }) { })() // don't update for custom titles - label && (document.title = `${label} | radio-rolnik`) + label && document.setTitle(label) }, [location]) // eslint-disable-line return ( diff --git a/src/components/Search/Search.js b/src/components/Search/Search.js index 3aa8dec..1a609e4 100644 --- a/src/components/Search/Search.js +++ b/src/components/Search/Search.js @@ -74,7 +74,7 @@ function Search() { )} {results.map((track) => { - return + return })} diff --git a/src/components/Status/Status.js b/src/components/Status/Status.js index b0b6db0..d6a56e3 100644 --- a/src/components/Status/Status.js +++ b/src/components/Status/Status.js @@ -1,11 +1,13 @@ import { useContext, useEffect, useState } from 'react' import { Link } from 'react-router-dom' +import { setColor } from '../../Color' + import PlayingContext from '../../contexts/Playing' import './Status.css' -function Status({ progress, paused, onEnd }) { +function Status({ progress, paused }) { const { id, title = '-', @@ -18,6 +20,10 @@ function Status({ progress, paused, onEnd }) { const step = 500, tread = step / duration + useEffect(() => { + if (album.art) setColor(album.art, 1) + }, [album.art]) + useEffect(() => { setElapsed(progress ? progress / duration : 0) }, [progress, duration]) diff --git a/src/components/Top/Top.js b/src/components/Top/Top.js index 0ac62dc..bb48567 100644 --- a/src/components/Top/Top.js +++ b/src/components/Top/Top.js @@ -6,7 +6,7 @@ function Top(props) { return (
{props.tracks.map((track, rank) => { - return + return })}
) diff --git a/src/components/Track/Track.css b/src/components/Track/Track.css index 4d90860..ba2c6d7 100644 --- a/src/components/Track/Track.css +++ b/src/components/Track/Track.css @@ -29,6 +29,11 @@ user-select: none; } +.track-timestamp { + font-size: 0.75em; + margin-left: -0.4em; +} + .track-section-b { width: 100%; overflow: hidden; @@ -37,11 +42,12 @@ display: flex; flex-direction: row; align-items: center; + margin-right: 1em; } -.track-timestamp { - font-size: 0.75em; - margin-left: -0.4em; +.track-section-a, +.track-section-b { + cursor: pointer; } .track-rank:before { @@ -68,12 +74,12 @@ .track-icons { float: right; - padding: 0 0.5em; margin: auto; } .track-icons > i { cursor: pointer; + padding: 0.5em; } .track-more { @@ -84,7 +90,7 @@ top: 0; border-radius: inherit; background: hsl(12deg, 4%, 20%); - transition: 0.3s ease-in-out; + transition: 0.3s cubic-bezier(0.7, 0, 0.3, 1); display: flex; flex-direction: row; @@ -112,6 +118,12 @@ width: 100%; } +.track-more-button > i[data-set='true'] { + background-color: var(--dark); + color: var(--main); + cursor: default; +} + .track-more-button:hover { background-color: #fff2; } diff --git a/src/components/Track/Track.js b/src/components/Track/Track.js index 1995667..297fa0e 100644 --- a/src/components/Track/Track.js +++ b/src/components/Track/Track.js @@ -1,20 +1,36 @@ import { useState } from 'react' -import { Link } from 'react-router-dom' +import { Link, useHistory } from 'react-router-dom' import './Track.css' -function Track({ - id, - title = '-', - artists = ['-'], - album = {}, - timestamp, - rank, -}) { - const [open, setOpen] = useState(false) +function Track({ track, timestamp, rank }) { + const [open, setOpen] = useState(false), + { + id, + title = '-', + artists = ['-'], + album = {}, + votes = {}, + cast = '', + } = track ?? {}, + history = useHistory() + + function vote(value) { + track + .setVote(value) + .then((ok) => ok && setOpen(false)) + .catch((err) => console.error(err)) + } + + function info() { + history.push(`/utwor/${id}`) + } return ( -
-
+
+
info()} + > {timestamp && {timestamp}} {rank && {rank}}
-
- - {title} - +
info()}> + {title} {artists.join(', ')}
@@ -41,21 +55,32 @@ function Track({
- + vote('up')} + >
+
- + vote('down')} + >
+ -
{ - setOpen(false) - }} - > - + +
+ { + setOpen(false) + }} + >
diff --git a/src/contexts/Ga.js b/src/contexts/Ga.js new file mode 100644 index 0000000..90ae41e --- /dev/null +++ b/src/contexts/Ga.js @@ -0,0 +1,5 @@ +import { createContext } from "react"; + +const GaContext = createContext('ga') + +export default GaContext \ No newline at end of file diff --git a/src/index.css b/src/index.css index 11425f6..4088e73 100644 --- a/src/index.css +++ b/src/index.css @@ -8,7 +8,7 @@ body { } :root { - --deg: 170deg; + --deg: 0deg; --white: hsl(0, 0%, 100%); --light: hsl(var(--deg) 50% 88%); --main: hsl(var(--deg), 57%, 52%);