File size: 5,722 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 |
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import VisibilitySensor from 'react-visibility-sensor';
import DirectMessageListItem from './messageThreadListItem';
import getCurrentUserDMThreadConnection, {
type GetCurrentUserDMThreadConnectionType,
} from 'shared/graphql/queries/directMessageThread/getCurrentUserDMThreadConnection';
import { deduplicateChildren } from 'src/components/infiniteScroll/deduplicateChildren';
import { LoadingDM } from 'src/components/loading';
import { ThreadsListScrollContainer } from './style';
import { ErrorBoundary } from 'src/components/error';
import { withCurrentUser } from 'src/components/withCurrentUser';
import { useConnectionRestored } from 'src/hooks/useConnectionRestored';
import type { WebsocketConnectionType } from 'src/reducers/connectionStatus';
import type { Query } from 'react-apollo';
import viewNetworkHandler, {
type ViewNetworkHandlerType,
} from 'src/components/viewNetworkHandler';
import { DesktopTitlebar } from 'src/components/titlebar';
import { NoCommunitySelected, NoCommunityHeading } from '../style';
type Props = {
currentUser: Object,
networkOnline: boolean,
websocketConnection: WebsocketConnectionType,
activeThreadId: ?string,
...$Exact<ViewNetworkHandlerType>,
dmData: {
...$Exact<Query>,
user: {
...$Exact<GetCurrentUserDMThreadConnectionType>,
},
},
};
class ThreadsList extends React.Component<Props> {
componentDidUpdate(prev: Props) {
const curr = this.props;
const didReconnect = useConnectionRestored({ curr, prev });
if (didReconnect && curr.dmData.refetch) {
curr.dmData.refetch();
}
}
shouldComponentUpdate(nextProps) {
const curr = this.props;
// fetching more
if (curr.dmData.networkStatus === 7 && nextProps.dmData.networkStatus === 3)
return false;
return true;
}
paginate = () => {
const { dmData, activeThreadId } = this.props;
// don't accidentally paginate the threadslist in the background on mobile
if (window && window.innerWidth < 768 && activeThreadId) return;
return dmData.fetchMore();
};
onLoadMoreVisible = (isVisible: boolean) => {
if (this.props.isFetchingMore || !isVisible) return;
return this.paginate();
};
render() {
const { currentUser, dmData, activeThreadId, isFetchingMore } = this.props;
if (!dmData) return null;
const dmDataExists =
currentUser && dmData.user && dmData.user.directMessageThreadsConnection;
const threads =
dmDataExists &&
dmData.user.directMessageThreadsConnection.edges &&
dmData.user.directMessageThreadsConnection.edges.length > 0
? dmData.user.directMessageThreadsConnection.edges
.map(thread => thread && thread.node)
.sort((a, b) => {
const x =
a &&
a.threadLastActive &&
new Date(a.threadLastActive).getTime();
const y =
b &&
b.threadLastActive &&
new Date(b.threadLastActive).getTime();
const val = parseInt(y, 10) - parseInt(x, 10);
return val;
})
: [];
const hasNextPage =
dmData.user &&
dmData.user.directMessageThreadsConnection &&
dmData.user.directMessageThreadsConnection.pageInfo &&
dmData.user.directMessageThreadsConnection.pageInfo.hasNextPage;
const uniqueThreads = deduplicateChildren(threads, 'id');
if (!dmDataExists && dmData.loading) {
return (
<ThreadsListScrollContainer>
<DesktopTitlebar title={'Messages'} />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
<LoadingDM />
</ThreadsListScrollContainer>
);
}
if (!uniqueThreads || uniqueThreads.length === 0) {
return (
<ThreadsListScrollContainer>
<DesktopTitlebar title={'Messages'} />
<NoCommunitySelected hideOnDesktop>
<div>
<NoCommunityHeading>No conversation selected</NoCommunityHeading>
</div>
</NoCommunitySelected>
</ThreadsListScrollContainer>
);
}
const LoadingDMWithVisibility = () => (
<VisibilitySensor
active={!isFetchingMore}
delayedCall
partialVisibility
scrollCheck
intervalDelay={250}
onChange={this.onLoadMoreVisible}
offset={{
bottom: -250,
}}
>
<LoadingDM key={0} />
</VisibilitySensor>
);
return (
<React.Fragment>
<DesktopTitlebar title={'Messages'} />
<ThreadsListScrollContainer>
{uniqueThreads.map(thread => {
if (!thread) return null;
return (
<ErrorBoundary key={thread.id}>
<DirectMessageListItem
thread={thread}
currentUser={currentUser}
active={activeThreadId === thread.id}
/>
</ErrorBoundary>
);
})}
{hasNextPage && <LoadingDMWithVisibility />}
</ThreadsListScrollContainer>
</React.Fragment>
);
}
}
const map = state => ({
networkOnline: state.connectionStatus.networkOnline,
websocketConnection: state.connectionStatus.websocketConnection,
});
export default compose(
withCurrentUser,
getCurrentUserDMThreadConnection,
viewNetworkHandler,
// $FlowIssue
connect(map)
)(ThreadsList);
|