fix(ui): fix library selection state for single-library users (#4686)
* 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
This commit is contained in:
@@ -8,18 +8,39 @@ const initialState = {
|
||||
export const libraryReducer = (previousState = initialState, payload) => {
|
||||
const { type, data } = payload
|
||||
switch (type) {
|
||||
case SET_USER_LIBRARIES:
|
||||
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,
|
||||
// If this is the first time setting user libraries and no selection exists,
|
||||
// default to all libraries
|
||||
selectedLibraries:
|
||||
previousState.selectedLibraries.length === 0 &&
|
||||
previousState.userLibraries.length === 0
|
||||
? data.map((lib) => lib.id)
|
||||
: previousState.selectedLibraries,
|
||||
selectedLibraries: finalSelection,
|
||||
}
|
||||
}
|
||||
case SET_SELECTED_LIBRARIES:
|
||||
return {
|
||||
...previousState,
|
||||
|
||||
Reference in New Issue
Block a user