/
community-memberships-list.js
142 lines (128 loc) · 4.8 KB
/
community-memberships-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { LitElement, html } from '../../vendor/lit-element/lit-element.js'
import { repeat } from '../../vendor/lit-element/lit-html/directives/repeat.js'
import { intersect } from '../lib/functions.js'
import * as session from '../lib/session.js'
import * as displayNames from '../lib/display-names.js'
import { AVATAR_URL } from '../lib/const.js'
export class CommunityMembershipsList extends LitElement {
static get properties () {
return {
userId: {type: String, attribute: 'user-id'},
memberships: {type: Array},
sharedCommunities: {type: Array},
isExpanded: {type: Boolean},
renderOpts: {type: Boolean}
}
}
createRenderRoot() {
return this // dont use shadow dom
}
constructor () {
super()
this.setAttribute('ctzn-elem', '1')
this.view = undefined
this.userId = undefined
this.memberships = undefined
this.sharedCommunities = undefined
this.isExpanded = false
this.renderOpts = {expandedOnly: false}
}
get showExpanded () {
return this.isExpanded || this.renderOpts?.expandedOnly
}
get canToggleExpanded () {
return !this.renderOpts?.expandedOnly && this.memberships?.length
}
setContextState (state) {
if (state?.page?.userId) {
if (!this.userId) {
this.userId = state.page.userId
}
}
}
updated (changedProperties) {
if (changedProperties.has('userId') && this.userId !== changedProperties.get('userId')) {
this.load()
}
}
async load () {
this.isExpanded = false
this.memberships = undefined
this.sharedCommunities = undefined
this.memberships = await session.ctzn.db(this.userId).table('ctzn.network/community-membership').list()
if (session.isActive() && this.userId !== session.info.userId) {
this.sharedCommunities = intersect(
session.myCommunities.map(c => c.userId),
this.memberships.map(m => m.value.community.userId)
)
}
}
// rendering
// =
render () {
if (typeof this.memberships === 'undefined') {
return html`
<div class="bg-white sm:rounded my-1 px-5 py-3">
<span class="text-lg font-medium mr-1">Communities</span>
<span class="spinner text-gray-500"></span>
</div>
`
}
return html`
<div class="bg-white sm:rounded my-1 ${this.memberships ? 'pb-1' : ''}">
<div
class="px-5 py-3 sm:rounded ${this.canToggleExpanded ? 'cursor-pointer hov:hover:text-blue-600' : ''}"
@click=${this.canToggleExpanded ? this.onToggleExpanded : undefined}
>
<div class="flex items-center justify-between">
<span>
<span class="text-lg font-medium mr-1">Communities</span>
<span class="text-gray-500 font-bold">${this.memberships?.length || '0'}</span>
</span>
${this.canToggleExpanded ? html`
<span class="fas fa-angle-${this.isExpanded ? 'up' : 'down'}"></span>
` : ''}
</div>
${this.sharedCommunities?.length ? html`
<div class="pt-1 flex items-center text-gray-500">
<span class="mr-2">Shared:</span>
${repeat(this.sharedCommunities.slice(0, 7), (userId, i) => html`
<span data-tooltip=${userId}>
<img src=${AVATAR_URL(userId)} class="inline-block rounded-md w-7 h-7 mr-1">
</span>
`)}
${this.sharedCommunities.length > 7 ? html`<span class="font-semibold ml-1">+${this.sharedCommunities.length - 7}</span>` : ''}
</div>
` : ''}
</div>
${this.showExpanded ? html`
<div class="sm:mx-2 mb-1 sm:rounded px-1 py-1 bg-gray-100">
${repeat(this.memberships || [], (membership, i) => {
const userId = membership.value.community.userId
const [username, domain] = userId.split('@')
return html`
<div class="flex items-center px-2 py-2 bg-white rounded ${i !== 0 ? 'mt-1' : ''}">
<a class="ml-1 mr-3" href="/${userId}" title=${userId}>
<img class="block rounded-md w-10 h-10 object-cover shadow-sm" src=${AVATAR_URL(userId)}>
</a>
<div class="flex-1 min-w-0 truncate">
<a class="hov:hover:underline" href="/${userId}" title=${userId}>
<span class="font-medium">${displayNames.render(userId)}</span>
</a>
<span class="hidden sm:inline text-sm text-gray-500">${domain}</span>
</div>
</div>
`
})}
</div>
` : ''}
</div>
`
}
// events
// =
onToggleExpanded (e) {
this.isExpanded = !this.isExpanded
}
}
customElements.define('ctzn-community-memberships-list', CommunityMembershipsList)