|
| 1 | +/** |
| 2 | + * Contacts view pane — renders a urn:solid:AddressBook as an address-book grid. |
| 3 | + * |
| 4 | + * Reads AddressBook.title and AddressBook.hasMember (an array of Person |
| 5 | + * references — either inlined objects with name/img/email or bare URI |
| 6 | + * references). Renders each member as a card with avatar, name, and contact |
| 7 | + * pills. Empty state when no members. |
| 8 | + * |
| 9 | + * Same data, different lens. Drop in alongside ui-pane (Inline) and |
| 10 | + * schema-pane (Edit) — LOSOS shows them all as tabs. |
| 11 | + * |
| 12 | + * AGPL-3.0 — part of solid-apps |
| 13 | + */ |
| 14 | + |
| 15 | +import { html, render } from 'https://losos.org/losos/html.js' |
| 16 | + |
| 17 | +const initial = (s) => (s || '?').trim().charAt(0).toUpperCase() |
| 18 | +const niceHost = (u) => { try { return new URL(u).host.replace(/^www\./, '') } catch { return u } } |
| 19 | + |
| 20 | +function asMember(m) { |
| 21 | + if (typeof m === 'string') return { id: m } |
| 22 | + if (m && typeof m === 'object') { |
| 23 | + return { |
| 24 | + id: m['@id'] || '', |
| 25 | + name: m.name, |
| 26 | + nick: m.nick, |
| 27 | + email: m.email, |
| 28 | + img: m.img, |
| 29 | + homepage: m.homepage, |
| 30 | + } |
| 31 | + } |
| 32 | + return null |
| 33 | +} |
| 34 | + |
| 35 | +export default { |
| 36 | + label: 'Contacts', |
| 37 | + icon: '\ud83d\udcd2', |
| 38 | + |
| 39 | + canHandle(subject, store) { |
| 40 | + const node = store.get(subject.value) |
| 41 | + if (!node) return false |
| 42 | + const t = store.type(node) |
| 43 | + if (!t) return false |
| 44 | + return Array.isArray(t) |
| 45 | + ? t.some(x => /AddressBook/i.test(x)) |
| 46 | + : /AddressBook/i.test(t) |
| 47 | + }, |
| 48 | + |
| 49 | + render(subject, lionStore, container, rawData) { |
| 50 | + let data = rawData |
| 51 | + if (!data) { |
| 52 | + const dataEl = document.querySelector('script[type="application/ld+json"]') |
| 53 | + try { data = JSON.parse(dataEl.textContent) } catch { return } |
| 54 | + } |
| 55 | + |
| 56 | + const title = data.title || 'Contacts' |
| 57 | + const description = data.description |
| 58 | + const rawMembers = Array.isArray(data.hasMember) ? data.hasMember : (data.hasMember ? [data.hasMember] : []) |
| 59 | + const members = rawMembers.map(asMember).filter(Boolean) |
| 60 | + |
| 61 | + const displayName = (m) => m.name || m.nick || (m.id ? niceHost(m.id) : 'Unknown') |
| 62 | + |
| 63 | + render(container, html` |
| 64 | + <style> |
| 65 | + .cv-bg { |
| 66 | + min-height: 100vh; |
| 67 | + background: |
| 68 | + radial-gradient(circle at 80% 0%, rgba(99,102,241,0.06) 0%, transparent 40%), |
| 69 | + radial-gradient(circle at 20% 100%, rgba(236,72,153,0.05) 0%, transparent 40%), |
| 70 | + linear-gradient(180deg, #fafaf8 0%, #f0efeb 100%); |
| 71 | + padding: 56px 24px 96px; |
| 72 | + } |
| 73 | + .cv-wrap { max-width: 880px; margin: 0 auto; } |
| 74 | + .cv-head { |
| 75 | + text-align: center; |
| 76 | + margin-bottom: 40px; |
| 77 | + } |
| 78 | + .cv-title { |
| 79 | + font: italic 400 44px/1.1 Georgia, serif; |
| 80 | + color: #1a1a1a; margin: 0 0 8px; |
| 81 | + letter-spacing: -0.8px; |
| 82 | + } |
| 83 | + .cv-desc { |
| 84 | + font: 400 15px/1.5 'Inter', -apple-system, sans-serif; |
| 85 | + color: #888; margin: 0 0 6px; |
| 86 | + } |
| 87 | + .cv-count { |
| 88 | + font: 600 11px/1 'Inter', -apple-system, sans-serif; |
| 89 | + letter-spacing: 0.12em; text-transform: uppercase; |
| 90 | + color: #999; |
| 91 | + } |
| 92 | + .cv-grid { |
| 93 | + display: grid; |
| 94 | + grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); |
| 95 | + gap: 16px; |
| 96 | + } |
| 97 | + .cv-card { |
| 98 | + background: #fff; |
| 99 | + border: 1px solid #e5e3de; |
| 100 | + border-radius: 12px; |
| 101 | + padding: 20px; |
| 102 | + display: flex; gap: 14px; align-items: center; |
| 103 | + text-decoration: none; |
| 104 | + color: inherit; |
| 105 | + transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease; |
| 106 | + } |
| 107 | + .cv-card:hover { |
| 108 | + transform: translateY(-2px); |
| 109 | + box-shadow: 0 8px 24px rgba(0,0,0,0.06); |
| 110 | + border-color: #6366f1; |
| 111 | + } |
| 112 | + .cv-avatar { |
| 113 | + width: 56px; height: 56px; border-radius: 50%; |
| 114 | + object-fit: cover; flex: 0 0 auto; |
| 115 | + background: #f5f4f0; |
| 116 | + } |
| 117 | + .cv-avatar-fallback { |
| 118 | + width: 56px; height: 56px; border-radius: 50%; |
| 119 | + background: linear-gradient(135deg, #6366f1, #8b5cf6); |
| 120 | + color: #fff; font: 600 22px/56px Georgia, serif; |
| 121 | + text-align: center; flex: 0 0 auto; |
| 122 | + } |
| 123 | + .cv-meta { min-width: 0; flex: 1; } |
| 124 | + .cv-name { |
| 125 | + font: 600 16px/1.2 'Inter', -apple-system, sans-serif; |
| 126 | + color: #1a1a1a; margin: 0 0 3px; |
| 127 | + white-space: nowrap; overflow: hidden; text-overflow: ellipsis; |
| 128 | + } |
| 129 | + .cv-sub { |
| 130 | + font: 400 13px/1.4 'Inter', -apple-system, sans-serif; |
| 131 | + color: #888; |
| 132 | + white-space: nowrap; overflow: hidden; text-overflow: ellipsis; |
| 133 | + } |
| 134 | + .cv-empty { |
| 135 | + background: #fff; |
| 136 | + border: 1px dashed #d4d2cd; |
| 137 | + border-radius: 12px; |
| 138 | + padding: 60px 20px; |
| 139 | + text-align: center; |
| 140 | + color: #999; |
| 141 | + font: 400 15px/1.5 'Inter', -apple-system, sans-serif; |
| 142 | + } |
| 143 | + </style> |
| 144 | +
|
| 145 | + <div class="cv-bg"> |
| 146 | + <div class="cv-wrap"> |
| 147 | + <div class="cv-head"> |
| 148 | + <h1 class="cv-title">${title}</h1> |
| 149 | + ${description ? html`<p class="cv-desc">${description}</p>` : null} |
| 150 | + <div class="cv-count">${members.length} ${members.length === 1 ? 'contact' : 'contacts'}</div> |
| 151 | + </div> |
| 152 | +
|
| 153 | + ${members.length === 0 |
| 154 | + ? html`<div class="cv-empty">No contacts yet.</div>` |
| 155 | + : html` |
| 156 | + <div class="cv-grid"> |
| 157 | + ${members.map(function(m) { |
| 158 | + const name = displayName(m) |
| 159 | + const sub = m.nick ? '@' + m.nick : (m.email || (m.id ? niceHost(m.id) : '')) |
| 160 | + const href = m.id || m.homepage || '#' |
| 161 | + return html` |
| 162 | + <a class="cv-card" href="${href}" target="_blank" rel="noopener"> |
| 163 | + ${m.img |
| 164 | + ? html`<img class="cv-avatar" src="${m.img}" alt="${name}" onerror="${function(e) { const f = document.createElement('div'); f.className = 'cv-avatar-fallback'; f.textContent = initial(name); e.target.replaceWith(f) }}" />` |
| 165 | + : html`<div class="cv-avatar-fallback">${initial(name)}</div>` |
| 166 | + } |
| 167 | + <div class="cv-meta"> |
| 168 | + <div class="cv-name">${name}</div> |
| 169 | + ${sub ? html`<div class="cv-sub">${sub}</div>` : null} |
| 170 | + </div> |
| 171 | + </a> |
| 172 | + ` |
| 173 | + })} |
| 174 | + </div> |
| 175 | + ` |
| 176 | + } |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + `) |
| 180 | + } |
| 181 | +} |
0 commit comments