| @@ -43,15 +43,8 @@ |
| 43 | 43 | loading = true; |
| 44 | 44 | loader.classList.remove('hidden'); |
| 45 | 45 | |
| 46 | | let url; |
| 47 | | if (view === 'dashboard') { |
| 48 | | url = `/api/dashboard?offset=${offset}&type=${postType}`; |
| 49 | | } else if (view === 'blog') { |
| 50 | | url = `/api/blog/${encodeURIComponent(blog)}?offset=${offset}&type=${postType}`; |
| 51 | | } else if (view === 'search') { |
| 52 | | url = `/api/search?q=${encodeURIComponent(query)}`; |
| 53 | | exhausted = true; // tagged endpoint returns one batch |
| 54 | | } |
| 46 | const url = postsUrl(); |
| 47 | if (view === 'search') exhausted = true; // tagged endpoint returns one batch |
| 55 | 48 | |
| 56 | 49 | try { |
| 57 | 50 | const resp = await fetch(url); |
| @@ -59,16 +52,11 @@ |
| 59 | 52 | const data = await resp.json(); |
| 60 | 53 | const newPosts = Array.isArray(data) ? data : (data || []); |
| 61 | 54 | |
| 62 | | if (!newPosts.length) { |
| 55 | renderPosts(newPosts); |
| 56 | offset += newPosts.length; |
| 57 | if (newPosts.length < 20) { |
| 63 | 58 | exhausted = true; |
| 64 | 59 | endMsg.classList.remove('hidden'); |
| 65 | | } else { |
| 66 | | renderPosts(newPosts); |
| 67 | | offset += newPosts.length; |
| 68 | | if (newPosts.length < 20) { |
| 69 | | exhausted = true; |
| 70 | | endMsg.classList.remove('hidden'); |
| 71 | | } |
| 72 | 60 | } |
| 73 | 61 | } catch (err) { |
| 74 | 62 | console.error('Fetch error:', err); |
| @@ -78,6 +66,13 @@ |
| 78 | 66 | } |
| 79 | 67 | } |
| 80 | 68 | |
| 69 | function postsUrl() { |
| 70 | if (view === 'dashboard') return `/api/dashboard?offset=${offset}&type=${postType}`; |
| 71 | if (view === 'blog') return `/api/blog/${encodeURIComponent(blog)}?offset=${offset}&type=${postType}`; |
| 72 | if (view === 'search') return `/api/search?q=${encodeURIComponent(query)}`; |
| 73 | return undefined; |
| 74 | } |
| 75 | |
| 81 | 76 | // ── Render ───────────────────────────────────────────────── |
| 82 | 77 | function renderPosts(newPosts) { |
| 83 | 78 | const startIndex = posts.length; |
| @@ -90,29 +85,29 @@ |
| 90 | 85 | }); |
| 91 | 86 | } |
| 92 | 87 | |
| 93 | | function buildCard(post, idx) { |
| 94 | | let mediaSrc = null; |
| 95 | | let isVideo = false; |
| 96 | | let thumb = null; |
| 97 | | |
| 88 | // mediaOf returns the post's displayable media, or null for other post types. |
| 89 | function mediaOf(post) { |
| 98 | 90 | if (post.type === 'photo' && post.photos && post.photos.length) { |
| 99 | | mediaSrc = post.photos[0].original_size.url; |
| 100 | | } else if (post.type === 'video' && post.video_url) { |
| 101 | | mediaSrc = post.video_url; |
| 102 | | thumb = post.thumbnail_url; |
| 103 | | isVideo = true; |
| 91 | return { src: post.photos[0].original_size.url, isVideo: false }; |
| 104 | 92 | } |
| 93 | if (post.type === 'video' && post.video_url) { |
| 94 | return { src: post.video_url, poster: post.thumbnail_url, isVideo: true }; |
| 95 | } |
| 96 | return null; |
| 97 | } |
| 105 | 98 | |
| 106 | | if (!mediaSrc) return null; |
| 99 | function buildCard(post, idx) { |
| 100 | const media = mediaOf(post); |
| 101 | if (!media) return null; |
| 107 | 102 | |
| 108 | 103 | const card = document.createElement('div'); |
| 109 | 104 | card.className = 'post-card'; |
| 110 | 105 | card.dataset.index = idx; |
| 111 | 106 | |
| 112 | | if (isVideo) { |
| 107 | if (media.isVideo) { |
| 113 | 108 | const vid = document.createElement('video'); |
| 114 | | vid.src = mediaSrc; |
| 115 | | if (thumb) vid.poster = thumb; |
| 109 | vid.src = media.src; |
| 110 | if (media.poster) vid.poster = media.poster; |
| 116 | 111 | vid.muted = true; |
| 117 | 112 | vid.loop = true; |
| 118 | 113 | vid.playsInline = true; |
| @@ -121,7 +116,7 @@ |
| 121 | 116 | card.appendChild(vid); |
| 122 | 117 | } else { |
| 123 | 118 | const img = document.createElement('img'); |
| 124 | | img.src = mediaSrc; |
| 119 | img.src = media.src; |
| 125 | 120 | img.loading = 'lazy'; |
| 126 | 121 | img.alt = ''; |
| 127 | 122 | card.appendChild(img); |
| @@ -162,26 +157,25 @@ |
| 162 | 157 | lbPrev.style.visibility = lightboxIndex > 0 ? 'visible' : 'hidden'; |
| 163 | 158 | lbNext.style.visibility = lightboxIndex < posts.length - 1 ? 'visible' : 'hidden'; |
| 164 | 159 | |
| 165 | | if (post.type === 'photo' && post.photos && post.photos.length) { |
| 166 | | const img = document.createElement('img'); |
| 167 | | img.src = post.photos[0].original_size.url; |
| 168 | | img.alt = ''; |
| 169 | | lbMedia.appendChild(img); |
| 170 | | } else if (post.type === 'video' && post.video_url) { |
| 160 | const media = mediaOf(post); |
| 161 | if (media && media.isVideo) { |
| 171 | 162 | const vid = document.createElement('video'); |
| 172 | | vid.src = post.video_url; |
| 173 | | if (post.thumbnail_url) vid.poster = post.thumbnail_url; |
| 163 | vid.src = media.src; |
| 164 | if (media.poster) vid.poster = media.poster; |
| 174 | 165 | vid.controls = true; |
| 175 | 166 | vid.autoplay = true; |
| 176 | 167 | lbMedia.appendChild(vid); |
| 168 | } else if (media) { |
| 169 | const img = document.createElement('img'); |
| 170 | img.src = media.src; |
| 171 | img.alt = ''; |
| 172 | lbMedia.appendChild(img); |
| 177 | 173 | } |
| 178 | 174 | |
| 179 | 175 | // Actions |
| 180 | 176 | lbLike.dataset.id = post.id_string || post.id; |
| 181 | 177 | lbLike.dataset.key = post.reblog_key; |
| 182 | | lbLike.dataset.liked = post.liked ? 'true' : 'false'; |
| 183 | | lbLike.classList.toggle('liked', !!post.liked); |
| 184 | | lbLike.textContent = post.liked ? '♥ Liked' : '♡ Like'; |
| 178 | showLiked(!!post.liked); |
| 185 | 179 | |
| 186 | 180 | lbReblog.dataset.id = post.id_string || post.id; |
| 187 | 181 | lbReblog.dataset.key = post.reblog_key; |
| @@ -205,14 +199,23 @@ |
| 205 | 199 | if (lightboxIndex < posts.length - 1) { lightboxIndex++; renderLightbox(); } |
| 206 | 200 | }); |
| 207 | 201 | |
| 202 | const lightboxKeys = { |
| 203 | Escape: closeLightbox, |
| 204 | ArrowLeft: () => lbPrev.click(), |
| 205 | ArrowRight: () => lbNext.click(), |
| 206 | }; |
| 208 | 207 | document.addEventListener('keydown', e => { |
| 209 | | if (lightbox.classList.contains('hidden')) return; |
| 210 | | if (e.key === 'Escape') closeLightbox(); |
| 211 | | if (e.key === 'ArrowLeft') lbPrev.click(); |
| 212 | | if (e.key === 'ArrowRight') lbNext.click(); |
| 208 | const action = lightboxKeys[e.key]; |
| 209 | if (action && !lightbox.classList.contains('hidden')) action(); |
| 213 | 210 | }); |
| 214 | 211 | |
| 215 | 212 | // ── Like ─────────────────────────────────────────────────── |
| 213 | function showLiked(liked) { |
| 214 | lbLike.dataset.liked = liked ? 'true' : 'false'; |
| 215 | lbLike.classList.toggle('liked', liked); |
| 216 | lbLike.textContent = liked ? '♥ Liked' : '♡ Like'; |
| 217 | } |
| 218 | |
| 216 | 219 | lbLike.addEventListener('click', async () => { |
| 217 | 220 | const id = lbLike.dataset.id; |
| 218 | 221 | const key = lbLike.dataset.key; |
| @@ -221,11 +224,8 @@ |
| 221 | 224 | try { |
| 222 | 225 | const resp = await fetch(url, { method: 'GET' }); |
| 223 | 226 | if (!resp.ok) throw new Error(await resp.text()); |
| 224 | | const post = posts[lightboxIndex]; |
| 225 | | post.liked = !liked; |
| 226 | | lbLike.dataset.liked = post.liked ? 'true' : 'false'; |
| 227 | | lbLike.classList.toggle('liked', post.liked); |
| 228 | | lbLike.textContent = post.liked ? '♥ Liked' : '♡ Like'; |
| 227 | posts[lightboxIndex].liked = !liked; |
| 228 | showLiked(!liked); |
| 229 | 229 | } catch (err) { |
| 230 | 230 | console.error('Like error:', err); |
| 231 | 231 | } |