audiobookshelf/client/store/user.js

177 lines
5.9 KiB
JavaScript
Raw Normal View History

export const state = () => ({
user: null,
settings: {
orderBy: 'media.metadata.title',
orderDesc: false,
filterBy: 'all',
playbackRate: 1,
bookshelfCoverSize: 120,
collapseSeries: false,
collapseBookSeries: false,
2024-07-04 19:35:58 +02:00
showSubtitles: false,
useChapterTrack: false,
seriesSortBy: 'name',
seriesSortDesc: false,
2024-02-04 04:48:35 +01:00
seriesFilterBy: 'all',
authorSortBy: 'name',
WIP: Add adjustable skip amount (#3113) * Add playback settings string to en-us * Add playback settings UI for jump forwards and jump backwards * Remove jump forwards and jump backwards settings * Remove jump forwards and jump backwards en-us strings * Update player UI to include player settings button * Add label view player settings string * Add PlayerSettingsModal component Includes a toggle switch for enabling/disabling the chapter track feature. * Add player settings modal component to MediaPlayerContainer * Handle useChapterTrack changes in PlayerUI * Add jump forwards and jump backwards settings to user store * Add jump forwards and jump backwards label strings * Add jump forwards and jump backwards settings to PlayerSettingsModal * Update jump forwards and jump backwards to handle user state values in PlayerHandler * Update jump backwards icon in PlayerPlaybackControls * Add playback settings string to en-us * Add playback settings UI for jump forwards and jump backwards * Remove jump forwards and jump backwards settings * Remove jump forwards and jump backwards en-us strings * Update player UI to include player settings button * Add label view player settings string * Add PlayerSettingsModal component Includes a toggle switch for enabling/disabling the chapter track feature. * Add player settings modal component to MediaPlayerContainer * Handle useChapterTrack changes in PlayerUI * Add jump forwards and jump backwards settings to user store * Add jump forwards and jump backwards label strings * Add jump forwards and jump backwards settings to PlayerSettingsModal * Update jump forwards and jump backwards to handle user state values in PlayerHandler * Update jump backwards icon in PlayerPlaybackControls * Add jump amounts to playback controls tooltips * Fix merge issues and add new Material Symbols to player ui * Alphabetize strings in en-us.json * Update dropdown component with SelectInput to support menu overflowing modal * Update localization for player settings * Update en-us strings order --------- Co-authored-by: advplyr <advplyr@protonmail.com>
2024-07-13 00:52:48 +02:00
authorSortDesc: false,
jumpForwardAmount: 10,
jumpBackwardAmount: 10,
}
})
export const getters = {
2021-08-27 14:01:47 +02:00
getIsRoot: (state) => state.user && state.user.type === 'root',
getIsAdminOrUp: (state) => state.user && (state.user.type === 'admin' || state.user.type === 'root'),
getToken: (state) => {
2023-05-28 17:47:28 +02:00
return state.user?.token || null
},
getUserMediaProgress: (state) => (libraryItemId, episodeId = null) => {
if (!state.user.mediaProgress) return null
2024-06-03 08:04:03 +02:00
return state.user.mediaProgress.find((li) => {
if (episodeId && li.episodeId !== episodeId) return false
2022-04-09 02:27:35 +02:00
return li.libraryItemId == libraryItemId
})
},
getUserBookmarksForItem: (state) => (libraryItemId) => {
if (!state.user.bookmarks) return []
2024-06-03 08:04:03 +02:00
return state.user.bookmarks.filter((bm) => bm.libraryItemId === libraryItemId)
},
getUserSetting: (state) => (key) => {
2023-06-11 20:22:58 +02:00
return state.settings?.[key] || null
},
getUserCanUpdate: (state) => {
2023-06-11 20:22:58 +02:00
return !!state.user?.permissions?.update
},
getUserCanDelete: (state) => {
2023-06-11 20:22:58 +02:00
return !!state.user?.permissions?.delete
},
getUserCanDownload: (state) => {
2023-06-11 20:22:58 +02:00
return !!state.user?.permissions?.download
},
getUserCanUpload: (state) => {
2023-06-11 20:22:58 +02:00
return !!state.user?.permissions?.upload
},
getUserCanAccessAllLibraries: (state) => {
2023-06-11 20:22:58 +02:00
return !!state.user?.permissions?.accessAllLibraries
},
getLibrariesAccessible: (state, getters) => {
if (!state.user) return []
if (getters.getUserCanAccessAllLibraries) return []
return state.user.librariesAccessible || []
},
getCanAccessLibrary: (state, getters) => (libraryId) => {
if (!state.user) return false
if (getters.getUserCanAccessAllLibraries) return true
return getters.getLibrariesAccessible.includes(libraryId)
},
getIsSeriesRemovedFromContinueListening: (state) => (seriesId) => {
if (!state.user || !state.user.seriesHideFromContinueListening || !state.user.seriesHideFromContinueListening.length) return false
return state.user.seriesHideFromContinueListening.includes(seriesId)
2024-06-03 08:04:03 +02:00
},
getSizeMultiplier: (state) => {
return state.settings.bookshelfCoverSize / 120
}
}
export const actions = {
// When changing libraries make sure sort and filter is still valid
checkUpdateLibrarySortFilter({ state, dispatch, commit }, mediaType) {
const settingsUpdate = {}
if (mediaType == 'podcast') {
if (state.settings.orderBy == 'media.metadata.authorName' || state.settings.orderBy == 'media.metadata.authorNameLF') {
settingsUpdate.orderBy = 'media.metadata.author'
}
if (state.settings.orderBy == 'media.duration') {
settingsUpdate.orderBy = 'media.numTracks'
}
if (state.settings.orderBy == 'media.metadata.publishedYear') {
settingsUpdate.orderBy = 'media.metadata.title'
}
const invalidFilters = ['series', 'authors', 'narrators', 'publishers', 'languages', 'progress', 'issues', 'ebooks', 'abridged']
const filterByFirstPart = (state.settings.filterBy || '').split('.').shift()
if (invalidFilters.includes(filterByFirstPart)) {
settingsUpdate.filterBy = 'all'
}
} else {
if (state.settings.orderBy == 'media.metadata.author') {
settingsUpdate.orderBy = 'media.metadata.authorName'
}
if (state.settings.orderBy == 'media.numTracks') {
settingsUpdate.orderBy = 'media.duration'
}
}
if (Object.keys(settingsUpdate).length) {
dispatch('updateUserSettings', settingsUpdate)
}
},
updateUserSettings({ state, commit }, payload) {
if (!payload) return false
let hasChanges = false
const existingSettings = { ...state.settings }
for (const key in existingSettings) {
if (payload[key] !== undefined && existingSettings[key] !== payload[key]) {
hasChanges = true
existingSettings[key] = payload[key]
}
}
if (hasChanges) {
commit('setSettings', existingSettings)
this.$eventBus.$emit('user-settings', state.settings)
}
},
loadUserSettings({ state, commit }) {
// Load settings from local storage
try {
let userSettingsFromLocal = localStorage.getItem('userSettings')
if (userSettingsFromLocal) {
userSettingsFromLocal = JSON.parse(userSettingsFromLocal)
const userSettings = { ...state.settings }
for (const key in userSettings) {
if (userSettingsFromLocal[key] !== undefined) {
userSettings[key] = userSettingsFromLocal[key]
}
}
commit('setSettings', userSettings)
this.$eventBus.$emit('user-settings', state.settings)
}
} catch (error) {
console.error('Failed to load userSettings from local storage', error)
}
}
}
export const mutations = {
setUser(state, user) {
state.user = user
if (user) {
if (user.token) localStorage.setItem('token', user.token)
} else {
localStorage.removeItem('token')
}
},
2022-07-19 00:19:16 +02:00
setUserToken(state, token) {
state.user.token = token
localStorage.setItem('token', user.token)
},
updateMediaProgress(state, { id, data }) {
if (!state.user) return
if (!data) {
2024-06-03 08:04:03 +02:00
state.user.mediaProgress = state.user.mediaProgress.filter((lip) => lip.id != id)
} else {
2024-06-03 08:04:03 +02:00
var indexOf = state.user.mediaProgress.findIndex((lip) => lip.id == id)
if (indexOf >= 0) {
state.user.mediaProgress.splice(indexOf, 1, data)
} else {
state.user.mediaProgress.push(data)
}
}
},
setSettings(state, settings) {
if (!settings) return
localStorage.setItem('userSettings', JSON.stringify(settings))
state.settings = settings
}
2024-06-03 08:04:03 +02:00
}