import { esc } from '../utils.js'; import { createElement, User } from 'lucide'; function postImages(post) { const embed = post?.embed; if (!embed) return []; if (embed.$type === 'app.bsky.embed.images#view') return embed.images || []; if (embed.$type === 'app.bsky.embed.recordWithMedia#view') { const media = embed.media; if (media?.$type === 'app.bsky.embed.images#view') return media.images || []; } return []; } export function createCard(profile, hotkey, onClick, isFollowed = false) { const card = document.createElement('div'); card.className = `border border-black dark:border-gray-600 p-3 cursor-pointer flex flex-col break-words${isFollowed ? ' opacity-60' : ''}`; const hint = isFollowed ? ` unfollow` : ''; const keyHtml = `[${hotkey}]${hint}`; const posts = (profile.topPosts || []).slice(0, 2); const postsHtml = posts.length ? posts.map((p, i) => { const lead = postImages(p)[0]; const leadHtml = lead ? `${esc(lead.alt || '')}` : ''; return `
${leadHtml}${esc(p.record?.text || '')}
${(p.likeCount || 0).toLocaleString()} likes
`; }).join('') : `
no recent posts
`; card.innerHTML = `
` + `
` + `
${keyHtml}${profile.displayName ? ` ${esc(profile.displayName)}` : ''}
` + `
@${esc(profile.handle)}${isFollowed ? ' (following)' : ''}
` + (profile.followersCount != null ? `
${profile.followersCount.toLocaleString()} followers
` : '') + `
` + `` + `
` + (profile.description ? `
${esc(profile.description.length > 100 ? profile.description.slice(0, profile.description.lastIndexOf(' ', 100)) + '…' : profile.description)}
` : '') + `
` + postsHtml + `
`; const slot = card.querySelector('.avatar-slot'); if (profile.avatar) { const img = document.createElement('img'); img.src = profile.avatar; img.className = 'w-10 h-10 object-cover block'; img.onerror = () => { img.remove(); slot.appendChild(createElement(User, { width: 16, height: 16, 'stroke-width': 2 })); }; slot.className = 'avatar-slot shrink-0 w-10 h-10 block'; slot.appendChild(img); } else { slot.appendChild(createElement(User, { width: 16, height: 16, 'stroke-width': 2 })); } card.addEventListener('click', onClick); return card; }