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:
Deluan Quintão
2025-11-15 17:42:28 -05:00
committed by GitHub
parent 0161a0958c
commit 395a36e10f
3 changed files with 230 additions and 9 deletions
+15 -1
View File
@@ -12,7 +12,21 @@ const isAdmin = () => {
const getSelectedLibraries = () => {
try {
const state = JSON.parse(localStorage.getItem('state'))
return state?.library?.selectedLibraries || []
const selectedLibraries = state?.library?.selectedLibraries || []
const userLibraries = state?.library?.userLibraries || []
// Validate selected libraries against current user libraries
const userLibraryIds = userLibraries.map((lib) => lib.id)
const validatedSelection = selectedLibraries.filter((id) =>
userLibraryIds.includes(id),
)
// If user has only one library, return empty array (no filter needed)
if (userLibraryIds.length === 1) {
return []
}
return validatedSelection
} catch (err) {
return []
}