File size: 5,322 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
// @flow
import * as React from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import type { Dispatch } from 'redux';
import { withApollo } from 'react-apollo';
import { Link } from 'react-router-dom';
import Icon from 'src/components/icon';
import Messages from '../components/messages';
import Header from '../components/header';
import viewNetworkHandler from 'src/components/viewNetworkHandler';
import getDirectMessageThread, {
  type GetDirectMessageThreadType,
} from 'shared/graphql/queries/directMessageThread/getDirectMessageThread';
import { setTitlebarProps } from 'src/actions/titlebar';
import { UserAvatar } from 'src/components/avatar';
import { MessagesContainer, ViewContent } from '../style';
import { Loading } from 'src/components/loading';
import { ErrorBoundary } from 'src/components/error';
import type { WebsocketConnectionType } from 'src/reducers/connectionStatus';
import { useConnectionRestored } from 'src/hooks/useConnectionRestored';
import { withCurrentUser } from 'src/components/withCurrentUser';
import { LoadingView, ErrorView } from 'src/views/viewHelpers';
import { DesktopTitlebar } from 'src/components/titlebar';

type Props = {
  data: {
    refetch: Function,
    directMessageThread: GetDirectMessageThreadType,
  },
  isLoading: boolean,
  match: Object,
  id: ?string,
  currentUser: Object,
  threadSliderIsOpen: boolean,
  networkOnline: boolean,
  websocketConnection: WebsocketConnectionType,
  dispatch: Dispatch<Object>,
};

class ExistingThread extends React.Component<Props> {
  componentDidMount() {
    const { threadId } = this.props.match.params;

    // escape to prevent this from running on mobile
    if (!threadId) return;
  }

  componentDidUpdate(prev) {
    const curr = this.props;
    const { dispatch, currentUser } = curr;

    const didReconnect = useConnectionRestored({ curr, prev });
    if (didReconnect && curr.data.refetch) {
      curr.data.refetch();
    }

    if (curr.data.directMessageThread) {
      const thread = curr.data.directMessageThread;
      const trimmedUsers = thread.participants.filter(
        user => user.userId !== currentUser.id
      );
      const titleIcon =
        trimmedUsers.length === 1 ? (
          <UserAvatar user={trimmedUsers[0]} size={24} />
        ) : null;
      const rightAction =
        trimmedUsers.length === 1 ? (
          <Link to={`/users/${trimmedUsers[0].username}`}>
            <Icon glyph={'info'} />
          </Link>
        ) : null;
      const names = trimmedUsers.map(user => user.name).join(', ');
      dispatch(
        setTitlebarProps({
          title: names,
          titleIcon,
          rightAction,
          leftAction: 'view-back',
        })
      );
    }

    // if the thread slider is open, dont be focusing shit up in heyuhr
    if (curr.threadSliderIsOpen) return;
    if (prev.match.params.threadId !== curr.match.params.threadId) {
      const threadId = curr.match.params.threadId;

      // prevent unnecessary behavior on mobile
      if (!threadId) return;
    }
  }

  render() {
    const id = this.props.match.params.threadId;
    const { currentUser, data, isLoading } = this.props;

    if (id !== 'new') {
      if (data.directMessageThread) {
        const thread = data.directMessageThread;
        const trimmedUsers = thread.participants.filter(
          user => user.userId !== currentUser.id
        );
        const titleIcon =
          trimmedUsers.length === 1 ? (
            <UserAvatar user={trimmedUsers[0]} size={24} />
          ) : null;
        const rightAction =
          trimmedUsers.length === 1 ? (
            <Link to={`/users/${trimmedUsers[0].username}`}>
              <Icon glyph={'info'} />
            </Link>
          ) : null;
        const names = trimmedUsers.map(user => user.name).join(', ');
        return (
          <div style={{ display: 'flex', flexDirection: 'column' }}>
            <DesktopTitlebar
              title={names}
              titleIcon={titleIcon}
              rightAction={rightAction}
            />
            <MessagesContainer>
              <ViewContent>
                {!isLoading ? (
                  <React.Fragment>
                    <ErrorBoundary>
                      <Header thread={thread} currentUser={currentUser} />
                    </ErrorBoundary>

                    <Messages
                      id={id}
                      currentUser={currentUser}
                      thread={thread}
                    />
                  </React.Fragment>
                ) : (
                  <Loading />
                )}
              </ViewContent>
            </MessagesContainer>
          </div>
        );
      }

      if (isLoading) {
        return <LoadingView />;
      }

      return <ErrorView />;
    }

    /*
      if we are viewing /new we will handle the messages view in the composer
      component
    */
    return null;
  }
}

const map = state => ({
  networkOnline: state.connectionStatus.networkOnline,
  websocketConnection: state.connectionStatus.websocketConnection,
  threadSliderIsOpen: state.threadSlider.isOpen,
});
export default compose(
  // $FlowIssue
  connect(map),
  getDirectMessageThread,
  withApollo,
  withCurrentUser,
  viewNetworkHandler
)(ExistingThread);