FreeTube/src/renderer/store/modules/utils.js

139 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-02-16 19:30:00 +01:00
import IsEqual from 'lodash.isequal'
2020-07-04 17:44:35 +02:00
import FtToastEvents from '../../components/ft-toast/ft-toast-events'
2020-02-16 19:30:00 +01:00
const state = {
isSideNavOpen: false,
sessionSearchHistory: [],
searchSettings: {
sortBy: 'relevance',
time: '',
type: 'all',
duration: ''
},
colorClasses: [
'mainRed',
'mainPink',
'mainPurple',
'mainDeepPurple',
'mainIndigo',
'mainBlue',
'mainLightBlue',
'mainCyan',
'mainTeal',
'mainGreen',
'mainLightGreen',
'mainLime',
'mainYellow',
'mainAmber',
'mainOrange',
'mainDeepOrange'
]
2020-02-16 19:30:00 +01:00
}
const getters = {
getIsSideNavOpen () {
return state.isSideNavOpen
},
getCurrentVolume () {
return state.currentVolume
},
2020-02-16 19:30:00 +01:00
getSessionSearchHistory () {
return state.sessionSearchHistory
},
getSearchSettings () {
return state.searchSettings
}
}
const actions = {
getRandomColorClass () {
const randomInt = Math.floor(Math.random() * state.colorClasses.length)
return state.colorClasses[randomInt]
},
2020-06-20 14:52:32 +02:00
getVideoIdFromUrl (_, url) {
/** @type {URL} */
let urlObject
try {
urlObject = new URL(url)
} catch (e) {
return false
}
2020-06-20 14:52:32 +02:00
const extractors = [
// anything with /watch?v=
function() {
2020-06-20 15:05:36 +02:00
if (urlObject.pathname === '/watch' && urlObject.searchParams.has('v')) {
return urlObject.searchParams.get('v')
2020-06-20 14:52:32 +02:00
}
},
// youtu.be
function() {
2020-06-20 15:05:36 +02:00
if (urlObject.host === 'youtu.be' && urlObject.pathname.match(/^\/[A-Za-z0-9_-]+$/)) {
2020-06-20 14:52:32 +02:00
return urlObject.pathname.slice(1)
}
},
// cloudtube
function() {
if (urlObject.host.match(/^cadence\.(gq|moe)$/) && urlObject.pathname.match(/^\/cloudtube\/video\/[A-Za-z0-9_-]+$/)) {
2020-06-20 15:05:36 +02:00
return urlObject.pathname.slice('/cloudtube/video/'.length)
2020-06-20 14:52:32 +02:00
}
}
]
return extractors.reduce((a, c) => a || c(), null) || false
2020-07-04 17:44:35 +02:00
},
showToast (_, message, action, time) {
FtToastEvents.$emit('toast.open', message, action, time)
}
}
2020-02-16 19:30:00 +01:00
const mutations = {
toggleSideNav (state) {
state.isSideNavOpen = !state.isSideNavOpen
},
setSessionSearchHistory (state, history) {
state.sessionSearchHistory = history
},
addToSessionSearchHistory (state, payload) {
const sameSearch = state.sessionSearchHistory.findIndex((search) => {
return search.query === payload.query && IsEqual(payload.searchSettings, search.searchSettings)
})
if (sameSearch !== -1) {
state.sessionSearchHistory[sameSearch].data = state.sessionSearchHistory[sameSearch].data.concat(payload.data)
state.sessionSearchHistory[sameSearch].nextPageRef = payload.nextPageRef
} else {
state.sessionSearchHistory.push(payload)
}
},
setSearchSortBy (state, value) {
state.searchSettings.sortBy = value
},
setSearchTime (state, value) {
state.searchSettings.time = value
},
setSearchType (state, value) {
state.searchSettings.type = value
},
setSearchDuration (state, value) {
state.searchSettings.duration = value
}
}
export default {
state,
getters,
actions,
mutations
}