/** * @fileoverview User Profile Component for Rantii * @author retoor * @description Complete user profile display with stats and content * @keywords user, profile, stats, about, content */ import { BaseComponent } from './base-component.js'; import { formatDate } from '../utils/date.js'; class UserProfile extends BaseComponent { static get observedAttributes() { return ['username', 'user-id']; } init() { this.profileData = null; this.isLoading = false; this.activeTab = 'rants'; this.render(); this.bindEvents(); } async load(username) { if (!username) return; this.isLoading = true; this.setAttr('username', username); this.render(); try { const result = await this.getApi()?.getProfileByUsername(username); if (result?.success) { this.profileData = result.profile; } } catch (error) { this.profileData = null; } finally { this.isLoading = false; this.render(); } } render() { if (this.isLoading) { this.setHtml(`
`); return; } if (!this.profileData) { this.setHtml(`

User not found

`); return; } const profile = this.profileData; const rants = profile.content?.content?.rants || []; const comments = profile.content?.content?.comments || []; this.addClass('user-profile'); this.setHtml(`

${profile.username}

+${profile.score}
${profile.about ? `
About

${profile.about}

` : ''} ${profile.location ? `
Location ${profile.location}
` : ''} ${profile.skills ? `
Skills ${profile.skills}
` : ''} ${profile.github ? ` ` : ''} ${profile.website ? ` ` : ''}
Joined ${formatDate(profile.created_time)}
${this.activeTab === 'rants' ? `
${rants.length > 0 ? rants.map(rant => ` `).join('') : '

No rants yet

'}
` : ''} ${this.activeTab === 'comments' ? `
${comments.length > 0 ? comments.map(comment => `
+${comment.score}
`).join('') : '

No comments yet

'}
` : ''}
`); this.initRantCards(rants); } escapeAttr(str) { if (!str) return ''; return str .replace(/&/g, '&') .replace(/"/g, '"') .replace(/'/g, ''') .replace(//g, '>'); } initRantCards(rants) { const cards = this.$$('rant-card'); cards.forEach((card, index) => { if (rants[index]) { card.setRant(rants[index]); } }); } bindEvents() { this.on(this, 'click', this.handleClick); } handleClick(e) { const backBtn = e.target.closest('.back-btn'); const tab = e.target.closest('.tab'); const profileComment = e.target.closest('.profile-comment'); if (backBtn) { e.preventDefault(); window.history.back(); return; } if (tab) { this.activeTab = tab.dataset.tab; this.render(); const rants = this.profileData?.content?.content?.rants || []; this.initRantCards(rants); return; } if (profileComment) { const rantId = profileComment.dataset.rantId; if (rantId) { this.getRouter()?.goToRant(rantId); } } } onAttributeChanged(name, oldValue, newValue) { if (name === 'username' && newValue && oldValue !== newValue) { this.load(newValue); } } getUsername() { return this.profileData?.username || this.getAttr('username'); } } customElements.define('user-profile', UserProfile); export { UserProfile };