|
281 | 281 | try { |
282 | 282 | const res = await fetch('whisper-demo/transcript.json', { cache: 'no-store' }); |
283 | 283 | if (res.ok) { |
284 | | - // Check if file was recently modified (within last 10 seconds) |
285 | | - const lastModified = res.headers.get('Last-Modified'); |
| 284 | + const data = await res.json().catch(() => null); |
286 | 285 | let isActive = false; |
287 | | - |
288 | | - if (lastModified) { |
289 | | - const modifiedDate = new Date(lastModified); |
290 | | - const now = new Date(); |
291 | | - const secondsSinceUpdate = (now - modifiedDate) / 1000; |
292 | | - isActive = secondsSinceUpdate < 10; // File updated in last 10 seconds |
293 | | - console.log('[Captions] Fetch successful - Age:', Math.round(secondsSinceUpdate), 's | Active:', isActive); |
| 286 | + |
| 287 | + // Prefer an explicit active flag from the producer |
| 288 | + if (data && data.active === true) { |
| 289 | + isActive = true; |
| 290 | + } else if (data && data.generated) { |
| 291 | + const t = Date.parse(data.generated); |
| 292 | + if (!Number.isNaN(t)) { |
| 293 | + const secondsSince = (Date.now() - t) / 1000; |
| 294 | + isActive = secondsSince < 10; // recent within 10s |
| 295 | + console.log('[Captions] Data generated age:', Math.round(secondsSince), 's | Active:', isActive); |
| 296 | + } |
| 297 | + } else { |
| 298 | + // Fallback: last-modified header as a hint |
| 299 | + const lastModified = res.headers.get('Last-Modified'); |
| 300 | + if (lastModified) { |
| 301 | + const modifiedDate = new Date(lastModified); |
| 302 | + const secondsSinceUpdate = (Date.now() - modifiedDate) / 1000; |
| 303 | + isActive = secondsSinceUpdate < 10; |
| 304 | + console.log('[Captions] Last-Modified age hint:', Math.round(secondsSinceUpdate), 's | Active:', isActive); |
| 305 | + } |
294 | 306 | } |
295 | | - |
296 | | - const data = await res.json(); |
297 | | - console.log('[Captions] Data received, length:', data.text?.length || 0); |
298 | | - // Button shows active status based on file modification time |
| 307 | + |
| 308 | + console.log('[Captions] Data received, text length:', data?.text?.length || 0); |
299 | 309 | updateCaptionButton(isActive); |
300 | | - // But always show the latest transcript text (even if slightly old) |
301 | | - updateLiveTranscript(data.text); |
| 310 | + updateLiveTranscript(data?.text || ''); |
302 | 311 | } else { |
303 | 312 | console.log('[Captions] Fetch failed:', res.status); |
304 | 313 | updateCaptionButton(false); |
|
0 commit comments