395a36e10f
* fix: validate library selection state for single-library users Fixes issues where users with a single library see no content when selectedLibraries in localStorage contains library IDs they no longer have access to (e.g., after removing libraries or switching accounts). Changes: - libraryReducer: Validate selectedLibraries when SET_USER_LIBRARIES is dispatched, filtering out invalid IDs and resetting to empty for single-library users (empty means 'all accessible libraries') - wrapperDataProvider: Add defensive validation in getSelectedLibraries to check against current user libraries before applying filters - Add comprehensive test coverage for reducer validation logic Fixes #4553, #4508, #4569 * style: format code with prettier
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import { SET_SELECTED_LIBRARIES, SET_USER_LIBRARIES } from '../actions'
|
|
|
|
const initialState = {
|
|
userLibraries: [],
|
|
selectedLibraries: [], // Empty means "all accessible libraries"
|
|
}
|
|
|
|
export const libraryReducer = (previousState = initialState, payload) => {
|
|
const { type, data } = payload
|
|
switch (type) {
|
|
case SET_USER_LIBRARIES: {
|
|
const newUserLibraryIds = data.map((lib) => lib.id)
|
|
|
|
// Validate and filter selected libraries to only include IDs that exist in new user libraries
|
|
const validatedSelection = previousState.selectedLibraries.filter((id) =>
|
|
newUserLibraryIds.includes(id),
|
|
)
|
|
|
|
// Determine the final selection:
|
|
// 1. If first time setting libraries (no previous user libraries), select all
|
|
// 2. If user now has only one library, reset to empty (no filter needed)
|
|
// 3. Otherwise, use validated selection (may be empty if all previous selections were invalid)
|
|
let finalSelection
|
|
if (
|
|
previousState.selectedLibraries.length === 0 &&
|
|
previousState.userLibraries.length === 0
|
|
) {
|
|
// First time: select all libraries
|
|
finalSelection = newUserLibraryIds
|
|
} else if (newUserLibraryIds.length === 1) {
|
|
// Single library: reset selection (empty means "all accessible")
|
|
finalSelection = []
|
|
} else {
|
|
// Multiple libraries: use validated selection
|
|
finalSelection = validatedSelection
|
|
}
|
|
|
|
return {
|
|
...previousState,
|
|
userLibraries: data,
|
|
selectedLibraries: finalSelection,
|
|
}
|
|
}
|
|
case SET_SELECTED_LIBRARIES:
|
|
return {
|
|
...previousState,
|
|
selectedLibraries: data,
|
|
}
|
|
default:
|
|
return previousState
|
|
}
|
|
}
|