@@ -353,48 +353,98 @@ window.giveKudos = async function() {
353353// Load post navigation (prev/next in same type)
354354async function loadPostNavigation ( postId ) {
355355 try {
356- const res = await fetch ( `${ API_BASE } /post/${ postId } /navigation` ) ;
357- const data = await res . json ( ) ;
356+ // Try the navigation API first
357+ let data = null ;
358+ try {
359+ const res = await fetch ( `${ API_BASE } /post/${ postId } /navigation` ) ;
360+ if ( res . ok ) {
361+ data = await res . json ( ) ;
362+ console . log ( 'Navigation API data:' , data ) ;
363+ }
364+ } catch ( apiErr ) {
365+ console . log ( 'Navigation API not available, using fallback' ) ;
366+ }
367+
368+ // If API didn't work or returned error, use fallback
369+ if ( ! data || data . error ) {
370+ console . log ( 'Using fallback navigation method' ) ;
371+ // Get current post type
372+ const postType = window . currentPostType || 'blog' ;
373+
374+ // Fetch all posts of this type
375+ const postsRes = await fetch ( `${ API_BASE } /post?type=${ postType } ` ) ;
376+ const postsData = await postsRes . json ( ) ;
377+
378+ // Flatten posts from grouped by year format
379+ const allPosts = ( postsData . posts || [ ] ) . flatMap ( g => g . posts ) ;
380+
381+ // Posts come sorted desc (newest first), so we reverse for chronological order
382+ const posts = allPosts . reverse ( ) ;
383+
384+ // Find current post index
385+ const currentIndex = posts . findIndex ( p => String ( p . id ) === String ( postId ) ) ;
386+
387+ if ( currentIndex !== - 1 ) {
388+ data = {
389+ type : postType ,
390+ prev : currentIndex > 0 ? posts [ currentIndex - 1 ] : null ,
391+ next : currentIndex < posts . length - 1 ? posts [ currentIndex + 1 ] : null
392+ } ;
393+ console . log ( 'Fallback navigation data:' , data ) ;
394+ } else {
395+ console . log ( 'Could not find post in list' ) ;
396+ return ;
397+ }
398+ }
358399
359400 const postType = data . type || 'blog' ;
360401 const typeLabel = postType === 'story' ? 'Story' : 'Post' ;
361402
362403 // Bottom navigation
363- const prevBtn = document . getElementById ( 'prevPost' ) ;
364- const nextBtn = document . getElementById ( 'nextPost' ) ;
365-
366- if ( prevBtn && data . prev ) {
367- prevBtn . href = `./post.html?id=${ data . prev . id } ` ;
368- prevBtn . innerHTML = `<i class="bi bi-arrow-left"></i> Older ${ typeLabel } ` ;
369- prevBtn . title = data . prev . title ;
404+ // Left button = Newer (data.next), Right button = Older (data.prev)
405+ const prevBtn = document . getElementById ( 'prevPost' ) ; // Left button
406+ const nextBtn = document . getElementById ( 'nextPost' ) ; // Right button
407+
408+ // Left button shows NEWER post (data.next)
409+ if ( prevBtn && data . next ) {
410+ prevBtn . href = `./post.html?id=${ data . next . id } ` ;
411+ prevBtn . innerHTML = `<i class="bi bi-arrow-left"></i> Newer ${ typeLabel } ` ;
412+ prevBtn . title = data . next . title ;
370413 prevBtn . style . visibility = 'visible' ;
371414 }
372415
373- if ( nextBtn && data . next ) {
374- nextBtn . href = `./post.html?id=${ data . next . id } ` ;
375- nextBtn . innerHTML = `Newer ${ typeLabel } <i class="bi bi-arrow-right"></i>` ;
376- nextBtn . title = data . next . title ;
416+ // Right button shows OLDER post (data.prev)
417+ if ( nextBtn && data . prev ) {
418+ nextBtn . href = `./post.html?id=${ data . prev . id } ` ;
419+ nextBtn . innerHTML = `Older ${ typeLabel } <i class="bi bi-arrow-right"></i>` ;
420+ nextBtn . title = data . prev . title ;
377421 nextBtn . style . visibility = 'visible' ;
378422 }
379423
380424 // Sidebar navigation
381- const prevSidebar = document . getElementById ( 'prevPostSidebar' ) ;
382- const nextSidebar = document . getElementById ( 'nextPostSidebar' ) ;
425+ const prevSidebar = document . getElementById ( 'prevPostSidebar' ) ; // Shows older
426+ const nextSidebar = document . getElementById ( 'nextPostSidebar' ) ; // Shows newer
383427
428+ // prevSidebar shows OLDER (data.prev)
384429 if ( prevSidebar && data . prev ) {
385430 const link = prevSidebar . querySelector ( 'a' ) ;
386- link . href = `./post.html?id=${ data . prev . id } ` ;
387- link . textContent = `← ${ data . prev . title } ` ;
388- link . title = data . prev . title ;
389- prevSidebar . style . display = 'list-item' ;
431+ if ( link ) {
432+ link . href = `./post.html?id=${ data . prev . id } ` ;
433+ link . textContent = `← Older: ${ data . prev . title } ` ;
434+ link . title = data . prev . title ;
435+ prevSidebar . style . display = 'list-item' ;
436+ }
390437 }
391438
439+ // nextSidebar shows NEWER (data.next)
392440 if ( nextSidebar && data . next ) {
393441 const link = nextSidebar . querySelector ( 'a' ) ;
394- link . href = `./post.html?id=${ data . next . id } ` ;
395- link . textContent = `${ data . next . title } →` ;
396- link . title = data . next . title ;
397- nextSidebar . style . display = 'list-item' ;
442+ if ( link ) {
443+ link . href = `./post.html?id=${ data . next . id } ` ;
444+ link . textContent = `Newer: ${ data . next . title } →` ;
445+ link . title = data . next . title ;
446+ nextSidebar . style . display = 'list-item' ;
447+ }
398448 }
399449
400450 } catch ( err ) {
0 commit comments