Allow pasting Playlist URLs in search to navigate straight to playlist page

This commit is contained in:
Preston 2021-01-15 11:34:44 -05:00
parent d5304cfcdd
commit 890d5b4a09
2 changed files with 46 additions and 18 deletions

View File

@ -77,7 +77,7 @@ export default Vue.extend({
this.debounceSearchResults = debounce(this.getSearchSuggestions, 200) this.debounceSearchResults = debounce(this.getSearchSuggestions, 200)
}, },
methods: { methods: {
goToSearch: function (query) { goToSearch: async function (query) {
const appWidth = $(window).width() const appWidth = $(window).width()
if (appWidth <= 680) { if (appWidth <= 680) {
@ -89,10 +89,18 @@ export default Vue.extend({
searchInput.blur() searchInput.blur()
} }
this.$store.dispatch('getVideoIdFromUrl', query).then((result) => { const videoId = await this.$store.dispatch('getVideoIdFromUrl', query)
if (result) { const playlistId = await this.$store.dispatch('getPlaylistIdFromUrl', query)
console.log(playlistId)
if (videoId) {
this.$router.push({ this.$router.push({
path: `/watch/${result}` path: `/watch/${videoId}`
})
} else if (playlistId) {
this.$router.push({
path: `/playlist/${playlistId}`
}) })
} else { } else {
router.push({ router.push({
@ -105,7 +113,6 @@ export default Vue.extend({
} }
}) })
} }
})
this.showFilters = false this.showFilters = false
}, },

View File

@ -229,6 +229,27 @@ const actions = {
return extractors.reduce((a, c) => a || c(), null) || false return extractors.reduce((a, c) => a || c(), null) || false
}, },
getPlaylistIdFromUrl (_, url) {
/** @type {URL} */
let urlObject
try {
urlObject = new URL(url)
} catch (e) {
return false
}
const extractors = [
// anything with /playlist?list=
function() {
if (urlObject.pathname === '/playlist' && urlObject.searchParams.has('list')) {
return urlObject.searchParams.get('list')
}
}
]
return extractors.reduce((a, c) => a || c(), null) || false
},
padNumberWithLeadingZeros(_, payload) { padNumberWithLeadingZeros(_, payload) {
let numberString = payload.number.toString() let numberString = payload.number.toString()
while (numberString.length < payload.length) { while (numberString.length < payload.length) {