@@ -157,46 +157,61 @@ async function onApiLoaded() {
157
157
} satisfies QueueResponse ) ;
158
158
} ) ;
159
159
160
- window . ipcRenderer . on ( 'ytmd:add-to-queue' , ( _ , videoId : string ) => {
161
- const queue = document . querySelector < QueueElement > ( '#queue' ) ;
162
- const app = document . querySelector < YouTubeMusicAppElement > ( 'ytmusic-app' ) ;
163
- if ( ! app ) return ;
164
-
165
- const store = queue ?. queue . store . store ;
166
- if ( ! store ) return ;
167
-
168
- app . networkManager
169
- . fetch ( '/music/get_queue' , {
170
- queueContextParams : store . getState ( ) . queue . queueContextParams ,
171
- queueInsertPosition : 'INSERT_AT_END' ,
172
- videoIds : [ videoId ] ,
173
- } )
174
- . then ( ( result ) => {
175
- if (
176
- result &&
177
- typeof result === 'object' &&
178
- 'queueDatas' in result &&
179
- Array . isArray ( result . queueDatas )
180
- ) {
181
- queue ?. dispatch ( {
182
- type : 'ADD_ITEMS' ,
183
- payload : {
184
- nextQueueItemId : store . getState ( ) . queue . nextQueueItemId ,
185
- index : store . getState ( ) . queue . items . length ?? 0 ,
186
- items : result . queueDatas
187
- . map ( ( it ) =>
188
- typeof it === 'object' && it && 'content' in it
189
- ? it . content
190
- : null ,
191
- )
192
- . filter ( Boolean ) ,
193
- shuffleEnabled : false ,
194
- shouldAssignIds : true ,
195
- } ,
196
- } ) ;
197
- }
198
- } ) ;
199
- } ) ;
160
+ window . ipcRenderer . on (
161
+ 'ytmd:add-to-queue' ,
162
+ ( _ , videoId : string , queueInsertPosition : string ) => {
163
+ const queue = document . querySelector < QueueElement > ( '#queue' ) ;
164
+ const app = document . querySelector < YouTubeMusicAppElement > ( 'ytmusic-app' ) ;
165
+ if ( ! app ) return ;
166
+
167
+ const store = queue ?. queue . store . store ;
168
+ if ( ! store ) return ;
169
+
170
+ app . networkManager
171
+ . fetch ( '/music/get_queue' , {
172
+ queueContextParams : store . getState ( ) . queue . queueContextParams ,
173
+ queueInsertPosition,
174
+ videoIds : [ videoId ] ,
175
+ } )
176
+ . then ( ( result ) => {
177
+ if (
178
+ result &&
179
+ typeof result === 'object' &&
180
+ 'queueDatas' in result &&
181
+ Array . isArray ( result . queueDatas )
182
+ ) {
183
+ const queueItems = store . getState ( ) . queue . items ;
184
+ const queueItemsLength = queueItems . length ?? 0 ;
185
+ queue ?. dispatch ( {
186
+ type : 'ADD_ITEMS' ,
187
+ payload : {
188
+ nextQueueItemId : store . getState ( ) . queue . nextQueueItemId ,
189
+ index :
190
+ queueInsertPosition === 'INSERT_AFTER_CURRENT_VIDEO'
191
+ ? queueItems . findIndex (
192
+ ( it ) =>
193
+ (
194
+ it . playlistPanelVideoRenderer ||
195
+ it . playlistPanelVideoWrapperRenderer
196
+ ?. primaryRenderer . playlistPanelVideoRenderer
197
+ ) ?. selected ,
198
+ ) + 1 || queueItemsLength
199
+ : queueItemsLength ,
200
+ items : result . queueDatas
201
+ . map ( ( it ) =>
202
+ typeof it === 'object' && it && 'content' in it
203
+ ? it . content
204
+ : null ,
205
+ )
206
+ . filter ( Boolean ) ,
207
+ shuffleEnabled : false ,
208
+ shouldAssignIds : true ,
209
+ } ,
210
+ } ) ;
211
+ }
212
+ } ) ;
213
+ } ,
214
+ ) ;
200
215
window . ipcRenderer . on (
201
216
'ytmd:move-in-queue' ,
202
217
( _ , fromIndex : number , toIndex : number ) => {
0 commit comments