Skip to content

Commit 11517fc

Browse files
committed
Implement album history feature
1 parent f5068e1 commit 11517fc

File tree

14 files changed

+171
-26
lines changed

14 files changed

+171
-26
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREDITS.md

CREDITS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,26 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
821821
OTHER DEALINGS IN THE SOFTWARE.
822822

823823

824+
-------------------------------------------------------------------------------
825+
826+
## Project
827+
pretty-bytes
828+
829+
### Source
830+
https://github.com/sindresorhus/pretty-bytes
831+
832+
### License
833+
MIT License
834+
835+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
836+
837+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
838+
839+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
840+
841+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
842+
843+
824844
-------------------------------------------------------------------------------
825845

826846
## Project

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"localforage": "^1.10.0",
2020
"lodash": "^4.17.21",
2121
"moment": "^2.29.4",
22+
"pretty-bytes": "^6.1.0",
2223
"query-string": "^8.1.0",
2324
"react": "^18.2.0",
2425
"react-colorful": "^5.6.1",

src/components/settings/GeneralSettings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import FirstDayOfWeekField from './FirstDayOfWeekField'
1010
import FetchExtraDataField from './FetchExtraDataField'
1111
import LabelBlocklistField from './LabelBlocklistField'
1212
import MinimumSavedTracksField from './MinimumSavedTracksField'
13+
import HistoryField from './HistoryField'
1314

