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

125 lines
2.7 KiB
JavaScript
Raw Normal View History

import { profilesDb } from '../datastores'
2020-08-23 21:07:29 +02:00
const state = {
profileList: [{
_id: 'allChannels',
2021-03-26 19:30:44 +01:00
name: 'All Channels',
bgColor: '#000000',
textColor: '#FFFFFF',
subscriptions: []
}],
activeProfile: 0
2020-08-23 21:07:29 +02:00
}
const getters = {
getProfileList: () => {
2020-08-24 04:56:33 +02:00
return state.profileList
},
getActiveProfile: () => {
return state.activeProfile
2020-08-23 21:07:29 +02:00
}
}
const actions = {
async grabAllProfiles({ rootState, dispatch, commit }, defaultName = null) {
let profiles = await profilesDb.find({})
if (profiles.length === 0) {
dispatch('createDefaultProfile', defaultName)
return
}
// We want the primary profile to always be first
// So sort with that then sort alphabetically by profile name
profiles = profiles.sort((a, b) => {
if (a._id === 'allChannels') {
return -1
}
if (b._id === 'allChannels') {
return 1
}
return b.name - a.name
2020-08-23 21:07:29 +02:00
})
if (state.profileList.length < profiles.length) {
const profileIndex = profiles.findIndex((profile) => {
return profile._id === rootState.settings.defaultProfile
2020-08-24 04:56:33 +02:00
})
if (profileIndex !== -1) {
commit('setActiveProfile', profileIndex)
}
}
commit('setProfileList', profiles)
},
async grabProfileInfo(_, profileId) {
console.log(profileId)
return await profilesDb.findOne({ _id: profileId })
2020-08-24 04:56:33 +02:00
},
async createDefaultProfile({ dispatch }, defaultName) {
2020-08-23 21:07:29 +02:00
const randomColor = await dispatch('getRandomColor')
const textColor = await dispatch('calculateColorLuminance', randomColor)
const defaultProfile = {
_id: 'allChannels',
name: defaultName,
bgColor: randomColor,
textColor: textColor,
subscriptions: []
}
2021-03-27 19:02:53 +01:00
await profilesDb.update(
{ _id: 'allChannels' },
defaultProfile,
{ upsert: true }
)
dispatch('grabAllProfiles')
2020-08-23 21:07:29 +02:00
},
async updateProfile({ dispatch }, profile) {
await profilesDb.update(
{ _id: profile._id },
profile,
{ upsert: true }
)
dispatch('grabAllProfiles')
2020-08-23 21:07:29 +02:00
},
async insertProfile({ dispatch }, profile) {
await profilesDb.insert(profile)
dispatch('grabAllProfiles')
2020-08-23 21:07:29 +02:00
},
async removeProfile({ dispatch }, profileId) {
await profilesDb.remove({ _id: profileId })
dispatch('grabAllProfiles')
},
compactProfiles(_) {
profilesDb.persistence.compactDatafile()
},
updateActiveProfile({ commit }, index) {
commit('setActiveProfile', index)
2020-08-23 21:07:29 +02:00
}
}
const mutations = {
setProfileList(state, profileList) {
2020-08-24 04:56:33 +02:00
state.profileList = profileList
},
setActiveProfile(state, activeProfile) {
state.activeProfile = activeProfile
2020-08-23 21:07:29 +02:00
}
}
export default {
state,
getters,
actions,
mutations
}