File size: 4,624 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
// @flow
import React, { useEffect, useState } from 'react';
import compose from 'recompose/compose';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { withApollo } from 'react-apollo';
import { getThreadByMatch } from 'shared/graphql/queries/thread/getThread';
import { withCurrentUser } from 'src/components/withCurrentUser';
import viewNetworkHandler, {
  type ViewNetworkHandlerType,
} from 'src/components/viewNetworkHandler';
import { LoadingView, ErrorView } from 'src/views/viewHelpers';
import {
  ViewGrid,
  SecondaryPrimaryColumnGrid,
  PrimaryColumn,
  SecondaryColumn,
  SingleColumnGrid,
} from 'src/components/layout';
import { setTitlebarProps } from 'src/actions/titlebar';
import MessagesSubscriber from '../components/messagesSubscriber';
import StickyHeader from '../components/stickyHeader';
import ThreadDetail from '../components/threadDetail';
import ThreadHead from '../components/threadHead';
import { Stretch } from '../style';
import { deduplicateChildren } from 'src/components/infiniteScroll/deduplicateChildren';
import type { GetThreadType } from 'shared/graphql/queries/thread/getThread';
import CommunitySidebar from 'src/components/communitySidebar';
import { ErrorBoundary } from 'src/components/error';

type Props = {
  ...$Exact<ViewNetworkHandlerType>,
  data: {
    thread: GetThreadType,
  },
  client: Object,
  className?: string,
  currentUser?: Object,
  dispatch: Function,
  isModal: boolean,
  children: React$Node,
};

const ThreadContainer = (props: Props) => {
  const {
    data,
    isLoading,
    children,
    dispatch,
    className,
    isModal = false,
  } = props;

  if (isLoading) return <LoadingView />;

  const { thread } = data;
  if (!thread) return <ErrorView data-cy="null-thread-view" />;

  /*
  update the last seen timestamp of the current thread whenever it first
  loads, as well as when it unmounts as the user closes the thread. This
  should provide the effect of locally marking the thread as "seen" while
  athena handles storing the actual lastSeen timestamp update in the background
  asynchronously.
  */

  const [, setMentionSuggestions] = useState([thread.author.user]);
  const updateMentionSuggestions = (thread: GetThreadType) => {
    const { messageConnection, author } = thread;

    if (!messageConnection || messageConnection.edges.length === 0)
      return setMentionSuggestions([author.user]);

    const participants = messageConnection.edges
      .map(edge => edge.node)
      .map(node => node.author.user);

    const participantsWithAuthor = [...participants, author.user];
    const filtered = deduplicateChildren(participantsWithAuthor, 'id');
    return setMentionSuggestions(filtered);
  };

  useEffect(() => {
    dispatch(
      setTitlebarProps({
        title: 'Conversation',
        leftAction: 'view-back',
      })
    );
  }, []);

  const renderPrimaryColumn = fullWidth => (
    <PrimaryColumn fullWidth={fullWidth}>
      {/*
        This <Stretch> container makes sure that the thread detail and messages
        component are always at least the height of the screen, minus the
        height of the chat input. This is necessary because we always want
        the chat input at the bottom of the view, so it must always be tricked
        into thinking that its preceding sibling is full-height.
      */}
      <Stretch
        isModal={isModal}
        data-cy={isModal ? 'thread-is-modal' : undefined}
      >
        <ErrorBoundary>
          <StickyHeader thread={thread} />
        </ErrorBoundary>

        <ThreadDetail thread={thread} />

        <MessagesSubscriber
          id={thread.id}
          thread={thread}
          isWatercooler={thread.watercooler} // used in the graphql query to always fetch the latest messages
          onMessagesLoaded={updateMentionSuggestions}
        />
      </Stretch>
    </PrimaryColumn>
  );

  return (
    <React.Fragment>
      <ThreadHead thread={thread} />
      <ViewGrid className={className} data-cy="thread-view">
        {children}
        {isModal ? (
          <SingleColumnGrid>{renderPrimaryColumn(true)}</SingleColumnGrid>
        ) : (
          <SecondaryPrimaryColumnGrid>
            <SecondaryColumn>
              <CommunitySidebar community={thread.community} />
            </SecondaryColumn>
            {renderPrimaryColumn(false)}
          </SecondaryPrimaryColumnGrid>
        )}
      </ViewGrid>
    </React.Fragment>
  );
};

export default compose(
  getThreadByMatch,
  viewNetworkHandler,
  withRouter,
  withApollo,
  withCurrentUser,
  connect()
)(ThreadContainer);