You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.1 KiB
JavaScript
118 lines
3.1 KiB
JavaScript
6 years ago
|
'use strict'
|
||
|
const React = require('react')
|
||
|
const ReactDOM = require('react-dom')
|
||
|
const create = require('create-react-class')
|
||
|
const Promise = require('bluebird')
|
||
|
const debounce = require('debounce')
|
||
6 years ago
|
const jdenticon = require('jdenticon')
|
||
6 years ago
|
|
||
6 years ago
|
const FilterList = require('./filterList.js')
|
||
6 years ago
|
|
||
|
let RoomListItem = create({
|
||
|
displayName: "RoomListItem",
|
||
|
|
||
|
getInitialState: function() {
|
||
6 years ago
|
let room = this.props.content
|
||
|
let client = this.props.properties.client
|
||
5 years ago
|
let jdenticon = <svg id="avatar" ref={this.jdenticonRef}/>
|
||
|
let avatarUrl
|
||
6 years ago
|
|
||
|
let roomState = room.getLiveTimeline().getState('f')
|
||
|
let avatarState = roomState.getStateEvents('m.room.avatar')
|
||
|
if (avatarState.length > 0) {
|
||
|
let event = avatarState[avatarState.length-1].event
|
||
|
let hs = client.baseUrl
|
||
|
let media_mxc = event.content.url.slice(6)
|
||
5 years ago
|
let path = `/_matrix/media/v1/thumbnail/${media_mxc}?width=128&height=128&method=scale`
|
||
|
avatarUrl = {
|
||
|
hs: hs,
|
||
|
path: path
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
return {
|
||
6 years ago
|
filterName: room.name.toUpperCase(),
|
||
|
unread: Math.random() > 0.7,
|
||
5 years ago
|
avatarUrl: avatarUrl,
|
||
|
jdenticon: jdenticon,
|
||
|
tries: 0
|
||
6 years ago
|
}
|
||
|
},
|
||
|
|
||
6 years ago
|
jdenticonRef: function(ref) {
|
||
6 years ago
|
jdenticon.update(ref, this.props.content.roomId)
|
||
6 years ago
|
},
|
||
|
|
||
5 years ago
|
avatarFallback: function() {
|
||
|
// instead of falling back on jdenticon immediately, we can try
|
||
|
// a third-party homeserver's media repo
|
||
|
// this does come with trust issues, and is opt-in in settings
|
||
|
let fallbackMediaRepos = this.props.properties.options.fallbackMediaRepos
|
||
|
|
||
|
if (this.state.tries < fallbackMediaRepos.length) {
|
||
|
let avatarUrl = this.state.avatarUrl
|
||
|
avatarUrl.hs = fallbackMediaRepos[this.state.tries]
|
||
|
this.setState({
|
||
|
avatarUrl: avatarUrl,
|
||
|
tries: this.state.tries + 1
|
||
|
})
|
||
|
} else {
|
||
|
this.setState({avatarUrl: null, avatar: jdenticon})
|
||
|
}
|
||
|
},
|
||
|
|
||
6 years ago
|
setRef: function(ref) {
|
||
|
if (ref == null) {
|
||
|
return
|
||
|
}
|
||
|
this.setState({ref: ref})
|
||
6 years ago
|
ref.addEventListener("click", () => {this.props.select(this.props.listId)})
|
||
6 years ago
|
},
|
||
|
|
||
6 years ago
|
render: function() {
|
||
|
if (this.state.filterName.indexOf(this.props.filter) == -1) {
|
||
|
return null
|
||
|
}
|
||
6 years ago
|
let className = "roomListItem"
|
||
6 years ago
|
if (this.props.selected) {
|
||
6 years ago
|
className += " active"
|
||
6 years ago
|
}
|
||
6 years ago
|
if (this.state.unread) {
|
||
|
className += " unread"
|
||
|
}
|
||
6 years ago
|
return <div className={className} ref={this.setRef}>
|
||
5 years ago
|
{this.state.avatarUrl ?
|
||
|
<img id="avatar" src={`${this.state.avatarUrl.hs}${this.state.avatarUrl.path}`} onError={this.avatarFallback}></img>
|
||
|
:
|
||
|
this.state.jdenticon
|
||
|
}
|
||
6 years ago
|
<span id="name">{this.props.content.name}</span>
|
||
6 years ago
|
</div>
|
||
|
}
|
||
|
})
|
||
|
|
||
|
let Sidebar = create({
|
||
|
displayName: "Sidebar",
|
||
|
|
||
|
getInitialState: function() {
|
||
|
return {
|
||
|
filter: ""
|
||
|
}
|
||
|
},
|
||
|
|
||
|
setFilter: function(filter) {
|
||
|
this.setState({
|
||
|
filter: filter.toUpperCase()
|
||
|
})
|
||
|
},
|
||
|
|
||
|
render: function() {
|
||
|
return <div className="sidebar">
|
||
5 years ago
|
<FilterList items={this.props.rooms} properties={{client: this.props.client, options: this.props.options}} element={RoomListItem} callback={(roomId) => {this.props.selectRoom(roomId)}}/>
|
||
6 years ago
|
</div>
|
||
|
}
|
||
|
})
|
||
|
|
||
|
|
||
|
module.exports = Sidebar
|