Skip to content

Commit 06a86c5

Browse files
Update single user playlist view to remove videos immediately on exit (FreeTubeApp#7544)
* * Update single user playlist view to remove videos immediately on exit * $ Update `removeVideos` usage to remove never used `videoIds` * * Close toast early on view exit * ! Fix unable to remove videos after clicking undo * * Reverse toast message update * * Update video count after remove (before actual remove)
1 parent 76f2d5a commit 06a86c5

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

src/renderer/components/FtToast/FtToast.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ let idCounter = 0
3737
const toasts = shallowReactive([])
3838
3939
/**
40-
* @param {CustomEvent<{ message: string, time: number | null, action: Function | null }>} event
40+
* @param {CustomEvent<{ message: string, time: number | null, action: Function | null, abortSignal: AbortSignal | null }>} event
4141
*/
42-
function open({ detail: { message, time, action } }) {
42+
function open({ detail: { message, time, action, abortSignal } }) {
4343
/** @type {Toast} */
4444
const toast = {
4545
message,
@@ -50,6 +50,11 @@ function open({ detail: { message, time, action } }) {
5050
}
5151
5252
toast.timeout = setTimeout(close, time || 3000, toast)
53+
if (abortSignal != null) {
54+
abortSignal.addEventListener('abort', () => {
55+
close(toast)
56+
})
57+
}
5358
5459
nextTick(() => {
5560
toast.isOpen = true

src/renderer/helpers/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ export const ToastEventBus = new EventTarget()
167167
* @param {string} message
168168
* @param {number} time
169169
* @param {Function} action
170+
* @param {AbortSignal} abortSignal
170171
*/
171-
export function showToast(message, time = null, action = null) {
172+
export function showToast(message, time = null, action = null, abortSignal = null) {
172173
ToastEventBus.dispatchEvent(new CustomEvent('toast-open', {
173174
detail: {
174175
message,
175176
time,
176-
action
177+
action,
178+
abortSignal,
177179
}
178180
}))
179181
}

src/renderer/views/Playlist/Playlist.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default defineComponent({
5151
continuationData: this.continuationData,
5252
})
5353
}
54+
this.removeToBeDeletedVideosSometimes()
5455
next()
5556
},
5657
data: function () {
@@ -80,9 +81,9 @@ export default defineComponent({
8081
videoSearchQuery: '',
8182

8283
promptOpen: false,
83-
deletedVideoIds: [],
8484
deletedPlaylistItemIds: [],
85-
isUndoToast: false
85+
// Present = shown
86+
undoToastAbortController: null,
8687
}
8788
},
8889
computed: {
@@ -591,44 +592,35 @@ export default defineComponent({
591592
try {
592593
const playlistItems = [].concat(this.playlistItems)
593594
const tempPlaylistItems = [].concat(this.playlistItems)
594-
let isUndoClicked = false
595595

596596
const videoIndex = this.playlistItems.findIndex((video) => {
597597
return video.videoId === videoId && video.playlistItemId === playlistItemId
598598
})
599599

600600
if (videoIndex !== -1) {
601-
this.deletedVideoIds.push(this.playlistItems[videoIndex].videoId)
602601
this.deletedPlaylistItemIds.push(this.playlistItems[videoIndex].playlistItemId)
603602
playlistItems.splice(videoIndex, 1)
604603
this.playlistItems = playlistItems
604+
this.videoCount = playlistItems.length
605605

606606
// Only show toast when no existing toast shown
607-
if (!this.isUndoToast) {
608-
this.isUndoToast = true
607+
if (this.undoToastAbortController == null) {
608+
this.undoToastAbortController = new AbortController()
609+
const actualRemoveVideosTimeout = setTimeout(() => {
610+
this.removeToBeDeletedVideosSometimes()
611+
}, 5000)
609612
showToast(
610613
this.$t('User Playlists.SinglePlaylistView.Toast["Video has been removed. Click here to undo."]'),
611614
5000,
612615
() => {
613616
this.playlistItems = tempPlaylistItems
614-
isUndoClicked = true
615-
this.isUndoToast = false
616-
this.deletedVideoIds = []
617+
this.videoCount = tempPlaylistItems.length
618+
clearTimeout(actualRemoveVideosTimeout)
617619
this.deletedPlaylistItemIds = []
618-
}
620+
this.undoToastAbortController = null
621+
},
622+
this.undoToastAbortController.signal,
619623
)
620-
setTimeout(() => {
621-
if (!isUndoClicked) {
622-
this.removeVideos({
623-
_id: this.playlistId,
624-
videoIds: this.deletedVideoIds,
625-
playlistItemIds: this.deletedPlaylistItemIds,
626-
})
627-
this.deletedVideoIds = []
628-
this.deletedPlaylistItemIds = []
629-
this.isUndoToast = false
630-
}
631-
}, 5000)
632624
}
633625
}
634626
} catch (e) {
@@ -637,6 +629,20 @@ export default defineComponent({
637629
}
638630
},
639631

632+
removeToBeDeletedVideosSometimes() {
633+
if (this.isLoading) { return }
634+
635+
if (this.deletedPlaylistItemIds.length > 0) {
636+
this.removeVideos({
637+
_id: this.playlistId,
638+
playlistItemIds: this.deletedPlaylistItemIds,
639+
})
640+
this.deletedPlaylistItemIds = []
641+
this.undoToastAbortController?.abort()
642+
this.undoToastAbortController = null
643+
}
644+
},
645+
640646
updatePageTitle() {
641647
const playlistTitle = this.playlistTitle
642648
const channelName = this.channelName

0 commit comments

Comments
 (0)