import {createClient} from '/account-base.js'; import {applyFrame,renderBadgeIcons} from './profile-customization.js'; const $=id=>document.getElementById(id); const PAGE_SIZE=24; let db,offset=0,searchVersion=0; function node(tag,className,text=''){const el=document.createElement(tag);if(className)el.className=className;el.textContent=text;return el;} function status(message,error=false){const el=$('people-status');el.textContent=message;el.dataset.error=String(error);el.dataset.tone=error?'error':message.startsWith('Loading')?'loading':'quiet';} async function rpc(name,args={}){const {data,error}=await db.rpc(name,args);if(error)throw new Error(error.message);return data;} function profileCard(row){ const card=node('article','person-card'); const link=node('a','person-card-link'); link.href='/users/'+encodeURIComponent(row.username); const cover=node('div','person-cover'); if(row.banner_url?.startsWith('https://')){ const banner=node('img'); banner.src=row.banner_url;banner.alt='';banner.loading='lazy';banner.decoding='async'; banner.addEventListener('load',()=>applyFrame(banner,{ x:row.banner_x,y:row.banner_y,zoom:row.banner_zoom })); banner.addEventListener('error',()=>banner.remove()); cover.append(banner); } const lower=node('div','person-card-content'); const image=node('div','person-avatar'); const fallback=node('span','',row.display_name.trim().charAt(0).toUpperCase()||'?'); image.append(fallback); if(row.avatar_url?.startsWith('https://')){ const img=node('img');img.src=row.avatar_url;img.alt='';img.loading='lazy'; img.addEventListener('load',()=>{ fallback.hidden=true; applyFrame(img,{x:row.avatar_x,y:row.avatar_y,zoom:row.avatar_zoom}); }); img.addEventListener('error',()=>img.remove()); image.append(img); } const details=node('div','person-details'); const heading=node('div','person-name-line'); heading.append(node('h2','',row.display_name)); const icons=node('div','person-inline-badges'); renderBadgeIcons(icons,row.badges); heading.append(icons); details.append(heading,node('p','person-handle','@'+row.username)); if(row.bio)details.append(node('p','person-bio',row.bio)); lower.append(image,details); link.append(cover,lower); card.append(link); return card; } async function search(){ const version=++searchVersion; status('Loading profiles…'); $('people-results').setAttribute('aria-busy','true'); $('people-prev').disabled=true;$('people-next').disabled=true; try{ const rows=await rpc('community_search_profiles_v2',{ p_search:$('people-query').value.trim(),p_limit:PAGE_SIZE,p_offset:offset }); if(version!==searchVersion)return; $('people-results').replaceChildren(); for(const item of rows)$('people-results').append(profileCard(item)); if(!rows.length)$('people-results').append(node('p','muted','No public profiles found. Try another name.')); $('people-page').textContent='Page '+(Math.floor(offset/PAGE_SIZE)+1); $('people-prev').disabled=offset===0; $('people-next').disabled=rows.length=10000-PAGE_SIZE; status(rows.length ? rows.length + (rows.length===1?' member found':' members shown') : 'No matching public profiles.'); }catch(error){if(version===searchVersion){status(error.message||'Unable to search profiles.',true);$('people-prev').disabled=offset===0;}} finally{if(version===searchVersion)$('people-results').removeAttribute('aria-busy');} } async function myReports(){ const rows=await rpc('hub_content_reports',{p_staff:false,p_offset:0}); $('my-reports').hidden=false; const container=$('my-report-list');container.replaceChildren(); if(!rows.length){container.append(node('p','muted','You have not submitted any reports.'));return;} for(const row of rows){ const item=node('div','people-report'); item.append(node('strong','',`#${row.id} · ${row.category}`), node('span','person-report-status',row.state.replaceAll('-',' ')), node('p','',(row.content_kind||'profile')+' · '+row.content_title), node('p','',row.details), node('small','',new Date(row.created_at).toLocaleString())); container.append(item); } } async function start(){ const response=await fetch('/api/account/config',{cache:'no-store'}); if(!response.ok)throw new Error('Account configuration unavailable.'); const config=await response.json(); if(!config.enabled)throw new Error('Accounts are not enabled.'); db=createClient(config.url,config.key,{auth:{flowType:'pkce',detectSessionInUrl:false,persistSession:true,autoRefreshToken:true}}); const initial=new URL(location.href).searchParams.get('q'); if(initial)$('people-query').value=initial.slice(0,60); $('people-search').addEventListener('submit',event=>{ event.preventDefault();offset=0; const value=$('people-query').value.trim(); const url=new URL(location.href);if(value)url.searchParams.set('q',value);else url.searchParams.delete('q'); history.replaceState(null,'',url); search(); }); $('people-prev').addEventListener('click',()=>{offset=Math.max(0,offset-PAGE_SIZE);search();}); $('people-next').addEventListener('click',()=>{offset+=PAGE_SIZE;search();}); await search(); const {data}=await db.auth.getSession(); if(data.session)myReports().catch(()=>{$('my-reports').hidden=true;}); } start().catch(error=>status(error.message||'Unable to load directory.',true));