web/website/control-panel/public/clan-profile-panels.mjs

25 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {mountClanDeedSettings} from './clan-deed-settings.mjs';
import {claimTariffs} from './cp-v2/cp-v2-claim-tariffs.mjs';
import {createOrganisationSettings} from './organisation-settings.mjs';
import {loadClanTreasury,loadClanMail,loadClanClaims} from './clan-dashboard-data.mjs';
import {createOrganisationDashboard} from './organisation-dashboard.mjs';
const el=(tag,text)=>{const node=document.createElement(tag);if(text!==undefined)node.textContent=text;return node;};
export function organiseClanProfile(host,data,{initialView='overview',motdForm=null}={}){
const dedicated=new URL(location.href).searchParams.get('own')==='1';if(dedicated){const back=host.querySelector(':scope > button');if(back)back.hidden=true;}const nav=el('nav');nav.className='clan-profile-tabs';nav.setAttribute('aria-label','Clan profile sections');
function openPage(view){if(dedicated){window.top.location.href='https://clans.dunerealms.com/#clan/clan-'+view;}else select(view);}
const panels=new Map(),overview=el('section'),roster=el('section'),grid=el('div');overview.className='clan-profile-overview';grid.className='clan-roster-cards';
overview.append(createOrganisationDashboard({kind:'clan',organisation:data.clan,members:data.members,role:data.role,canReview:data.canReview,requests:data.requests||[],activity:data.activity||[],activityStatus:data.activityStatus,loadClaims:data.role?()=>loadClanClaims(data.clan.id):undefined,loadTreasury:data.role?()=>loadClanTreasury(data.clan.id):undefined,loadMailbox:['owner','officer'].includes(data.role)||data.canManage?()=>loadClanMail({clanId:data.clan.id}):undefined,onMembers:()=>openPage('roster'),onRequests:()=>openPage('requests')}));
for(const node of [...host.children]){if(node.matches('img.clan-emblem,h3,span.clan-role')||node.tagName==='DIV'&&node.textContent==='['+data.clan.tag+']')node.remove();}const introduction=host.querySelector(':scope > p');if(introduction&&introduction.textContent===(data.clan.description||'No clan introduction yet.'))introduction.remove();
panels.set('overview',{label:'Overview',panel:overview});panels.set('roster',{label:'Roster',panel:roster});
const table=host.querySelector('.clan-table-scroll'),rows=[...(table?.querySelectorAll('tbody tr')||[])];
for(const [index,member]of data.members.entries()){
const card=el('article');card.className='clan-roster-member';const heading=el('header'),image=el('img'),link=el('a',member.username);image.src='https://cp.dunerealms.com/api/avatars/face/roster/'+encodeURIComponent(member.username);image.alt='';image.width=image.height=64;image.loading='lazy';link.href='https://www.dunerealms.com/people/'+encodeURIComponent(member.username);link.target='_top';heading.append(image,link);card.append(heading,el('strong',data.clan.titles?.[member.clan_role]||(member.clan_role==='owner'?'Clan owner':member.clan_role==='officer'?'Officer':'Member')),el('small','Joined '+new Date(Number(member.joined_at)).toLocaleDateString('en-GB')));
const actions=rows[index]?.querySelector('.clan-member-actions');if(actions?.firstChild){const controls=el('div');controls.className='clan-roster-controls';while(actions.firstChild)controls.append(actions.firstChild);card.append(controls);}grid.append(card);
}
table?.remove();roster.append(grid);
const requests=host.querySelector('.clan-requests');if(requests)panels.set('requests',{label:'Join requests',panel:requests});
const settingsNodes=[...host.querySelectorAll('[data-clan-settings]')];if(settingsNodes.length){const settings=el('section'),forms=[...settingsNodes[0].querySelectorAll(':scope > form')],leader=el('button','Open member management →');leader.type='button';leader.onclick=()=>openPage('roster');const motd=motdForm||el('p','Only the clan owner can publish the message of the day.');const claimInfo=el('div'),claimTiers=claimTariffs(data.clan.institution?.protected?'protected':'clan');for(const stage of ['Basic','Keyed','Royal']){const rows=claimTiers.filter(row=>row.stage===stage);claimInfo.append(el('p',stage+': '+rows[0].chunks+'–'+rows.at(-1).chunks+' total chunks'));}claimInfo.append(el('p','Upgrade claim capacity ingame by right-clicking the Home Block.'));const claimLink=el('a','View claim charter & prices →');claimLink.href='https://clans.dunerealms.com/#clan/clan-claims';claimLink.target='_top';claimInfo.append(claimLink);const archive=settingsNodes.find(n=>n.classList.contains('clan-danger'));settings.append(createOrganisationSettings([{title:'Identity & homeworld',description:'Clan name, banner tag, description and homeworld.',content:forms[0]},{title:'Clan emblem',description:'Your shared identity on the dashboard, directory and member profiles.',content:forms[1]},{title:'Message of the day',description:'Publish a separate welcome message for your members.',content:motd},{title:'Leadership & roles',description:'Member roles and ownership transfers use the existing authorised member controls.',content:leader},{title:'Ownership deed & dynasty',description:'Ruler, Heir and Consort are appointments on the deed. Ordinary member ranks remain unchanged.',content:(()=>{const host=el('div');mountClanDeedSettings(host,data.clan);return host;})()},{title:'Claim charter & capacity',description:'Planned claim limits, Home Block upgrades and upkeep. No current tier is assigned by this preview.',content:claimInfo,pending:true},{title:'Relations & subhouses',description:'Parent organisations, subhouses and subsidiaries will be managed here.',pending:true},{title:'Archive clan',description:'Review the existing archive conditions and confirmation before making changes.',content:archive,danger:true}]));for(const node of settingsNodes)if(node!==archive)node.remove();panels.set('settings',{label:'Clan Settings',panel:settings});}
for(const [id,value]of panels){value.panel.classList.add('clan-profile-panel');value.panel.dataset.clanPanel=id;const button=el('button',value.label);button.type='button';button.onclick=()=>select(id);value.button=button;nav.append(button);}
function select(id){nav.hidden=dedicated;for(const [key,{panel,button}]of panels){panel.hidden=key!==id;button.setAttribute('aria-current',key===id?'page':'false');}}
host.append(nav);for(const {panel}of panels.values())host.append(panel);select(panels.has(initialView)?initialView:'overview');
}