web/website/control-panel/public/overview.mjs
Dev 6deb4a2e7d DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
2026-11-13 00:05:14 +01:00

29 lines
8.5 KiB
JavaScript

const dashboard=document.getElementById('dashboard'),nav=dashboard?.querySelector('nav');
const el=(tag,text)=>{const node=document.createElement(tag);if(text!==undefined)node.textContent=text;return node;};
const asset=(name,full=false)=>'https://www.dunerealms.com/assets/nav-'+name+(full?'-full-v2.webp':'-v2.webp');
const tools={user:[['Game identity','game','web.menu.game','account','Connect and review your Minecraft identity.'],['Inventory & sales','planetary-inventory','web.menu.inventory','exchange','Review your saved kit and trading history.'],['Reward codes','rewards','web.menu.rewards','clan','View earned community rewards.'],['Your tickets','tickets','web.menu.tickets','support','Follow your private support cases.'],['Xchange','exchange','web.menu.game','exchange','Browse the current game market.'],['Account security','security','web.menu.security','account','Manage your password and sessions.']],staff:[['Ticket desk','staff-tickets','web.tickets.staff','support','Review cases available to your team.'],['Forum moderation','forum-admin','web.menu.forum','boards','Review discussions and reports.'],['Website posts','website-posts','web.menu.posts','discord','Write and manage community news.'],['Website ranks','permissions','web.menu.permissions','clan','Manage website access.'],['Server operations','server-operations','web.menu.servers','home','View status and authorised server tools.'],['Staff manual','staff-manual','web.menu.staffmanual','manual','Find feature guides and procedures.'],['Exchange administration','exchange-admin','web.menu.exchange','exchange','Monitor the community market.'],['LuckPerms','luckperms','web.menu.luckperms','clan','Review native game permissions.']]};
let user=null;const views={};
if(dashboard&&nav){
for(const area of ['user','staff']){
const section=el('section');section.id=area+'-home';section.hidden=true;section.className='cp-overview';
const heading=el('div');heading.className='overview-heading';const intro=el('div');intro.append(el('p',area==='user'?'YOUR ACCOUNT':'STAFF WORKSPACE'),el('h2',area==='user'?'Your community, at a glance.':'The community desk.'),el('p',area==='user'?'Your identity, support and next steps in one place.':'A clear view of the queues and tools available to your role.'));heading.append(intro);const art=el('img');art.src=asset(area==='user'?'account':'support',true);art.alt='';art.width=144;art.height=144;heading.append(art);section.append(heading);
const stats=el('div');stats.className='overview-stat-grid';const status=el('p');status.className='overview-status';status.role='status';const refresh=el('button','Refresh overview');refresh.type='button';refresh.onclick=()=>load(area);const bar=el('div');bar.className='overview-status-bar';bar.append(status,refresh);section.append(stats,bar,el('h3','Quick shortcuts'));
const shortcuts=el('div');shortcuts.className='overview-tools';section.append(shortcuts);dashboard.append(section);
const link=el('a','Overview');link.href='#'+section.id;link.dataset.overviewArea=area;nav.prepend(link);
views[area]={section,stats,status,refresh,shortcuts,generation:0};
}
function shortcuts(){for(const area of ['user','staff']){const root=views[area].shortcuts;root.replaceChildren();for(const [label,id,node,art,copy]of tools[area]){if(area==='staff'&&!['staff-tickets','forum-admin','staff-manual'].includes(id))continue;if(!user?.permissions.includes(node))continue;const link=el('a');link.href='#'+id;link.className='overview-tool';const image=el('img');image.src=asset(art);image.alt='';image.width=48;image.height=48;const text=el('div');text.append(el('strong',label),el('span',copy));link.append(image,text);root.append(link);}}}
async function load(area){const view=views[area],generation=++view.generation;view.refresh.disabled=true;view.status.textContent='Updating your overview…';try{const response=await fetch('/api/overview'+(area==='staff'?'/staff':''),{cache:'no-store'}),data=await response.json();if(!response.ok)throw Error(data.message||'Overview unavailable.');if(generation!==view.generation)return;view.stats.replaceChildren();for(const metric of data.metrics){const card=el('a');card.href=metric.href;card.className='overview-stat';const image=el('img');image.src=asset(metric.artwork);image.alt='';image.width=36;image.height=36;card.append(image,el('span',metric.label),el('strong',metric.available?String(metric.value):'—'),el('small',metric.description));view.stats.append(card);}view.status.textContent='Updated '+new Date(data.updatedAt).toLocaleTimeString([], {hour:'2-digit',minute:'2-digit'})+' · Private to your account';}catch(error){view.status.textContent=error.message;}finally{if(generation===view.generation)view.refresh.disabled=false;}}
window.addEventListener('dune:session',event=>{user=event.detail;for(const view of Object.values(views))view.section.hidden=!user;shortcuts();if(user){const area=location.hash==='#staff-home'?'staff':'user';if(!location.hash||location.hash==='#'+area+'-home')load(area);}});
window.addEventListener('dune:page',event=>{if(user&&event.detail.page==='user-home')load('user');});
dashboard.addEventListener('click',event=>{const link=event.target.closest('.overview-tool,.overview-stat');if(!link)return;event.preventDefault();location.hash=link.hash;});
}
// A shared welcome and prominent private ticket queue for every staff workspace.
const staffWelcome={ 'staff-home':['Community','The community desk.','Support your community and keep conversations moving.','support'], 'website-staff-home':['Website','Your publishing desk.','Create, publish and manage the community website.','discord'], 'game-staff-home':['Minecraft','Your operations desk.','Manage the worlds, game content and player experience.','staff'], 'network-staff-home':['Network','Your network command desk.','All authorised staff tools, organised in one place.','clan'] };
let staffGeneration=0;
function prepareStaffHomes(){for(const [id,[area,title,copy,art]] of Object.entries(staffWelcome)){const section=document.getElementById(id);if(!section||section.dataset.welcomeReady)continue;section.dataset.welcomeReady='1';section.classList.add('staff-welcome');if(id==='staff-home'){section.replaceChildren();const grid=el('div');grid.className='staff-area-quicklinks';section.append(grid);}else{section.querySelector(':scope>h2')?.remove();section.querySelector(':scope>p')?.remove();}const heading=el('div');heading.className='overview-heading staff-welcome-heading';const text=el('div');text.append(el('p',area.toUpperCase()+' WORKSPACE'),el('h2',title),el('p',copy));const image=el('img');image.src=asset(art,true);image.alt='';image.width=112;image.height=112;heading.append(text,image);const queue=el('div');queue.className='staff-ticket-focus';queue.setAttribute('aria-live','polite');const quickTitle=el('h3','Quick shortcuts');quickTitle.className='staff-shortcuts-title';section.prepend(heading,queue,quickTitle);}}
async function loadStaffHome(id){prepareStaffHomes();const section=document.getElementById(id);if(!section)return;const queue=section.querySelector('.staff-ticket-focus'),generation=++staffGeneration;queue.replaceChildren(el('h3','Open tickets'),el('p','Checking your authorised queues…'));try{const response=await fetch('/api/overview/staff',{cache:'no-store'}),data=await response.json();if(!response.ok)throw Error(data.message||'Ticket overview unavailable.');if(generation!==staffGeneration)return;const tickets=data.metrics.filter(m=>m.key==='open-support'||m.key==='open-appeals');queue.replaceChildren();const intro=el('div');intro.className='staff-ticket-intro';intro.append(el('span','YOUR TEAM QUEUES'),el('h3','Open tickets'),el('p','Cases your role is authorised to review.'));queue.append(intro);if(!tickets.length){queue.append(el('p','Your role has no ticket queues assigned.'));return;}for(const metric of tickets){const link=el('a');link.href=metric.href;link.className='staff-ticket-card';link.append(el('strong',metric.available?String(metric.value):'—'),el('span',metric.label),el('small',metric.available?'Open ticket desk →':'Queue temporarily unavailable'));queue.append(link);}const refresh=el('button','Refresh tickets');refresh.type='button';refresh.onclick=()=>loadStaffHome(id);queue.append(refresh);}catch(error){queue.replaceChildren(el('h3','Open tickets'),el('p',error.message));}}
queueMicrotask(prepareStaffHomes);
window.addEventListener('dune:page',event=>{if(staffWelcome[event.detail.page])loadStaffHome(event.detail.page);});
window.addEventListener('dune:session',event=>{if(event.detail&&staffWelcome[location.hash.slice(1)])loadStaffHome(location.hash.slice(1));});