MC/website/web/feeds.js
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

6 lines
4.3 KiB
JavaScript

const kind=document.body.dataset.feed;let records=[],connected=false,nextCursor=null,busy=false;const results=document.querySelector('#feed-results'),state=document.querySelector('#feed-state'),more=document.querySelector('#load-more');
function element(tag,text,cls){const e=document.createElement(tag);if(text!==undefined)e.textContent=text;if(cls)e.className=cls;return e}
function empty(title,message){const box=element('div',undefined,'feed-empty');box.append(element('span','✦','seal'),element('h2',title),element('p',message));results.replaceChildren(box)}
function draw(){if(!connected)return;const q=document.querySelector('#feed-search').value.trim().toLowerCase(),type=document.querySelector('#type-filter').value;const shown=records.filter(r=>(type==='all'||(kind==='exchange'?r.type:r.category)===type)&&window.realmNames.text(JSON.stringify(r)).toLowerCase().includes(q));if(!shown.length){empty(records.length?'No matching records':kind==='exchange'?'The exchange is quiet':'The dispatch is quiet',records.length?'Try another filter or search.':'The server is connected, but there are no records to display.');return}const grid=element('div',undefined,kind==='exchange'?'feed-grid':'news-list');for(const r of shown){const card=element('article',undefined,kind==='exchange'?'offer-card':'news-card');if(kind==='exchange'){card.append(element('span',r.type==='contract'?'HARVEST CONTRACT':r.type==='buy'?'BUYING':'SELLING','offer-type'),element('h3',window.realmNames.material(r.item)),element('p',window.realmNames.text(r.description||'')));const dl=element('dl');for(const [label,value]of [['Posted by',r.owner],['Quantity',r.quantity===null?'Standing offer':r.quantity],['Price / reward',r.price===null?'15% worker / 85% sponsor':`${r.price.toLocaleString()} dunemarks per unit`],['Status',r.status]]){const row=element('div');row.append(element('dt',label),element('dd',String(value)));dl.append(row)}card.append(dl);if(r.type==='contract'&&r.status==='open'&&/^(?:[A-Za-z0-9_]{1,16}|[0-9a-f-]{36})$/i.test(r.owner)){card.append(element('p',`In game: /dune job accept ${r.owner}`))}}else{card.append(element('span',r.category,'news-category'),element('h3',window.realmNames.text(r.title)),element('p',window.realmNames.text(r.message)));const time=element('time',new Date(r.timestamp).toLocaleString());time.dateTime=r.timestamp;card.append(time)}grid.append(card)}results.replaceChildren(grid)}
async function refresh(append=false){if(busy)return;busy=true;document.querySelector('#refresh-feed').disabled=true;try{const query=append&&nextCursor?'?before='+encodeURIComponent(nextCursor):'';const response=await fetch('/api/'+kind+query,{headers:{Accept:'application/json'}});if(!response.ok)throw Error();const data=await response.json();connected=data.status==='connected';if(!connected){records=[];nextCursor=null;more.hidden=true;state.className='feed-state';state.textContent=data.status==='not_connected'?'Server not connected':data.message||'The connection is temporarily unavailable';empty('Awaiting the Compact connection',data.status==='not_connected'?'The Minecraft server has not been connected yet. Real offers and public notifications will appear here once the DiscordSRV connection is configured.':'The server feed is temporarily unavailable. Please refresh shortly.');return}const incoming=kind==='exchange'?data.offers:data.notifications;records=append?[...records,...incoming.filter(r=>!records.some(x=>x.id===r.id))]:incoming;nextCursor=data.nextCursor||null;more.hidden=!nextCursor;state.className='feed-state live';state.textContent=`Connected · ${records.length} records · Updated ${new Date(data.updatedAt).toLocaleTimeString()}`;draw()}catch{state.className='feed-state error';state.textContent='Connection unavailable';if(!records.length)empty('Awaiting the Compact connection','The live feed cannot be reached yet. No offers or announcements have been fabricated.');else state.textContent+=' · Showing the last received records'}finally{busy=false;document.querySelector('#refresh-feed').disabled=false}}
document.querySelector('#refresh-feed').addEventListener('click',()=>refresh());more.addEventListener('click',()=>refresh(true));document.querySelector('#type-filter').addEventListener('change',draw);document.querySelector('#feed-search').addEventListener('input',draw);refresh();setInterval(()=>{if(!document.hidden&&kind==='exchange')refresh()},60000);