Skip to content

Commit d3f3976

Browse files
committed
changing data fetch to pull only the library count, instead of a hardcoded 1000
1 parent 5557de0 commit d3f3976

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

server/src/modules/api/plex-api/plex-api.controller.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ export class PlexApiController {
4646
sort,
4747
);
4848
}
49+
@Get('library/:id/content/count')
50+
async getLibraryContentCount(
51+
@Param('id') id: string,
52+
@Query('type') type?: EPlexDataType,
53+
) {
54+
const [count, libraries] = await Promise.all([
55+
this.plexApiService.getLibraryContentCount(id, type),
56+
this.plexApiService.getLibraries(),
57+
]);
58+
59+
const library = libraries.find((lib) => String(lib.key) === String(id));
60+
const libraryName = library?.title ?? 'Unknown';
61+
62+
return { count: (count ?? 0) + 20, libraryName };
63+
}
64+
4965
@Get('library/:id/content/search/:query')
5066
searchibraryContent(
5167
@Param('id') id: string,

server/src/modules/api/plex-api/plex-api.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ export class PlexApiService {
226226
'X-Plex-Container-Size': '0',
227227
},
228228
});
229-
230229
return response.MediaContainer.totalSize;
231230
} catch (err) {
232231
this.logger.warn(

ui/src/components/Overview/index.tsx

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const Overview = () => {
1919
const [selectedLibrary, setSelectedLibrary] = useState<number | undefined>(
2020
undefined,
2121
)
22+
const backendSortableFields = [
23+
'addedAt',
24+
'originallyAvailableAt',
25+
'viewCount',
26+
'lastViewedAt',
27+
]
2228

2329
useEffect(() => {
2430
const stored = sessionStorage.getItem('maintainerr_selectedLibrary')
@@ -30,7 +36,7 @@ const Overview = () => {
3036
const [searchUsed, setSearchUsed] = useState<boolean>(false)
3137
const SearchCtx = useContext(SearchContext)
3238
const LibrariesCtx = useContext(LibrariesContext)
33-
39+
const [libraryCount, setLibraryCount] = useState<number>(1000)
3440
const [sortOption, setSortOption] = useState<SortOption>('title:asc')
3541
const [filterOption, setFilterOption] = useState<FilterOption>('all')
3642
const [viewMode, setViewMode] = useState<ViewMode>(() => {
@@ -84,27 +90,22 @@ const Overview = () => {
8490
}, [LibrariesCtx.libraries])
8591

8692
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 === '') {
9694
setSearchUsed(false)
9795
setData([])
9896
loadingRef.current = true
99-
fetchData()
97+
98+
if (selectedLibrary !== undefined) {
99+
switchLib(selectedLibrary)
100+
}
100101
}
101102
}, [SearchCtx.search.text])
102103

103104
useEffect(() => {
104-
if (selectedLibrary !== undefined && SearchCtx.search.text === '') {
105-
fetchData()
105+
if (!searchUsed && selectedLibrary !== undefined) {
106+
switchLib(selectedLibrary)
106107
}
107-
}, [selectedLibrary])
108+
}, [sortOption, filterOption])
108109

109110
const sortData = (
110111
items: IPlexMetadata[],
@@ -127,57 +128,44 @@ const Overview = () => {
127128
return items
128129
}
129130

130-
useEffect(() => {
131-
setVisibleCount(100)
132-
fetchData()
133-
}, [sortOption, filterOption])
134-
135-
const switchLib = (libraryId: number) => {
131+
const switchLib = async (libraryId: number) => {
136132
loadingRef.current = true
137133
setData([])
138134
setSearchUsed(false)
139135
setSelectedLibrary(libraryId)
140-
}
141136

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+
])
148142

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
181169
}
182170

183171
// Triggers additional data load when near the bottom

0 commit comments

Comments
 (0)