Skip to content

Commit bf407da

Browse files
author
vikasrohit
authored
Merge pull request #525 from appirio-tech/feature/scroll-to-specific-discussion
Github issue #517 Discourse email to topic link: scroll users to the correct topic on Discussions page
2 parents 09ad5e0 + 76d5d26 commit bf407da

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/components/MessageList/MessageList.jsx

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {PropTypes} from 'react'
1+
import React, {Component, PropTypes} from 'react'
22
import Panel from '../Panel/Panel'
33
import './MessageList.scss'
44
import cn from 'classnames'
@@ -49,18 +49,40 @@ const MessageRow = ({title, messages, isActive, unreadCount, onClick}) => {
4949
}
5050

5151

52-
const MessageList = ({threads, onSelect, onAdd, showAddButton, showEmptyState}) => (
53-
<Panel className="message-list">
54-
<Panel.Title>
55-
Discussions
56-
{ showAddButton && <Panel.AddBtn onClick={onAdd} /> }
57-
</Panel.Title>
58-
<div className="panel-messages">
59-
{ showEmptyState && <MessageRow title="First discussion post" isActive messages={[{ content: ''}]} /> }
60-
{threads.map((item) => <MessageRow key={item.id} onClick={(e) => onSelect(item, e) } {...item} />)}
61-
</div>
62-
</Panel>
63-
)
52+
class MessageList extends Component {
53+
constructor(props) {
54+
super(props)
55+
}
56+
57+
componentDidMount() {
58+
const { scrollPosition } = this.props
59+
if (scrollPosition) {
60+
const panelMessages = this.refs.panelMessages
61+
// We use requestAnimationFrame because this function may be executed before
62+
// the DOM elements are actually drawn.
63+
// Source: http://stackoverflow.com/a/28748160
64+
requestAnimationFrame(() => {
65+
panelMessages.scrollTop += scrollPosition
66+
})
67+
}
68+
}
69+
70+
render() {
71+
const {threads, onSelect, onAdd, showAddButton, showEmptyState } = this.props
72+
return (
73+
<Panel className="message-list">
74+
<Panel.Title>
75+
Discussions
76+
{ showAddButton && <Panel.AddBtn onClick={onAdd} /> }
77+
</Panel.Title>
78+
<div className="panel-messages" ref="panelMessages">
79+
{ showEmptyState && <MessageRow title="First discussion post" isActive messages={[{ content: ''}]} /> }
80+
{threads.map((item) => <MessageRow key={item.id} onClick={(e) => onSelect(item, e) } {...item} />)}
81+
</div>
82+
</Panel>
83+
)
84+
}
85+
}
6486

6587
MessageList.propTypes = {
6688
/**

src/projects/detail/containers/MessagesContainer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ class MessagesView extends React.Component {
9292

9393
init(props) {
9494
const { activeThreadId } = this.state
95-
const propsThreadId = props.location.state ? props.location.state.threadId : null
95+
const propsThreadId = _.get(props, 'location.state.threadId', null)
9696
const threadId = activeThreadId ? activeThreadId : propsThreadId
9797
const activeThreadIndex = threadId
9898
? _.findIndex(props.threads, (thread) => thread.id === threadId )
9999
: 0
100+
100101
this.setState({
102+
scrollPosition: activeThreadIndex * 71,
101103
threads: props.threads.map((thread, idx) => {
102104
return this.mapFeed(thread,
103105
idx === activeThreadIndex,
@@ -189,7 +191,7 @@ class MessagesView extends React.Component {
189191
}
190192

191193
render() {
192-
const {threads, isCreateNewMessage, showEmptyState} = this.state
194+
const {threads, isCreateNewMessage, showEmptyState, scrollPosition} = this.state
193195
const { currentUser, isCreatingFeed, currentMemberRole, error } = this.props
194196
const activeThread = threads.filter((item) => item.isActive)[0]
195197
const renderRightPanel = () => {
@@ -230,6 +232,7 @@ class MessagesView extends React.Component {
230232
onSelect={this.onThreadSelect}
231233
showAddButton={ !!currentMemberRole }
232234
showEmptyState={ showEmptyState && !threads.length }
235+
scrollPosition={ scrollPosition }
233236
/>
234237
</div>
235238
<div className="right-area">

0 commit comments

Comments
 (0)