File size: 5,281 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
// @flow
import * as React from 'react';
import compose from 'recompose/compose';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import { timeDifference } from 'shared/time-difference';
import { convertTimestampToDate } from 'shared/time-formatting';
import type { GetThreadType } from 'shared/graphql/queries/thread/getThread';
import ThreadRenderer from 'src/components/threadRenderer';
import ActionBar from './actionBar';
import { withCurrentUser } from 'src/components/withCurrentUser';
import { UserListItem } from 'src/components/entities';
import {
  ThreadWrapper,
  ThreadContent,
  ThreadHeading,
  ThreadSubtitle,
  BylineContainer,
} from '../style';
import getThreadLink from 'src/helpers/get-thread-link';
import type { Dispatch } from 'redux';
import { ErrorBoundary } from 'src/components/error';

type State = {
  body: ?string,
  title: string,
  flyoutOpen?: ?boolean,
  error?: ?string,
  parsedBody: ?Object,
};

type Props = {
  thread: GetThreadType,
  dispatch: Dispatch<Object>,
  currentUser: ?Object,
  ref?: any,
};

class ThreadDetailPure extends React.Component<Props, State> {
  state = {
    parsedBody: null,
    body: '',
    title: '',
    flyoutOpen: false,
    error: '',
  };

  bodyEditor: any;
  titleTextarea: React$Node;

  componentWillMount() {
    this.setThreadState();
  }

  setThreadState() {
    const { thread } = this.props;

    const parsedBody = JSON.parse(thread.content.body);

    return this.setState({
      body: '',
      title: thread.content.title,
      // We store this in the state to avoid having to JSON.parse on every render
      parsedBody,
      flyoutOpen: false,
    });
  }

  componentDidUpdate(prevProps) {
    if (
      prevProps.thread &&
      this.props.thread &&
      prevProps.thread.id !== this.props.thread.id
    ) {
      this.setThreadState();
    }
  }

  changeTitle = e => {
    const title = e.target.value;
    if (/\n$/g.test(title)) {
      this.bodyEditor.focus && this.bodyEditor.focus();
      return;
    }
    this.setState({
      title,
    });
  };

  changeBody = evt => {
    this.setState({
      body: evt.target.value,
    });
  };

  render() {
    const { currentUser, thread } = this.props;

    const createdAt = new Date(thread.createdAt).getTime();
    const timestamp = convertTimestampToDate(createdAt);
    const { author } = thread;

    const editedTimestamp = thread.modifiedAt
      ? new Date(thread.modifiedAt).getTime()
      : null;

    return (
      <ThreadWrapper ref={this.props.ref}>
        <ThreadContent>
          <BylineContainer>
            <UserListItem
              userObject={author.user}
              name={author.user.name}
              username={author.user.username}
              profilePhoto={author.user.profilePhoto}
              badges={author.roles}
              isCurrentUser={currentUser && author.user.id === currentUser.id}
              avatarSize={40}
              showHoverProfile={false}
              messageButton={currentUser && author.user.id !== currentUser.id}
            />
          </BylineContainer>

          {thread.community.website && thread.community.redirect && (
            <div
              style={{
                width: 'calc(100% + 32px)',
                borderBottom: '1px solid #f6f7f8',
                padding: '12px 16px',
                background: '#FFE6BF',
                marginLeft: '-16px',
                marginRight: '-16px',
                color: '#7D4A00',
              }}
            >
              The {thread.community.name} community has a new home. This thread
              is preserved for historical purposes. The content of this
              conversation may be innaccurrate or out of date.{' '}
              <a
                style={{ color: '#D85537', fontWeight: '600' }}
                href={thread.community.website}
              >
                Go to new community home &rarr;
              </a>
            </div>
          )}

          <div style={{ height: '16px' }} />

          <ThreadHeading>{thread.content.title}</ThreadHeading>

          <ThreadSubtitle>
            <Link to={getThreadLink(thread)}>
              {timestamp}
              {thread.modifiedAt && (
                <React.Fragment>
                  {' '}
                  (Edited{' '}
                  {timeDifference(Date.now(), editedTimestamp).toLowerCase()}
                  {thread.editedBy &&
                    thread.editedBy.user.id !== thread.author.user.id &&
                    ` by @${thread.editedBy.user.username}`}
                  )
                </React.Fragment>
              )}
            </Link>
          </ThreadSubtitle>

          <ThreadRenderer body={JSON.parse(thread.content.body)} />
        </ThreadContent>

        <ErrorBoundary>
          <ActionBar
            currentUser={currentUser}
            thread={thread}
            title={this.state.title}
          />
        </ErrorBoundary>
      </ThreadWrapper>
    );
  }
}

const ThreadDetail = compose(withRouter)(ThreadDetailPure);

const map = state => ({
  flyoutOpen: state.flyoutOpen,
});

export default compose(
  withCurrentUser,
  // $FlowIssue
  connect(map)
)(ThreadDetail);