Skip to content

Commit

Permalink
feat: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: PikachuEXE <git@pikachuexe.net>
  • Loading branch information
Hoverth and PikachuEXE committed Sep 30, 2024
1 parent aebeb44 commit 0613bbf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/renderer/helpers/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ export function getSortedPlaylistItems(playlistItems, sortOrder, locale, reverse
})
}

export function checkDurationFromVideo(a) {
return (isNaN(a.lengthSeconds) || a.lengthSeconds === 0 || typeof a.lengthSeconds !== 'number') ? 0 : a.lengthSeconds
export function videoDurationPresent(v) {
if (typeof v.lengthSeconds !== 'number') { return false }

Check failure on line 40 in src/renderer/helpers/playlists.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
return !(isNaN(v.lengthSeconds) || v.lengthSeconds === 0)
}

function videoDurationWithFallback(v) {
if (videoDurationPresent(v)) { return v.lengthSeconds }

// Fallback
return 0
}

function compareTwoPlaylistItems(a, b, sortOrder, collator) {
Expand All @@ -54,10 +63,10 @@ function compareTwoPlaylistItems(a, b, sortOrder, collator) {
case SORT_BY_VALUES.AuthorDescending:
return collator.compare(b.author, a.author)
case SORT_BY_VALUES.VideoDurationAscending: {
return checkDurationFromVideo(a) - checkDurationFromVideo(b)
return videoDurationWithFallback(a) - videoDurationWithFallback(b)
}
case SORT_BY_VALUES.VideoDurationDescending: {
return checkDurationFromVideo(b) - checkDurationFromVideo(a)
return videoDurationWithFallback(b) - videoDurationWithFallback(a)
}
default:
console.error(`Unknown sortOrder: ${sortOrder}`)
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/views/Playlist/Playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
deepCopy,
} from '../../helpers/utils'
import { invidiousGetPlaylistInfo, youtubeImageUrlToInvidious } from '../../helpers/api/invidious'
import { getSortedPlaylistItems, checkDurationFromVideo, SORT_BY_VALUES } from '../../helpers/playlists'
import { getSortedPlaylistItems, videoDurationPresent, SORT_BY_VALUES } from '../../helpers/playlists'
import packageDetails from '../../../../package.json'
import { MOBILE_WIDTH_THRESHOLD, PLAYLIST_HEIGHT_FORCE_LIST_THRESHOLD } from '../../../constants'

Expand Down Expand Up @@ -437,7 +437,7 @@ export default defineComponent({
getDurationPlaylistItems: function () {
const modifiedPlaylistItems = deepCopy(this.playlistItems)
modifiedPlaylistItems.forEach(video => {
if (!checkDurationFromVideo(video)) {
if (!videoDurationPresent(video)) {
const videoHistory = this.$store.getters.getHistoryCacheById[video.videoId]
if (typeof videoHistory !== 'undefined') video.lengthSeconds = videoHistory.lengthSeconds
}
Expand All @@ -447,7 +447,7 @@ export default defineComponent({

showNoticesSometimes: function (playlistItems) {
if (this.alreadyShownNotice) return
const anyVideoMissingDuration = playlistItems.some(v => !checkDurationFromVideo(v))
const anyVideoMissingDuration = playlistItems.some(v => !videoDurationPresent(v))
if (anyVideoMissingDuration) {
showToast(this.$t('User Playlists.SinglePlaylistView.Toast.This playlist has a video with a duration error'), 5000)
this.alreadyShownNotice = true
Expand Down

0 comments on commit 0613bbf

Please sign in to comment.