This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspacesManager.js
428 lines (384 loc) · 15.4 KB
/
spacesManager.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import { h, Fragment, createRef, Component } from 'preact';
import * as Matrix from "matrix-js-sdk"
import Client from './client.js'
import './styles/spacesManager.css'
import Modal from './modal.js'
import { toastError } from "./utils/alerts.js"
import ManageMembership from './manageMembership.js'
import Resource from './utils/resource.js'
import RoomSettings from './roomSettings.js'
import SearchBar from './search.js'
import LeaveRoom from './leaveRoom.js'
import ArchiveRoom from './archiveRoom.js'
import AddCollection from './addCollection.js'
import RoomIcon from './roomIcon.js'
import ToolTip from './utils/tooltip.js'
import { RoomIconPlaceholder } from './roomIcon.js'
import * as Icons from './icons.js'
import { RoomColor } from './utils/colors.js'
import { populusCollectionChild } from "./constants.js"
export default class SpacesManager extends Component {
constructor(props) {
super(props)
SpacesManager.init()
this.state = {
spaces: Client.client.getVisibleRooms()
.filter(room => room.getMyMembership() === "join")
.filter(this.isActiveCollection)
}
}
handleRoom = _ => {
clearTimeout(this.roomDebounceTimeout)
this.roomDebounceTimeout = setTimeout(_ => {
this.setState({
spaces: Client.client.getVisibleRooms()
.filter(room => room.getMyMembership() === "join")
.filter(this.isActiveCollection)
})
})
}
static init() {
if (SpacesManager.initialized) return
SpacesManager.initialized = true
SpacesManager.spaces = {}
Client.client.on("RoomState.events", e => {
if (SpacesManager.spaces[e.getRoomId()] && e.getType() === Matrix.EventType.SpaceChild) {
if (e.getContent().via) {
const responsePromise = Client.client.getRoomHierarchy(e.getStateKey(), 1, 0)
responsePromise.then(response => {
const [child] = response.rooms
SpacesManager.spaces[e.getRoomId()].children[child.room_id] = child
SpacesManager.spaces[e.getRoomId()].via[e.getStateKey()] = e.getContent().via
}).then(_ => Client.client.emit("Space.update", e.getRoomId()))
} else {
delete SpacesManager.spaces[e.getRoomId()].children[e.getStateKey()]
Client.client.emit("Space.update", e.getRoomId())
}
}
})
}
componentDidMount () {
Client.client.on("Room", this.handleRoom)
Client.client.on("Room.name", this.handleRoom)
Client.client.on("Room.accountData", this.handleRoom)
}
componentWillUnmount () {
Client.client.off("Room", this.handleRoom)
Client.client.off("Room.name", this.handleRoom)
Client.client.off("Room.accountData", this.handleRoom)
}
filterToggle = s => {
const newItems = this.props.filterItems.filter(item => item.value !== s.value)
if (newItems.length === this.props.filterItems.length) newItems.push(s)
this.props.setFilterItems(newItems)
this.props.showMainView
? this.props.showMainView()
: null
}
isActiveCollection(room) {
const roomState = room.getLiveTimeline().getState(Matrix.EventTimeline.FORWARDS)
const creation = roomState.getStateEvents("m.room.create", "")
const isSpace = creation?.getContent()?.type === "m.space"
const isActive = !room.tags["m.lowpriority"]
return isSpace && isActive && !Resource.hasResource(room)
}
addCollection = _ => {
Modal.set(<AddCollection />, "Add New Collection")
}
render(props, state) {
return <div id="spaces-manager">
<h1>Collections</h1>
<hr class="styled-rule" />
<div id="spaces-list">
{state.spaces.map(room => <SpaceListing filterToggle={this.filterToggle} oneColumn={props.oneColumn} key={room.roomId} room={room} />)}
</div>
<div>
<button onclick={this.addCollection} id="add-space">Add Collection</button>
</div>
</div>
}
}
class SpaceListing extends Component {
constructor(props) {
super(props)
if (!SpacesManager.spaces[this.props.room.roomId]) {
SpacesManager.spaces[this.props.room.roomId] = {
via: {},
children: {},
nextBatch: null
}
}
this.initialChildCount = props.room.getLiveTimeline()
.getState(Matrix.EventTimeline.FORWARDS)
.getStateEvents("m.space.child")
.filter(childEvent => childEvent.getContent()?.via)
.length
this.state = {
actionsVisible: false,
// We use an array here to avoid duplicating children
children: SpacesManager.spaces[this.props.room.roomId].children,
limit: 30,
via: SpacesManager.spaces[this.props.room.roomId].via
}
}
componentDidMount() {
Client.client.on("Space.update", this.handleSpaceUpdate)
if (this.state.limit > Object.keys(this.state.children).length) this.loadChildren()
}
componentWillUnmount() {
Client.client.off("Space.update", this.handleSpaceUpdate)
}
handleSpaceUpdate = roomId => {
if (roomId === this.props.room.roomId) {
const children = SpacesManager.spaces[this.props.room.roomId].children
const via = SpacesManager.spaces[this.props.room.roomId].via
this.setState({ via, children }, this.refreshModal)
}
}
loadChildren = async _ => {
// dendrite will still use the fallback route, which can't restrict depth
let nextBatch = SpacesManager.spaces[this.props.room.roomId].nextBatch
const response = await Client.client.getRoomHierarchy(this.props.room.roomId, 30, 1, false, nextBatch)
const via = SpacesManager.spaces[this.props.room.roomId].via
for (const childState of response.rooms[0]?.children_state) {
via[childState.state_key] = childState.content.via
}
const children = SpacesManager.spaces[this.props.room.roomId].children
for (const child of response.rooms) {
if (child.room_id !== this.props.room.roomId) children[child.room_id] = child
}
nextBatch = response.next_batch
SpacesManager.spaces[this.props.room.roomId] = { via, children, nextBatch }
this.setState({ via, children }, this.refreshModal)
}
pageChildren = async _ => {
const nextBatch = SpacesManager.spaces[this.props.room.roomId].nextBatch
if (nextBatch) this.loadChildren()
}
addChildren = _ => {
const limit = this.state.limit + 15
if (limit > Object.keys(this.state.children).length) this.pageChildren()
this.setState({limit})
}
refreshModal = _ => Modal.getTitle() === "Manage Discussions"
? Modal.set(<AddChild
children={Object.values(this.state.children)}
nextBatch={this.state.nextBatch}
pageChildren={this.pageChildren}
room={this.props.room}
/>, "Manage Discussions", `to ${this.props.room.name}`)
: null
searchMe = _ => this.props.filterToggle({
display: <Fragment><span class="small-icon">{Icons.collection}</span>{this.props.room.name}</Fragment>,
value: `*${this.props.room.name}`
})
toggleActions = _ => this.setState(oldState => { return { actionsVisible: !oldState.actionsVisible } })
addChild = _ => {
this.setState({ actionsVisible: false })
Modal.set(<AddChild
// the root is always first in the listing
children={Object.values(this.state.children)}
nextBatch={this.state.nextBatch}
pageChildren={this.pageChildren}
room={this.props.room}
/>, "Manage Discussions", `in ${this.props.room.name}`)
}
joinChild = roomId => Client.client.joinRoom(roomId, { viaServers: this.state.via[roomId] })
.catch(toastError("Couldn't join this discussion"))
toggleChild = (roomId, name) => this.props.filterToggle({
value: roomId,
display: <Fragment><span class="small-icon">{Icons.page}</span>{name}</Fragment>
})
openSettings = _ => {
this.setState({ actionsVisible: false })
Modal.set(<RoomSettings joinLink={true} room={this.props.room} />, "Room Settings", `for ${this.props.room.name}`)
}
openMembership = _ => {
this.setState({ actionsVisible: false })
Modal.set(<ManageMembership room={this.props.room} />, "Manage Membership", `for ${this.props.room.name}`)
}
handleClose = _ => Modal.set(<LeaveRoom room={this.props.room} />, "Leave Room?", `for ${this.props.room.name}`)
archiveRoom = _ => Modal.set(<ArchiveRoom room={this.props.room} />, "Archive Collection?", `for ${this.props.room.name}`)
roomColor = new RoomColor(this.props.room.name)
render(props, state) {
const userMember = props.room.getMember(Client.client.getUserId())
const isAdmin = userMember.powerLevel >= 100
const canInvite = props.room.getLiveTimeline()
.getState(Matrix.EventTimeline.FORWARDS)
.hasSufficientPowerLevelFor("invite", userMember.powerLevel)
// should do this in a more fine-grained way with hasSufficientPowerLevelFor
return <div style={this.roomColor.styleVariables} class="space-listing">
<h3>
<span onclick={this.searchMe}>{props.room.name}</span>
<button data-narrow-view={props.oneColumn} onclick={this.toggleActions}>{Icons.moreVertical}</button>
</h3>
{ state.actionsVisible
? <div class="space-listing-actions">
{isAdmin
? <ToolTip content="Add new discussion">
<button class="small-icon" onclick={this.addChild}>{ Icons.newDiscussion }</button>
</ToolTip>
: null
}
{canInvite
? <ToolTip content="Manage membership">
<button class="small-icon" onclick={this.openMembership}>{ Icons.userPlus }</button>
</ToolTip>
: null
}
{isAdmin
? <ToolTip content="Configure settings">
<button class="small-icon" onclick={this.openSettings}>{ Icons.settings }</button>
</ToolTip>
: null
}
<ToolTip content="Hide and archive">
<button class="small-icon" onclick={this.archiveRoom}>{ Icons.archive }</button>
</ToolTip>
<ToolTip content="Leave collection">
<button class="small-icon" onclick={this.handleClose}>{ Icons.exit }</button>
</ToolTip>
</div>
: null
}
<div class="space-listing-children">
{Object.values(state.children).length > 0
// the root is always first in the listing
? Object.values(state.children).slice(0, state.limit).map(child => <RoomIcon
key={child.room_id}
size={50}
inactiveClick={this.joinChild}
activeClick={this.toggleChild}
roomId={child.room_id}
avatarUrl={child.avatar_url}
numJoinedMembers={child.num_joined_members}
joinRule={child.join_rule}
topic={child.topic}
name={child.name || child?.canonical_alias?.slice(1) || "?"}
/>)
: Array(Math.min(state.limit, this.initialChildCount)).fill().map((_, idx) => <RoomIconPlaceholder
key={idx}
size={50}
/>)
}
{(state.nextBatch || state.limit < Object.values(state.children).length)
? <div class="space-listing-more" onclick={this.addChildren}>...</div>
: null
}
</div>
</div>
}
}
class AddChild extends Component {
constructor(props) {
super(props)
props.children.map(child => child.name)
this.state = {
search: "",
adding: true,
discussions: Client.client
.getVisibleRooms()
.filter(room => Resource.hasResource(room))
}
}
componentDidMount () {
this.updateHeight()
}
componentDidUpdate () {
this.updateHeight()
}
currentList = createRef()
currentListWrapper = createRef()
handleScroll = _ => {
clearTimeout(this.debounceTimeout)
this.debounceTimeout = setTimeout(_ => {
const list = this.currentList.current
if (list.scrollTop + list.clientHeight + 10 >= list.scrollHeight) {
this.props.pageChildren()
}
}, 500)
}
updateHeight = _ => this.currentListWrapper.current.style.height = `${this.currentList.current.scrollHeight}px`
addDiscussions = _ => this.setState({adding: true})
removeDiscussions = _ => this.setState({adding: false})
filterDiscussions = search => {
this.setState({
search,
discussions: Client.client
.getVisibleRooms()
.filter(room =>
Resource.hasResource(room) &&
room.name.toLowerCase().includes(search.toLowerCase())
)
})
}
render(props, state) {
const childIds = this.props.children.map(child => child.room_id)
const availableDiscussions = state.adding && state.discussions.filter(room => !childIds.includes(room.roomId))
const currentDiscussions = !state.adding && props.children.filter(child => child.name.toLowerCase().includes(state.search.toLowerCase()))
return <Fragment>
<SearchBar search={state.search} setSearch={this.filterDiscussions} />
<div id="manage-discussion-select-view" class="select-view">
<button onClick={this.addDiscussions} data-current-button={state.adding}>Add Discussions</button>
<button onClick={this.removeDiscussions} data-current-button={!state.adding}>Remove Discussions</button>
</div>
<div id="manage-discussion-list-wrapper" ref={this.currentListWrapper}>
{state.adding
? <div ref={this.currentList} id="available-discussions-list">
{ availableDiscussions.map(room => <AvailableDiscussionListing key={room.roomId} room={room} collection={props.room} />) }
</div>
: <div onscroll={this.handleScroll} ref={this.currentList} id="current-discussions-list">
{ currentDiscussions.map(child => <CurrentDiscussionListing key={child.room_id} child={child} collection={props.room} />) }
</div>
}
</div>
</Fragment>
}
}
class CurrentDiscussionListing extends Component {
removeMe = async _ => {
this.setState({pending: true})
await Client.client
.sendStateEvent(this.props.collection.roomId, Matrix.EventType.SpaceChild, {}, this.props.child.room_id)
.catch(toastError("Couldn't remove discussion from collection"))
await Client.client
.sendStateEvent(this.props.child.room_id, Matrix.EventType.SpaceParent, {}, this.props.collection.roomId)
.catch(toastError("Couldn't remove collection as parent of discussion"))
}
render(props, state) {
return <button
data-change-pending={state.pending}
class="discussion-listing"
onclick={this.removeMe}>
<span>{Icons.trash}</span>
<span>{props.child.name}</span>
</button>
}
}
class AvailableDiscussionListing extends Component {
addMe = async _ => {
this.setState({pending: true})
const theDomain = Client.client.getDomain()
const childContent = {
via: [theDomain],
[populusCollectionChild]: true
}
const parentContent = { via: [theDomain] }
await Client.client
.sendStateEvent(this.props.collection.roomId, Matrix.EventType.SpaceChild, childContent, this.props.room.roomId)
.catch(toastError("Couldn't add discussion to collection"))
await Client.client
.sendStateEvent(this.props.room.roomId, Matrix.EventType.SpaceParent, parentContent, this.props.collection.roomId)
.catch(toastError("Couldn't add collection as parent of discussion"))
}
render(props, state) {
return <button
aria-label={`add ${props.room.name} to discussion`}
data-change-pending={state.pending}
class="discussion-listing"
onclick={this.addMe}>
<span>{Icons.newDiscussion}</span>
<span>{props.room.name}</span>
</button>
}
}