-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathindex.tsx
160 lines (140 loc) · 4.18 KB
/
index.tsx
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
// Copyright 2017 Brandon Mowat
// Written, developed, and designed by Brandon Mowat for the purpose of helping
// other developers make chat interfaces.
import * as React from 'react';
import BubbleGroup from '../BubbleGroup';
import DefaultChatBubble from '../ChatBubble';
import ChatInput from '../ChatInput';
import Message from '../Message';
import styles from './styles';
// Model for ChatFeed props.
interface ChatFeedInterface {
props: {
bubblesCentered?: boolean;
bubbleStyles?: object;
hasInputField?: boolean;
isTyping?: boolean;
maxHeight?: number;
messages: any;
showSenderName?: boolean;
chatBubble?: React.Component;
preventConflictingAutoScroll?: boolean;
};
}
// React component to render a complete chat feed
export default class ChatFeed extends React.Component<ChatFeedInterface> {
static defaultProps = {
preventConflictingAutoScroll: true,
};
// If the user scrolls this close to the bottom of the feed, we will re-enable autoscroll
static MANUAL_SCROLL_BOTTOM_MARGIN = 20;
props;
chat: {
scrollHeight: number;
clientHeight: number;
scrollTop: number;
addEventListener: Function;
removeEventListener: Function;
};
_hasUserScrolledUp: boolean = false;
constructor(props: ChatFeedInterface) {
super(props);
this.handleScrollEvent = this.handleScrollEvent.bind(this);
}
componentDidMount() {
this.scrollToBottom();
this.chat.addEventListener('scroll', this.handleScrollEvent);
}
componentDidUpdate() {
const {preventConflictingAutoScroll} = this.props;
if (preventConflictingAutoScroll && this._hasUserScrolledUp) {
return;
}
this.scrollToBottom();
}
private getMaxScrollTop(): number {
if (!this.chat) return 0;
const scrollHeight = this.chat.scrollHeight;
const height = this.chat.clientHeight;
return scrollHeight - height;
}
scrollToBottom() {
const maxScrollTop = this.getMaxScrollTop();
this.chat.scrollTop = maxScrollTop > 0 ? maxScrollTop : 0;
}
componentWillUnmount(): void {
this.chat.removeEventListener('scroll', this.handleScrollEvent);
}
private handleScrollEvent(event: Event) {
if (!this.chat) return;
const maxScrollTop = this.getMaxScrollTop();
if (this.chat.scrollTop < maxScrollTop - ChatFeed.MANUAL_SCROLL_BOTTOM_MARGIN) {
this._hasUserScrolledUp = true;
} else {
this._hasUserScrolledUp = false;
}
}
/**
* Determines what type of message/messages to render.
*/
renderMessages(messages: [Message]) {
const { isTyping, bubbleStyles, chatBubble, showSenderName } = this.props;
const ChatBubble = chatBubble || DefaultChatBubble;
let group = [];
const messageNodes = messages.map((message, index) => {
group.push(message);
// Find diff in message type or no more messages
if (index === messages.length - 1 || messages[index + 1].id !== message.id) {
const messageGroup = group;
group = [];
return (
<BubbleGroup
key={index}
messages={messageGroup}
id={message.id}
showSenderName={showSenderName}
chatBubble={ChatBubble}
bubbleStyles={bubbleStyles}
/>
);
}
return null;
});
// Other end is typing...
if (isTyping) {
messageNodes.push(
<div key="isTyping" style={{ ...styles.chatbubbleWrapper }}>
<ChatBubble
message={new Message({ id: 1, message: '...', senderName: '' })}
bubbleStyles={bubbleStyles}
/>
</div>
);
}
// return nodes
return messageNodes;
}
/**
* render : renders our chatfeed
*/
render() {
const inputField = this.props.hasInputField && <ChatInput />;
const { maxHeight } = this.props;
return (
<div id="chat-panel" style={styles.chatPanel}>
<div
ref={c => {
this.chat = c;
}}
className="chat-history"
style={{ ...styles.chatHistory, maxHeight }}
>
<div className="chat-messages">
{this.renderMessages(this.props.messages)}
</div>
</div>
{inputField}
</div>
);
}
}