@@ -20,6 +20,21 @@ notificationChannel.onmessage = (event) => {
2020 }
2121} ;
2222
23+ // NEW: Listen for direct messages from the service worker
24+ navigator . serviceWorker . addEventListener ( 'message' , event => {
25+ if ( event . data && event . data . type === 'navigate-to-program' ) {
26+ console . log ( '[NOTIF] Received navigate-to-program message from SW.' ) ;
27+ const { channelId, programStart, programId } = event . data . data ;
28+ if ( channelId && programStart ) {
29+ // A short delay can help ensure the UI is ready, especially if the app was just opened.
30+ setTimeout ( ( ) => {
31+ navigateToProgramInGuide ( channelId , programStart , programId ) ;
32+ } , 500 ) ;
33+ }
34+ }
35+ } ) ;
36+
37+
2338function urlBase64ToUint8Array ( base64String ) {
2439 const padding = '=' . repeat ( ( 4 - base64String . length % 4 ) % 4 ) ;
2540 const base64 = ( base64String + padding ) . replace ( / - / g, '+' ) . replace ( / _ / g, '/' ) ;
@@ -371,18 +386,23 @@ const setupNotificationListEventListeners = () => {
371386
372387export const navigateToProgramInGuide = async ( channelId , programStart , programId ) => {
373388 console . log ( `[NOTIF_NAV] Navigating to program. Channel: ${ channelId } , Start: ${ programStart } ` ) ;
374- const stableChannelIdSuffix = channelId . includes ( '_' ) ? '_' + channelId . split ( '_' ) . pop ( ) : channelId ;
389+
390+ // Fallback for potentially incomplete channelId from older notifications
391+ const stableChannelIdSuffix = channelId . includes ( '_' ) ? channelId . split ( '_' ) . pop ( ) : channelId ;
375392
376393 navigate ( '/tvguide' ) ;
377- await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
394+
395+ // Give the UI a moment to switch tabs and render the guide structure
396+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
378397
379398 const targetProgramStart = new Date ( programStart ) ;
380399 const currentGuideDate = new Date ( guideState . currentDate ) ;
381400 currentGuideDate . setHours ( 0 , 0 , 0 , 0 ) ;
382401
402+ // If the program is not on the currently displayed date, switch the date and reload the guide
383403 if ( targetProgramStart . toDateString ( ) !== currentGuideDate . toDateString ( ) ) {
384404 guideState . currentDate = targetProgramStart ;
385- await handleSearchAndFilter ( true ) ;
405+ await handleSearchAndFilter ( true ) ; // `true` forces a full reload and scroll reset
386406 }
387407
388408 const channelScrolledAndRendered = await scrollToChannel ( stableChannelIdSuffix ) ;
@@ -392,13 +412,15 @@ export const navigateToProgramInGuide = async (channelId, programStart, programI
392412 return ;
393413 }
394414
415+ // Now that the channel is visible, find its full dynamic ID
395416 const currentChannelElement = UIElements . guideGrid . querySelector ( `.channel-info[data-id$="${ stableChannelIdSuffix } "]` ) ;
396417 if ( ! currentChannelElement ) {
397- showNotification ( "An unexpected error occurred while locating the channel." , true ) ;
418+ showNotification ( "An unexpected error occurred while locating the channel element ." , true ) ;
398419 return ;
399420 }
400421 const currentDynamicChannelId = currentChannelElement . dataset . id ;
401422
423+ // Finally, find the specific program element
402424 const programElement = UIElements . guideGrid . querySelector (
403425 `.programme-item[data-prog-start="${ programStart } "][data-channel-id="${ currentDynamicChannelId } "]`
404426 ) ;
@@ -412,6 +434,7 @@ export const navigateToProgramInGuide = async (channelId, programStart, programI
412434 const programRect = programElement . getBoundingClientRect ( ) ;
413435 const containerRect = guideContainer . getBoundingClientRect ( ) ;
414436
437+ // Calculate the desired scroll position to center the program both vertically and horizontally
415438 const desiredScrollTop = guideContainer . scrollTop + programRect . top - containerRect . top - ( containerRect . height / 2 ) + ( programRect . height / 2 ) ;
416439 const desiredScrollLeft = guideContainer . scrollLeft + programRect . left - containerRect . left - ( containerRect . width / 2 ) + ( programRect . width / 2 ) ;
417440
@@ -421,9 +444,10 @@ export const navigateToProgramInGuide = async (channelId, programStart, programI
421444 behavior : 'smooth'
422445 } ) ;
423446
447+ // Use a timeout to ensure scrolling is complete before opening details and highlighting
424448 setTimeout ( ( ) => {
425449 openProgramDetails ( programElement ) ;
426450 programElement . classList . add ( 'highlighted-search' ) ;
427451 setTimeout ( ( ) => { programElement . classList . remove ( 'highlighted-search' ) ; } , 2500 ) ;
428- } , 300 ) ;
452+ } , 350 ) ;
429453} ;
0 commit comments