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