// === OVERRIDE: loadHotTopics loads from MULTIPLE hashtags === // v3: 5 topics (AI Thế Giới + AI Việt Nam + top 3 hot) + lazy load more // This file loaded AFTER app_v2.js, overrides the function const HT_AI_TOPICS = ['AI Thế Giới', 'AI Việt Nam']; let _htAllSources = []; let _htTopics = []; let _htPages = 0; let _htDates = []; async function loadHotTopics(){ const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]})); const el=document.getElementById('hot-topics');if(!el)return; const topics=j.topics||[]; // Prepend AI topics that aren't already in the list const prepend = HT_AI_TOPICS.filter(ai => !topics.some(t => (t.topic || '').toLowerCase().includes(ai.toLowerCase())) ).map(ai => ({label: '#' + ai.replace(/\s+/g, ''), topic: ai, count: 0})); const finalTopics = [...prepend, ...topics].slice(0, 18); el.innerHTML=finalTopics.map(t=>{ const topicText=t.topic||t.label.replace(/^#/,''); return``; }).join(''); // Load tin HOT = tổng hợp từ TOP 5 hashtag (AI Thế Giới + AI Việt Nam + top 3 hot) const top5 = [...prepend, ...topics].slice(0, 5); if(top5.length>=2){ _htTopics = top5.map(t=>t.topic||t.label.replace(/^#/,'')); loadMultiHashtag(_htTopics); }else if(topics.length){ searchTopic(topics[0].topic||topics[0].label.replace(/^#/,'')); } } async function loadMultiHashtag(topicList){ const box=document.getElementById('hashtag-box');if(!box)return; _htAllSources = []; _htPages = 0; box.innerHTML=`

🔥 Tin HOT tổng hợp

Tổng hợp từ ${topicList.length} chủ đề nóng nhất...
`; try{ const results=await Promise.all(topicList.map(topic=> fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=0`).then(r=>r.json()).catch(()=>({sources:[]})) )); // Interleave from all topics const all=[];const seen=new Set(); const mx=Math.max(...results.map(r=>(r.sources||[]).length)); for(let i=0;i

🔥 Tin HOT

Lỗi tải tin
`;} } function _renderHT(){ const box=document.getElementById('hashtag-box');if(!box)return; const all = _htAllSources; if(!all.length){box.innerHTML=`

🔥 Tin HOT

Đang cập nhật...
`;return;} const perPage = 12; const total = all.length; const shown = Math.min((_htPages + 1) * perPage, total); const visible = all.slice(0, shown); let h=`

🔥 Tin HOT tổng hợp (${total} bài · ${_htTopics.length} chủ đề)

`; visible.forEach((s,i)=>{ const dateStr = _htDates[i] ? `${esc(_htDates[i])}` : ''; h+=`
${esc(s.title)}
${esc(s.via||'')}${dateStr} · #${esc(s._topic||'')}
`; }); h+=`
`; _htTopics.forEach(t=>{h+=``;}); h+=`
`; if(shown < total){ h+=``; } h+=`
`; box.innerHTML=h; // Lazy load images + dates visible.forEach((s,i)=>{if(!s.url)return;const ctrl=new AbortController();setTimeout(()=>ctrl.abort(),4000);fetch('/api/article?url='+encodeURIComponent(s.url),{signal:ctrl.signal}).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+i);if(el)el.innerHTML=``;}if(d&&d.date_vn){_htDates[i]=d.date_vn;const viaEl=document.querySelector('#ht-list .hashtag-src-item:nth-child('+(i+1)+') .hashtag-src-via');if(viaEl&&!viaEl.querySelector('.ht-date')){const sp=document.createElement('span');sp.className='ht-date';sp.style.cssText='font-size:9px;color:#666;margin-left:4px';sp.textContent=d.date_vn;viaEl.insertBefore(sp,viaEl.querySelector('span')||null);}}}).catch(()=>{});}); _htTopic = _htTopics[0]; } async function htLoadMore(){ const btn=document.getElementById('ht-load-more');if(!btn)return; btn.disabled=true;btn.textContent='Đang tải thêm...'; const nextPage = _htPages + 1; try{ // Fetch next page from each topic const results=await Promise.all(_htTopics.map(topic=> fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${nextPage}`).then(r=>r.json()).catch(()=>({sources:[]})) )); _htPages = nextPage; // Interleave new sources const seen=new Set(_htAllSources.map(s=>s.url)); const mx=Math.max(...results.map(r=>(r.sources||[]).length)); const newItems=[]; for(let i=0;i