31 lines
6.8 KiB
JavaScript
31 lines
6.8 KiB
JavaScript
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
|
||
const dashboard=document.getElementById('dashboard'),nav=dashboard?.querySelector('nav');
|
||
const section=el('section');section.id='site-profile';section.hidden=true;section.className='site-profile-page';
|
||
section.append(el('p','YOUR COMMUNITY IDENTITY'),el('h2','Site profile'),el('p','Introduce yourself, share your stream and choose the social links displayed beside your forum posts. Your Minecraft identity and login name remain separate.'));
|
||
const publicLink=el('a','View my public profile & honours');publicLink.target='_blank';publicLink.rel='noopener';publicLink.hidden=true;section.append(publicLink);
|
||
const status=el('p');status.role='status';section.append(status);
|
||
const gitPanel=el('aside');gitPanel.className='profile-preview';const gitHeading=el('h3','DuneHub <20> Your code forge'),gitStatus=el('p'),gitLink=el('a','Browse DuneGit');gitLink.href='https://www.dunerealms.com/git/';gitPanel.append(gitHeading,gitStatus,gitLink);section.append(gitPanel);
|
||
const honourBox=el('aside');honourBox.className='profile-preview';section.append(honourBox);
|
||
function renderHonours(honours=[]){honourBox.replaceChildren();honourBox.hidden=!honours.length;for(const h of honours){const card=el('section'),image=el('img'),title=el('h3',h.title),citation=el('p',h.citation||'Honoured member of the Order.');image.src=h.seal;image.alt=h.title+' seal';image.width=image.height=130;citation.style.whiteSpace='pre-wrap';card.append(image,title,citation);honourBox.append(card);}}
|
||
const form=el('form');form.className='site-profile-form';
|
||
const fields={};function field(key,label,type='text',limit){const box=el('label',label),input=el(type==='textarea'?'textarea':'input');input.name=key;if(type!=='textarea')input.type=type;if(limit)input.maxLength=limit;box.append(input);form.append(box);fields[key]=input;return input;}
|
||
field('displayName','Display name','text',40).required=true;
|
||
field('bio','Bio / About your stream','textarea',1000).placeholder='Your adventures, broadcasts and community interests…';
|
||
field('publicInfo','Other public information','textarea',500);
|
||
const socials=el('fieldset');socials.className='profile-social-grid';socials.append(el('legend','Social buttons'));
|
||
const labels={twitch:'Twitch',youtube:'YouTube',twitter:'X / Twitter',facebook:'Facebook',discord:'Discord',instagram:'Instagram',tiktok:'TikTok',github:'GitHub',website:'Your website'};
|
||
const placeholders={twitch:'https://www.twitch.tv/yourchannel',youtube:'https://www.youtube.com/@yourchannel',twitter:'https://x.com/yourname',facebook:'https://www.facebook.com/yourpage',discord:'https://discord.gg/yourinvite',instagram:'https://www.instagram.com/yourname',tiktok:'https://www.tiktok.com/@yourname',github:'https://github.com/yourname',website:'https://example.com'};
|
||
for(const[key,label]of Object.entries(labels)){const box=el('label',label),input=el('input');input.type='url';input.name=key;input.placeholder=placeholders[key];input.maxLength=500;box.append(input);socials.append(box);fields[key]=input;}form.append(socials);
|
||
const birthday=el('details');birthday.append(el('summary','Optional birthday preferences'));
|
||
const birthLabel=el('label','Birthday'),birth=el('input');birth.type='date';birth.name='birthday';birthLabel.append(birth);birthday.append(birthLabel);fields.birthday=birth;
|
||
const publicLabel=el('label','Show birthday on my site profile'),publicBirth=el('input');publicBirth.type='checkbox';publicLabel.prepend(publicBirth);birthday.append(publicLabel,el('p','Birthday is not shared with forum social buttons or streaming links.'));form.append(birthday);
|
||
const save=el('button','Save profile');save.type='submit';form.append(save);section.append(form);
|
||
const preview=el('aside');preview.className='profile-preview';preview.append(el('h3','Public preview'));const previewName=el('strong'),previewBio=el('p'),previewInfo=el('p'),previewLinks=el('div');preview.append(previewName,previewBio,previewInfo,previewLinks);section.append(preview);dashboard?.append(section);
|
||
const link=el('a','Site profile');link.href='#site-profile';link.hidden=true;nav?.append(link);
|
||
function renderPreview(){previewName.textContent=fields.displayName.value;previewBio.textContent=fields.bio.value;previewInfo.textContent=fields.publicInfo.value;previewLinks.replaceChildren();for(const[key,label]of Object.entries(labels)){const value=fields[key].value;if(!value)continue;try{const url=new URL(value);if(url.protocol!=='https:'||url.username||url.password)continue;const a=el('a',label);a.href=url.href;a.target='_blank';a.rel='noopener noreferrer';previewLinks.append(a);}catch{}}}
|
||
form.addEventListener('input',renderPreview);
|
||
async function load(){try{const response=await fetch('/api/account/profile',{cache:'no-store'});if(!response.ok)throw Error('Sign in to edit your site profile.');const{profile}=await response.json();if(profile.username){publicLink.href='https://www.dunerealms.com/people/'+encodeURIComponent(profile.username);publicLink.hidden=false;}renderHonours(profile.honours);gitStatus.textContent=profile.git?'Verified account: '+profile.git.login+' <20> '+profile.git.ownedRepoCount+' owned repositories':'Your DuneGit account is being prepared. You can browse the community forge below.';gitLink.href=profile.git?.profileUrl||'https://www.dunerealms.com/git/';gitLink.textContent=profile.git?'Open my DuneHub profile':'Browse DuneGit';for(const key of ['displayName','bio','publicInfo','birthday'])fields[key].value=profile[key]??(key==='displayName'?profile.display_name:'')??'';publicBirth.checked=profile.publicBirthday;for(const key of Object.keys(labels))fields[key].value=profile.socials?.[key]||'';section.hidden=false;link.hidden=false;renderPreview();}catch(error){status.textContent=error.message;}}
|
||
form.onsubmit=async event=>{event.preventDefault();save.disabled=true;status.textContent='Saving profile…';try{const body={displayName:fields.displayName.value,bio:fields.bio.value,publicInfo:fields.publicInfo.value,birthday:fields.birthday.value,publicBirthday:publicBirth.checked,socials:Object.fromEntries(Object.keys(labels).map(key=>[key,fields[key].value]))};const response=await fetch('/api/account/profile',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});const data=await response.json();if(!response.ok)throw Error(data.message||'Profile could not be saved.');status.textContent='Profile saved. Public details are shared with your community account.';renderPreview();}catch(error){status.textContent=error.message;}finally{save.disabled=false;}};
|
||
window.addEventListener('dune:session',event=>{if(event.detail)load();else{section.hidden=true;link.hidden=true;}});
|
||
window.addEventListener('dune:page',event=>{if(event.detail.page==='site-profile')load();});
|
||
const session=await fetch('/api/auth/session',{cache:'no-store'});if(session.ok&&(await session.json()).authenticated)await load();
|

Staff