@@ -19,6 +19,12 @@ const Overview = () => {
19
19
const [ selectedLibrary , setSelectedLibrary ] = useState < number | undefined > (
20
20
undefined ,
21
21
)
22
+ const backendSortableFields = [
23
+ 'addedAt' ,
24
+ 'originallyAvailableAt' ,
25
+ 'viewCount' ,
26
+ 'lastViewedAt' ,
27
+ ]
22
28
23
29
useEffect ( ( ) => {
24
30
const stored = sessionStorage . getItem ( 'maintainerr_selectedLibrary' )
@@ -30,7 +36,7 @@ const Overview = () => {
30
36
const [ searchUsed , setSearchUsed ] = useState < boolean > ( false )
31
37
const SearchCtx = useContext ( SearchContext )
32
38
const LibrariesCtx = useContext ( LibrariesContext )
33
-
39
+ const [ libraryCount , setLibraryCount ] = useState < number > ( 1000 )
34
40
const [ sortOption , setSortOption ] = useState < SortOption > ( 'title:asc' )
35
41
const [ filterOption , setFilterOption ] = useState < FilterOption > ( 'all' )
36
42
const [ viewMode , setViewMode ] = useState < ViewMode > ( ( ) => {
@@ -84,27 +90,22 @@ const Overview = () => {
84
90
} , [ LibrariesCtx . libraries ] )
85
91
86
92
useEffect ( ( ) => {
87
- if ( SearchCtx . search . text !== '' ) {
88
- GetApiHandler ( `/plex/search/${ SearchCtx . search . text } ` ) . then (
89
- ( resp : IPlexMetadata [ ] ) => {
90
- setSearchUsed ( true )
91
- setData ( resp ? resp : [ ] )
92
- loadingRef . current = false
93
- } ,
94
- )
95
- } else {
93
+ if ( SearchCtx . search . text === '' ) {
96
94
setSearchUsed ( false )
97
95
setData ( [ ] )
98
96
loadingRef . current = true
99
- fetchData ( )
97
+
98
+ if ( selectedLibrary !== undefined ) {
99
+ switchLib ( selectedLibrary )
100
+ }
100
101
}
101
102
} , [ SearchCtx . search . text ] )
102
103
103
104
useEffect ( ( ) => {
104
- if ( selectedLibrary !== undefined && SearchCtx . search . text === '' ) {
105
- fetchData ( )
105
+ if ( ! searchUsed && selectedLibrary !== undefined ) {
106
+ switchLib ( selectedLibrary )
106
107
}
107
- } , [ selectedLibrary ] )
108
+ } , [ sortOption , filterOption ] )
108
109
109
110
const sortData = (
110
111
items : IPlexMetadata [ ] ,
@@ -127,57 +128,44 @@ const Overview = () => {
127
128
return items
128
129
}
129
130
130
- useEffect ( ( ) => {
131
- setVisibleCount ( 100 )
132
- fetchData ( )
133
- } , [ sortOption , filterOption ] )
134
-
135
- const switchLib = ( libraryId : number ) => {
131
+ const switchLib = async ( libraryId : number ) => {
136
132
loadingRef . current = true
137
133
setData ( [ ] )
138
134
setSearchUsed ( false )
139
135
setSelectedLibrary ( libraryId )
140
- }
141
136
142
- const backendSortableFields = [
143
- 'addedAt' ,
144
- 'originallyAvailableAt' ,
145
- 'viewCount' ,
146
- 'lastViewedAt' ,
147
- ]
137
+ // Fetch count and exclusions first
138
+ const [ countResp , exclusionResp ] = await Promise . all ( [
139
+ GetApiHandler ( `/plex/library/${ libraryId } /content/count` ) ,
140
+ GetApiHandler ( `/rules/exclusion/all` ) ,
141
+ ] )
148
142
149
- const fetchData = async ( ) => {
150
- if ( selectedLibrary && SearchCtx . search . text === '' ) {
151
- const askedLib = selectedLibrary
152
- const sortField = sortOption . split ( ':' ) [ 0 ]
153
- const isBackendSortable = backendSortableFields . includes ( sortField )
154
- const apiSortParam = isBackendSortable ? `&sort=${ sortOption } ` : ''
155
-
156
- const [ plexResp , exclusionResp ] = await Promise . all ( [
157
- GetApiHandler (
158
- `/plex/library/${ selectedLibrary } /content?page=1&size=1000${ apiSortParam } ` ,
159
- ) ,
160
- GetApiHandler ( `/rules/exclusion/all` ) ,
161
- ] )
162
-
163
- const enrichedItems = metadataEnrichment ( plexResp . items , exclusionResp )
164
-
165
- const sortedItems = sortData ( enrichedItems , sortOption )
166
-
167
- const filteredItems = sortedItems . filter ( ( item ) => {
168
- if ( filterOption === 'excluded' ) return ! ! item . maintainerrExclusionType
169
- if ( filterOption === 'nonExcluded' )
170
- return ! item . maintainerrExclusionType
171
- return true
172
- } )
173
-
174
- if ( askedLib === selectedLibrary ) {
175
- setVisibleCount ( 100 )
176
- setAllItems ( filteredItems )
177
- setData ( filteredItems . slice ( 0 , 100 ) )
178
- loadingRef . current = false
179
- }
180
- }
143
+ const count = countResp ?. count ?? 1000
144
+ setLibraryCount ( count )
145
+ console . log ( `Fetching ${ count } items for library ${ libraryId } ` )
146
+
147
+ // Fetch all metadata
148
+ const sortField = sortOption . split ( ':' ) [ 0 ]
149
+ const isBackendSortable = backendSortableFields . includes ( sortField )
150
+ const apiSortParam = isBackendSortable ? `&sort=${ sortOption } ` : ''
151
+
152
+ const plexResp = await GetApiHandler (
153
+ `/plex/library/${ libraryId } /content?page=1&size=${ count } ${ apiSortParam } ` ,
154
+ )
155
+
156
+ const enrichedItems = metadataEnrichment ( plexResp . items , exclusionResp )
157
+ const sortedItems = sortData ( enrichedItems , sortOption )
158
+
159
+ const filteredItems = sortedItems . filter ( ( item ) => {
160
+ if ( filterOption === 'excluded' ) return ! ! item . maintainerrExclusionType
161
+ if ( filterOption === 'nonExcluded' ) return ! item . maintainerrExclusionType
162
+ return true
163
+ } )
164
+
165
+ setVisibleCount ( 100 )
166
+ setAllItems ( filteredItems )
167
+ setData ( filteredItems . slice ( 0 , 100 ) )
168
+ loadingRef . current = false
181
169
}
182
170
183
171
// Triggers additional data load when near the bottom
0 commit comments