@@ -143,43 +143,99 @@ export function RecordingsView() {
143143 // Show loading state
144144 setRecordings ( [ ] ) ;
145145
146+ // Create a pagination object with the specified page
147+ const paginationWithPage = {
148+ ...pagination ,
149+ currentPage : page
150+ } ;
151+
146152 // Update URL with filters if requested
147153 if ( updateUrl ) {
148- urlUtils . updateUrlWithFilters ( filters , pagination , sortField , sortDirection ) ;
154+ urlUtils . updateUrlWithFilters ( filters , paginationWithPage , sortField , sortDirection ) ;
149155 }
150156
151157 // Load recordings from API
152- const data = await recordingsAPI . loadRecordings ( filters , pagination , sortField , sortDirection ) ;
158+ const data = await recordingsAPI . loadRecordings ( filters , paginationWithPage , sortField , sortDirection ) ;
153159
154160 // Store recordings in the component state
155161 setRecordings ( data . recordings || [ ] ) ;
156162
157163 // Update pagination
158- updatePaginationFromResponse ( data ) ;
164+ updatePaginationFromResponse ( data , page ) ;
159165 } catch ( error ) {
160166 console . error ( 'Error loading recordings:' , error ) ;
161167 showStatusMessage ( 'Error loading recordings: ' + error . message ) ;
162168 }
163169 } ;
164170
165171 // Update pagination from API response
166- const updatePaginationFromResponse = ( data ) => {
172+ const updatePaginationFromResponse = ( data , currentPage ) => {
173+ // Use the provided page parameter instead of the state
174+ currentPage = currentPage || pagination . currentPage ;
175+
167176 if ( data . pagination ) {
177+ const pageSize = data . pagination . limit || 20 ;
178+ const totalItems = data . pagination . total || 0 ;
179+ const totalPages = data . pagination . pages || 1 ;
180+
181+ // Calculate start and end items based on current page
182+ let startItem = 0 ;
183+ let endItem = 0 ;
184+
185+ if ( data . recordings . length > 0 ) {
186+ startItem = ( currentPage - 1 ) * pageSize + 1 ;
187+ endItem = Math . min ( startItem + data . recordings . length - 1 , totalItems ) ;
188+ }
189+
190+ console . log ( 'Pagination update:' , {
191+ currentPage,
192+ pageSize,
193+ totalItems,
194+ totalPages,
195+ startItem,
196+ endItem,
197+ recordingsLength : data . recordings . length
198+ } ) ;
199+
168200 setPagination ( prev => ( {
169201 ...prev ,
170- totalItems : data . pagination . total || 0 ,
171- totalPages : data . pagination . pages || 1 ,
172- pageSize : data . pagination . limit || 20 ,
173- startItem : data . recordings . length > 0 ? ( pagination . currentPage - 1 ) * data . pagination . limit + 1 : 0 ,
174- endItem : Math . min ( ( pagination . currentPage - 1 ) * data . pagination . limit + data . recordings . length , data . pagination . total )
202+ totalItems,
203+ totalPages,
204+ pageSize,
205+ startItem,
206+ endItem
175207 } ) ) ;
176208 } else {
209+ // Fallback if pagination object is not provided
210+ const pageSize = pagination . pageSize ;
211+ const totalItems = data . total || 0 ;
212+ const totalPages = Math . ceil ( totalItems / pageSize ) || 1 ;
213+
214+ // Calculate start and end items based on current page
215+ let startItem = 0 ;
216+ let endItem = 0 ;
217+
218+ if ( data . recordings . length > 0 ) {
219+ startItem = ( currentPage - 1 ) * pageSize + 1 ;
220+ endItem = Math . min ( startItem + data . recordings . length - 1 , totalItems ) ;
221+ }
222+
223+ console . log ( 'Pagination update (fallback):' , {
224+ currentPage,
225+ pageSize,
226+ totalItems,
227+ totalPages,
228+ startItem,
229+ endItem,
230+ recordingsLength : data . recordings . length
231+ } ) ;
232+
177233 setPagination ( prev => ( {
178234 ...prev ,
179- totalItems : data . total || 0 ,
180- totalPages : Math . ceil ( data . total / prev . pageSize ) || 1 ,
181- startItem : data . recordings . length > 0 ? ( pagination . currentPage - 1 ) * prev . pageSize + 1 : 0 ,
182- endItem : Math . min ( ( pagination . currentPage - 1 ) * prev . pageSize + data . recordings . length , data . total )
235+ totalItems,
236+ totalPages,
237+ startItem,
238+ endItem
183239 } ) ) ;
184240 }
185241 } ;
@@ -229,18 +285,33 @@ export function RecordingsView() {
229285
230286 // Reset filters
231287 const resetFilters = ( ) => {
232- // Reset filter state
233- setFilters ( {
288+ // Create default filters
289+ const defaultFilters = {
234290 dateRange : 'last7days' ,
235291 startDate : '' ,
236292 startTime : '00:00' ,
237293 endDate : '' ,
238294 endTime : '23:59' ,
239295 streamId : 'all' ,
240296 recordingType : 'all'
241- } ) ;
297+ } ;
242298
243- setDefaultDateRange ( ) ;
299+ // Get default date range
300+ const now = new Date ( ) ;
301+ const sevenDaysAgo = new Date ( now ) ;
302+ sevenDaysAgo . setDate ( now . getDate ( ) - 7 ) ;
303+
304+ defaultFilters . endDate = now . toISOString ( ) . split ( 'T' ) [ 0 ] ;
305+ defaultFilters . startDate = sevenDaysAgo . toISOString ( ) . split ( 'T' ) [ 0 ] ;
306+
307+ // Reset filter state
308+ setFilters ( defaultFilters ) ;
309+
310+ // Create default pagination
311+ const defaultPagination = {
312+ ...pagination ,
313+ currentPage : 1
314+ } ;
244315
245316 // Reset pagination to first page
246317 setPagination ( prev => ( {
@@ -253,7 +324,19 @@ export function RecordingsView() {
253324 window . history . pushState ( { path : baseUrl } , '' , baseUrl ) ;
254325
255326 // Load recordings with default settings
256- loadRecordings ( 1 , false ) ;
327+ // Use a direct API call to avoid state update delays
328+ recordingsAPI . loadRecordings ( defaultFilters , defaultPagination , 'start_time' , 'desc' )
329+ . then ( data => {
330+ // Store recordings in the component state
331+ setRecordings ( data . recordings || [ ] ) ;
332+
333+ // Update pagination
334+ updatePaginationFromResponse ( data , 1 ) ;
335+ } )
336+ . catch ( error => {
337+ console . error ( 'Error loading recordings:' , error ) ;
338+ showStatusMessage ( 'Error loading recordings: ' + error . message ) ;
339+ } ) ;
257340 } ;
258341
259342 // Remove filter
@@ -300,24 +383,24 @@ export function RecordingsView() {
300383 const goToPage = ( page ) => {
301384 if ( page < 1 || page > pagination . totalPages ) return ;
302385
386+ // Set the current page in pagination state
303387 setPagination ( prev => ( {
304388 ...prev ,
305389 currentPage : page
306390 } ) ) ;
307391
308- // Create URL parameters object with current filters
309- const params = new URLSearchParams ( window . location . search ) ;
310-
311- // Update only the page parameter
312- params . set ( 'page' , page . toString ( ) ) ;
392+ // Create a new pagination object with the updated page
393+ const updatedPagination = {
394+ ... pagination ,
395+ currentPage : page
396+ } ;
313397
314- // Update URL without reloading the page
315- const newUrl = `${ window . location . pathname } ?${ params . toString ( ) } ` ;
316- window . history . pushState ( { path : newUrl } , '' , newUrl ) ;
398+ // Update URL with all filters and the new page
399+ urlUtils . updateUrlWithFilters ( filters , updatedPagination , sortField , sortDirection ) ;
317400
318401 // Use setTimeout to ensure the pagination state is updated before loading recordings
319402 setTimeout ( ( ) => {
320- loadRecordings ( ) ;
403+ loadRecordings ( page , false ) ; // Don't update URL again in loadRecordings
321404 } , 0 ) ;
322405 } ;
323406
@@ -405,42 +488,34 @@ export function RecordingsView() {
405488 // Set the sort parameters directly
406489 setSortField ( sortField ) ;
407490 setSortDirection ( sortDirection ) ;
491+
492+ // Update pagination with the preserved page
408493 setPagination ( prev => ( {
409494 ...prev ,
410495 currentPage : page
411496 } ) ) ;
412497
413498 // Wait for state to update
414499 setTimeout ( ( ) => {
415- // Build query parameters
416- const params = new URLSearchParams ( window . location . search ) ;
417-
418- // Update pagination, sort, and order parameters
419- params . set ( 'page' , page . toString ( ) ) ;
420- params . set ( 'limit' , pagination . pageSize . toString ( ) ) ;
421- params . set ( 'sort' , sortField ) ;
422- params . set ( 'order' , sortDirection ) ;
500+ // Create a new pagination object with the updated page
501+ const updatedPagination = {
502+ ...pagination ,
503+ currentPage : page
504+ } ;
423505
424- // Update URL with preserved parameters
425- const newUrl = `${ window . location . pathname } ?${ params . toString ( ) } ` ;
426- window . history . pushState ( { path : newUrl } , '' , newUrl ) ;
506+ // Update URL with all filters and the preserved parameters
507+ urlUtils . updateUrlWithFilters ( filters , updatedPagination , sortField , sortDirection ) ;
427508
428- // Fetch recordings with preserved parameters
429- fetch ( `/api/recordings?${ params . toString ( ) } ` )
430- . then ( response => {
431- if ( ! response . ok ) {
432- throw new Error ( 'Failed to load recordings' ) ;
433- }
434- return response . json ( ) ;
435- } )
509+ // Load recordings from API
510+ recordingsAPI . loadRecordings ( filters , updatedPagination , sortField , sortDirection )
436511 . then ( data => {
437512 console . log ( 'Recordings data received:' , data ) ;
438513
439514 // Store recordings in the component state
440515 setRecordings ( data . recordings || [ ] ) ;
441516
442517 // Update pagination without changing the current page
443- updatePaginationFromResponse ( data ) ;
518+ updatePaginationFromResponse ( data , page ) ;
444519 } )
445520 . catch ( error => {
446521 console . error ( 'Error loading recordings:' , error ) ;
0 commit comments