1415
/**
1516
* Render general settings fields
@@ -27,6 +28,7 @@ function GeneralSettings() {
2728
<TimePeriodField />
2829
<FirstDayOfWeekField />
2930
<UriLinksField />
31+
<HistoryField />
3032
<LabelBlocklistField />
3133
{user && <DataInfo />}
3234
{hasAppData && <DataReset />}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { useEffect, useState } from 'react'
2+
import { useSelector, useDispatch } from 'react-redux'
3+
import prettyBytes from 'pretty-bytes'
4+
import { getSettings } from 'state/selectors'
5+
import { setSettings } from 'state/actions'
6+
import { defer, sleep } from 'helpers'
7+
import * as history from 'history'
8+
import { Button, Checkbox } from 'components/common'
9+
import HelpText from './HelpText'
10+
11+
function HistoryField() {
12+
const { autoHistoryUpdate } = useSelector(getSettings)
13+
const dispatch = useDispatch()
14+
const [size, setSize] = useState(0)
15+
const [cleared, setCleared] = useState(false)
16+
17+
useEffect(() => {
18+
history.size().then(setSize)
19+
}, [])
20+
21+
const clearHistory = () => {
22+
history.clear()
23+
setCleared(true)
24+
setSize(0)
25+
sleep(1200).then(() => setCleared(false))
26+
}
27+
28+
return (
29+
<div className="HistoryField Settings__field field">
30+
<label className="label has-text-light" htmlFor="autoHistoryUpdate">
31+
Album history
32+
</label>
33+
<div className="Settings__help">
34+
<HelpText>
35+
If enabled, all newly fetched albums will be automatically added to the local history
36+
data. This will hide them on subsequent refreshes, making it easier to see what's new.
37+
</HelpText>
38+
</div>
39+
<div className="control">
40+
<div className="field">
41+
<Checkbox
42+
id="autoHistoryUpdate"
43+
label="Update album history on refresh"
44+
defaultChecked={autoHistoryUpdate}
45+
onChange={(event) =>
46+
defer(dispatch, setSettings({ autoHistoryUpdate: event.target.checked }))
47+
}
48+
/>
49+
</div>
50+
</div>
51+
<Button
52+
title={
53+
cleared ? 'Album history cleared' : `Clear album history (${prettyBytes(size)} used)`
54+
}
55+
icon={cleared && 'fas fa-check-circle'}
56+
disabled={cleared}
57+
onClick={clearHistory}
58+
small
59+
/>
60+
</div>
61+
)
62+
}
63+
64+
export default HistoryField

src/helpers.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,19 @@ export function captureException(error) {
272272
*
273273
* @param {AlbumRaw[]} albumsRaw
274274
* @param {string} minDate
275+
* @param {AlbumsHistory} history
275276
*/
276-
export function mergeAlbumsRaw(albumsRaw, minDate) {
277+
export function mergeAlbumsRaw(albumsRaw, minDate, history) {
277278
const maxDate = moment().add(1, 'day').format(MomentFormat.ISO_DATE)
278279
const albumsRawMap = albumsRaw.reduce((map, album) => {
279-
if (album.releaseDate < minDate || album.releaseDate > maxDate) {
280-
return map
281-
}
280+
const { id, releaseDate, artistIds } = album
282281

283-
const matched = map[album.id]
282+
if (history.has(id)) return map
283+
if (releaseDate < minDate || releaseDate > maxDate) return map
284284

285-
if (!matched) {
286-
map[album.id] = album
287-
return map
288-
}
285+
if (id in map) merge(map[id].artistIds, artistIds)
286+
else map[id] = album
289287

290-
merge(matched.artistIds, album.artistIds)
291288
return map
292289
}, /** @type {{ [id: string]: AlbumRaw }} */ ({}))
293290

src/history.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import localForage from 'localforage'
2+
import { Base64 } from 'js-base64'
3+
4+
const storageKey = 'albums-history'
5+
6+
export async function load() {
7+
/** @type {AlbumsHistory} */
8+
const history = new Set()
9+
/** @type {Uint8Array | null} */
10+
const byteArray = await localForage.getItem(storageKey)
11+
if (!byteArray) return history
12+
13+
for (let i = 0; i < byteArray.length; i += 17) {
14+
const bytes = byteArray.slice(i, i + 17)
15+
const id = Base64.fromUint8Array(bytes).slice(0, -2)
16+
history.add(id)
17+
}
18+
19+
return history
20+
}
21+
22+
/** @param {AlbumsHistory} history */
23+
export function persist(history) {
24+
const byteArray = new Uint8Array(history.size * 17)
25+
let i = 0
26+
27+
for (const id of history) {
28+
const bytes = Base64.toUint8Array(id + 'A=')
29+
byteArray.set(bytes, i)
30+
i += 17
31+
}
32+
33+
return localForage.setItem(storageKey, byteArray)
34+
}
35+
36+
export function clear() {
37+
return localForage.removeItem(storageKey)
38+
}
39+
40+
export async function size() {
41+
/** @type {Uint8Array | null} */
42+
const byteArray = await localForage.getItem(storageKey)
43+
return byteArray?.length ?? 0
44+
}

src/sagas/notification.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { call, delay, race, select, take } from 'redux-saga/effects'
2+
import isEmpty from 'lodash/isEmpty'
23
import { createNotification } from 'helpers'
34
import { syncFinished } from 'state/actions'
45
import { getPreviousSyncMaxDate, getReleasesMaxDate, getSettings } from 'state/selectors'
@@ -26,19 +27,20 @@ function* notificationWorker() {
2627
yield delay(5 * 1000)
2728

2829
while (true) {
29-
yield take(syncFinished.type)
30-
30+
/** @type {ReturnType<typeof syncFinished>} */
31+
const action = yield take(syncFinished.type)
3132
/** @type {ReturnType<typeof getSettings>} */
32-
const { notifications } = yield select(getSettings)
33+
const { notifications, autoHistoryUpdate } = yield select(getSettings)
3334
/** @type {ReturnType<typeof getPreviousSyncMaxDate>} */
3435
const previousMaxDate = yield select(getPreviousSyncMaxDate)
3536
/** @type {ReturnType<typeof getReleasesMaxDate>} */
3637
const currentMaxDate = yield select(getReleasesMaxDate)
3738

3839
if (Notification.permission !== 'granted') continue
3940
if (!notifications) continue
40-
if (!previousMaxDate) continue
41-
if (currentMaxDate === previousMaxDate) continue
41+
if (isEmpty(action.payload.albums)) continue
42+
if (!autoHistoryUpdate && !previousMaxDate) continue
43+
if (!autoHistoryUpdate && currentMaxDate === previousMaxDate) continue
4244

4345
yield call(createNotification, 'New music has been released')
4446
}

src/sagas/root.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fork, take, takeEvery, takeLeading } from 'redux-saga/effects'
22
import { REHYDRATE } from 'redux-persist'
3+
import * as history from 'history'
34
import {
45
authorize,
56
authorizeError,
@@ -29,19 +30,15 @@ export function* rootSaga() {
2930
yield takeEvery(setSettings.type, themeUpdateSaga)
3031
yield takeEvery(setSettings.type, firstDayOfWeekUpdateSaga)
3132
yield takeEvery(reset.type, deleteAuthData)
33+
yield takeEvery(reset.type, history.clear)
3234
yield takeEvery(authorizeError.type, authorizeErrorSaga)
3335
yield takeLeading(authorize.type, authorizeSaga)
3436
yield takeLeadingCancellable(sync.type, syncCancel.type, syncSaga)
3537
yield takeLeadingCancellable(createPlaylist.type, createPlaylistCancel.type, createPlaylistSaga)
3638
yield fork(autoSyncSaga)
3739

38-
if (navigator.serviceWorker) {
39-
yield fork(updateSaga)
40-
}
41-
42-
if (window.Notification) {
43-
yield fork(notificationSaga)
44-
}
40+
if (navigator.serviceWorker) yield fork(updateSaga)
41+
if (window.Notification) yield fork(notificationSaga)
4542
}
4643

4744
export default rootSaga

src/sagas/sync.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
getUserSavedTracksPage,
1313
} from 'api'
1414
import { getAuthData, getSyncScopes } from 'auth'
15+
import { buildAlbumsMap, buildArtist, deleteLabels, mergeAlbumsRaw } from 'helpers'
16+
import * as history from 'history'
1517
import { getSettings, getReleasesMaxDate } from 'state/selectors'
1618
import {
1719
setSyncingProgress,
@@ -29,7 +31,6 @@ import {
2931
getAllCursorPaged,
3032
getAllPaged,
3133
} from './helpers'
32-
import { buildAlbumsMap, buildArtist, deleteLabels, mergeAlbumsRaw } from 'helpers'
3334

3435
const { ISO_DATE } = MomentFormat
3536
const { FOLLOWED, SAVED_ALBUMS, SAVED_TRACKS } = ArtistSource
@@ -83,8 +84,10 @@ function* syncMainSaga(action) {
8384

8485
/** @type {ReturnType<typeof getAuthData>} */
8586
const { token } = yield call(getAuthData)
87+
/** @type {Await<ReturnType<typeof history.load>>} */
88+
const albumsHistory = yield call(history.load)
8689
/** @type {ReturnType<typeof getSettings>} */
87-
const { days, fullAlbumData, labelBlocklist } = yield select(getSettings)
90+
const { days, fullAlbumData, labelBlocklist, autoHistoryUpdate } = yield select(getSettings)
8891
/** @type {ReturnType<typeof getReleasesMaxDate>} */
8992
const previousSyncMaxDate = yield select(getReleasesMaxDate)
9093

@@ -120,7 +123,7 @@ function* syncMainSaga(action) {
120123
)
121124

122125
/** @type {Await<ReturnType<typeof mergeAlbumsRaw>>} */
123-
const mergedAlbums = yield call(mergeAlbumsRaw, albumsRaw, minDate)
126+
const mergedAlbums = yield call(mergeAlbumsRaw, albumsRaw, minDate, albumsHistory)
124127
/** @type {Await<ReturnType<typeof buildAlbumsMap>>} */
125128
const albums = yield call(buildAlbumsMap, mergedAlbums, artists)
126129

@@ -129,6 +132,11 @@ function* syncMainSaga(action) {
129132
yield call(deleteLabels, albums, labelBlocklist)
130133
}
131134

135+
if (autoHistoryUpdate) {
136+
for (const album of mergedAlbums) albumsHistory.add(album.id)
137+
yield call(history.persist, albumsHistory)
138+
}
139+
132140
yield cancel(tasks)
133141
yield call(requestChannel.close)
134142
yield call(responseChannel.close)

0 commit comments

Comments
 (0)