55 playlistLibraryActions ,
66 playlistLibraryHelpers
77} from '@audius/common/store'
8- import { fork , put , takeEvery } from 'typed-redux-saga'
8+ import { fork , put , select , takeEvery } from 'typed-redux-saga'
99
1010import { updateProfileAsync } from 'common/store/profile/sagas'
1111import { waitForWrite } from 'utils/sagaHelpers'
@@ -14,8 +14,11 @@ import { watchAddToFolderSaga } from './watchAddToFolderSaga'
1414import { watchReorderLibrarySaga } from './watchReorderLibrarySaga'
1515
1616const { update } = playlistLibraryActions
17- const { removePlaylistLibraryDuplicates, removeFromPlaylistLibrary } =
18- playlistLibraryHelpers
17+ const {
18+ getPlaylistsNotInLibrary,
19+ removePlaylistLibraryDuplicates,
20+ removeFromPlaylistLibrary
21+ } = playlistLibraryHelpers
1922
2023function * watchUpdatePlaylistLibrary ( ) {
2124 yield * takeEvery (
@@ -47,9 +50,56 @@ export function* removePlaylistFromLibrary(id: PlaylistLibraryID) {
4750 yield * put ( update ( { playlistLibrary : updatedLibrary } ) )
4851}
4952
53+ function * watchAddAccountPlaylist ( ) {
54+ yield * takeEvery (
55+ accountActions . addAccountPlaylist . type ,
56+ function * handleAddAccountPlaylist ( ) {
57+ yield * waitForWrite ( )
58+ const account = yield * select ( ( state ) => state . account )
59+ const playlistLibrary = account . playlistLibrary
60+ if ( ! playlistLibrary ) return
61+ yield * put ( update ( { playlistLibrary } ) )
62+ }
63+ )
64+ }
65+
66+ function * watchRepairEmptyPlaylistLibrary ( ) {
67+ yield * takeEvery (
68+ accountActions . fetchAccountSucceeded . type ,
69+ function * handleRepairEmptyPlaylistLibrary ( ) {
70+ yield * waitForWrite ( )
71+ const account = yield * select ( ( state ) => state . account )
72+ const { collections, playlistLibrary } = account
73+ const collectionCount = Object . keys ( collections ) . length
74+ if ( collectionCount === 0 ) return
75+
76+ const missing = getPlaylistsNotInLibrary (
77+ playlistLibrary ?? null ,
78+ collections
79+ )
80+ const missingCount = Object . keys ( missing ) . length
81+ if ( missingCount === 0 ) return
82+
83+ const existingContents = playlistLibrary ?. contents ?? [ ]
84+ const newContents = [
85+ ...existingContents ,
86+ ...Object . values ( missing ) . map ( ( c ) => ( {
87+ playlist_id : c . id ,
88+ type : 'playlist' as const
89+ } ) )
90+ ]
91+ yield * put (
92+ update ( { playlistLibrary : { contents : newContents } } )
93+ )
94+ }
95+ )
96+ }
97+
5098export default function sagas ( ) {
5199 const sagas = [
52100 watchUpdatePlaylistLibrary ,
101+ watchAddAccountPlaylist ,
102+ watchRepairEmptyPlaylistLibrary ,
53103 watchReorderLibrarySaga ,
54104 watchAddToFolderSaga
55105 ]
0 commit comments