import type { Profile } from '../types' import { getAvatarUrl, getAvatarUrlRemote } from '../lib/api' // Service definitions for profile icons export interface ServiceLink { name: string icon: string url: string collection: string } // Get available services based on user's collections export function getServiceLinks(handle: string, collections: string[]): ServiceLink[] { const services: ServiceLink[] = [] if (collections.includes('ai.syui.card.user')) { services.push({ name: 'Card', icon: '/service/ai.syui.card.png', url: `/@${handle}/at/card`, collection: 'ai.syui.card.user' }) } return services } export async function renderProfile( did: string, profile: Profile, handle: string, webUrl?: string, localOnly = false, collections: string[] = [] ): Promise { // Local mode: sync, no API call. Remote mode: async with API call const avatarUrl = localOnly ? getAvatarUrl(did, profile, true) : await getAvatarUrlRemote(did, profile) const displayName = profile.value.displayName || handle || 'Unknown' const description = profile.value.description || '' // Build profile link (e.g., https://bsky.app/profile/did:plc:xxx) const profileLink = webUrl ? `${webUrl}/profile/${did}` : null const handleHtml = profileLink ? `@${escapeHtml(handle)}` : `@${escapeHtml(handle)}` const avatarHtml = avatarUrl ? `${escapeHtml(displayName)}` : `
` // Service icons (show for users with matching collections) let serviceIconsHtml = '' if (collections.length > 0) { const services = getServiceLinks(handle, collections) if (services.length > 0) { const iconsHtml = services.map(s => ` ${s.name} `).join('') serviceIconsHtml = `
${iconsHtml}
` } } return `
${avatarHtml}

${escapeHtml(displayName)}

${handleHtml}

${description ? `

${escapeHtml(description)}

` : ''}
${serviceIconsHtml}
` } export function mountProfile(container: HTMLElement, html: string): void { container.innerHTML = html } function escapeHtml(text: string): string { return text .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') }