File size: 7,508 Bytes
1e92f2d |
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 |
// @flow
import React from 'react';
import { withRouter } from 'react-router-dom';
import queryString from 'query-string';
import compose from 'recompose/compose';
import getThreadMessages, {
type GetThreadMessageConnectionType,
} from 'shared/graphql/queries/thread/getThreadMessageConnection';
import { sortAndGroupMessages } from 'shared/clients/group-messages';
import NextPageButton from 'src/components/nextPageButton';
import viewNetworkHandler, {
type ViewNetworkHandlerType,
} from 'src/components/viewNetworkHandler';
import ChatMessages from 'src/components/messageGroup';
import { Loading } from 'src/components/loading';
import NullMessages from './nullMessages';
import type { Location } from 'react-router';
import { NullMessagesWrapper } from '../style';
type Props = {
// Used by getThreadMessages query
isWatercooler: boolean,
data: {
loading: boolean,
thread: ?GetThreadMessageConnectionType,
},
loadPreviousPage: Function,
loadNextPage: Function,
onMessagesLoaded?: Function,
location: Location,
thread?: Object,
...$Exact<ViewNetworkHandlerType>,
};
class Messages extends React.Component<Props> {
componentDidMount() {
const thread = this.props.data.thread || this.props.thread;
// Scroll to bottom on mount if we got cached data as getSnapshotBeforeUpdate does not fire for mounts
if (thread && thread.watercooler) {
const elem = document.getElementById('main');
if (!elem) return;
elem.scrollTop = elem.scrollHeight;
}
}
getSnapshotBeforeUpdate(prev) {
const curr = this.props;
// First load
if (!prev.data.thread && curr.data.thread && curr.data.thread.watercooler) {
return {
type: 'bottom',
};
}
// New messages
if (
prev.data.thread &&
curr.data.thread &&
prev.data.thread.messageConnection.edges.length > 0 &&
curr.data.thread.messageConnection.edges.length > 0 &&
prev.data.thread.messageConnection.edges.length <
curr.data.thread.messageConnection.edges.length
) {
const elem = document.getElementById('main');
if (!elem || !curr.data.thread) return null;
// If new messages were added at the top, persist the scroll position
if (
prev.data.thread.messageConnection.edges[0].node.id !==
curr.data.thread.messageConnection.edges[0].node.id
) {
return {
type: 'persist',
values: {
top: elem.scrollTop,
height: elem.scrollHeight,
},
};
}
// If more than one new message was added at the bottom, stick to the current position
if (
prev.data.thread.messageConnection.edges.length + 1 <
curr.data.thread.messageConnection.edges.length
) {
return null;
}
// If only one message came in and we are near the bottom when new messages come in, stick to the bottom
if (elem.scrollHeight < elem.scrollTop + elem.clientHeight + 400) {
return {
type: 'bottom',
};
}
// Otherwise stick to the current position
return null;
}
return null;
}
componentDidUpdate(prevProps, __, snapshot) {
const { onMessagesLoaded } = this.props;
// after the messages load, pass it back to the thread container so that
// it can populate @ mention suggestions
const prevData = prevProps.data;
const currData = this.props.data;
const wasLoading = prevData && prevData.loading;
const hasPrevThread = prevData && prevData.thread;
const hasCurrThread = currData && currData.thread;
const previousMessageConnection =
// $FlowIssue
hasPrevThread && prevData.thread.messageConnection;
const currMessageConnection =
// $FlowIssue
hasCurrThread && currData.thread.messageConnection;
// thread loaded for the first time
if (!hasPrevThread && hasCurrThread && currMessageConnection) {
if (currMessageConnection.edges.length > 0) {
onMessagesLoaded && onMessagesLoaded(currData.thread);
}
}
// new messages arrived
if (previousMessageConnection && hasCurrThread && currMessageConnection) {
if (
currMessageConnection.edges.length >
previousMessageConnection.edges.length
) {
onMessagesLoaded && onMessagesLoaded(currData.thread);
}
// already loaded the thread, but was refetched
if (wasLoading && !currData.loading) {
onMessagesLoaded && onMessagesLoaded(currData.thread);
}
}
if (snapshot) {
const elem = document.getElementById('main');
if (!elem) return;
switch (snapshot.type) {
case 'bottom': {
elem.scrollTop = elem.scrollHeight;
return;
}
case 'persist': {
elem.scrollTop =
elem.scrollHeight - snapshot.values.height + snapshot.values.top;
return;
}
default: {
return;
}
}
}
}
render() {
const { data, isLoading, isFetchingMore, hasError } = this.props;
const { thread } = data;
if (thread && thread.messageConnection) {
const { messageConnection } = thread;
const { edges } = messageConnection;
if (edges.length === 0) return <NullMessages />;
const unsortedMessages = edges.map(message => message && message.node);
const sortedMessages = sortAndGroupMessages(unsortedMessages);
if (!sortedMessages || sortedMessages.length === 0)
return <NullMessages />;
return (
<React.Fragment>
{messageConnection.pageInfo.hasPreviousPage && (
<NextPageButton
isFetchingMore={isFetchingMore}
fetchMore={this.props.loadPreviousPage}
automatic={!!thread.watercooler}
href={{
pathname: this.props.location.pathname,
search: queryString.stringify({
...queryString.parse(this.props.location.search),
msgsbefore: messageConnection.edges[0].cursor,
msgsafter: undefined,
}),
}}
>
Show previous messages
</NextPageButton>
)}
<ChatMessages
thread={thread}
uniqueMessageCount={unsortedMessages.length}
messages={sortedMessages}
threadType={'story'}
isWatercooler={thread.watercooler}
/>
{messageConnection.pageInfo.hasNextPage && (
<NextPageButton
isFetchingMore={isFetchingMore}
fetchMore={this.props.loadNextPage}
href={{
pathname: this.props.location.pathname,
search: queryString.stringify({
...queryString.parse(this.props.location.search),
msgsafter:
messageConnection.edges[messageConnection.edges.length - 1]
.cursor,
msgsbefore: undefined,
}),
}}
>
Show more messages
</NextPageButton>
)}
</React.Fragment>
);
}
if (isLoading)
return (
<NullMessagesWrapper>
<Loading style={{ height: '80vh' }} />
</NullMessagesWrapper>
);
if (hasError) return null;
return null;
}
}
export default compose(
withRouter,
getThreadMessages,
viewNetworkHandler
)(Messages);
|