aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Thread.js
blob: 7464ca7f5a375fc4f29474f980054df405f2b6bf (plain) (blame)
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
import React from 'react';
import { Link } from 'react-router-dom';
import * as qs from 'query-string';
import moment from 'moment';

import Message from './Message';
import MessageInput from './MessageInput';
import Spinner from './Spinner';
import Avatar from './Avatar';
import Icon from './Icon';

import { format } from '../utils/embed';

export default class Thread extends React.Component {
  constructor(props) {
    super(props);
    const { msg } = (this.props.location.state || {});
    this.state = {
      msg: msg || {},
      replies: [],
      active: 0
    };
    this.loaded = this.loaded.bind(this);
    this.postComment = this.postComment.bind(this);
  }
  componentDidMount() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    this.loadReplies();
  }
  loadReplies() {
    this.setState({ replies: [] })
    const { mid } = this.props.match.params;
    let params = {
      mid: mid
    }
    if (this.props.visitor && this.props.visitor.hash) {
      params.hash = this.props.visitor.hash
    };
    const url = `https://api.juick.com/thread?${qs.stringify(params)}`;
    fetch(url)
      .then(response => {
        return response.json()
      })
      .then(data => {
        let msg = data.shift();
        this.setState({
          msg: msg,
          replies: data,
          active: 0
        })
      }
      ).catch(ex => {
        console.log(ex);
      });
  }
  loaded() {
    return (this.state.replies && this.state.replies.length > 0) || ('mid' in this.state.msg && !('replies' in this.state.msg));
  }
  setActive(msg, event) {
    this.setState({
      active: msg.rid || 0
    })
  }
  postComment(template) {
    const url = `https://api.juick.com/comment?hash=${this.props.visitor.hash}`;
    let form = new FormData();
    form.append('mid', template.mid);
    form.append('rid', template.rid);
    form.append('body', template.body);
    form.append('attach', template.attach);
    fetch(url, {
      method: 'POST',
      body: form
    }).then(response => {
      return response.json()
    }).then(res => {
      this.loadReplies()
    })
      .catch(console.log)
  }

  render() {
    const msg = this.state.msg;
    return (
      <React.Fragment>
        <ul id="0">
          <li className="msg msgthread">
            {
              msg.mid ? (
                <Message data={msg} visitor={this.props.visitor}>
                  {this.state.active === (msg.rid || 0) && <MessageInput data={msg} onSend={this.postComment} />}
                  <Recommendations src={msg.recommendations} />
                </Message>
              ) : (
                  <Spinner />
                )
            }
          </li>
        </ul>
        {
          <React.Fragment>
            {
              this.props.visitor.uid > 0 && msg.mid &&
              <div className="title2">
                <img style={{ display: 'none' }} src={`https://api.juick.com/thread/mark_read/${msg.mid}-${msg.rid || 0}.gif?hash=${this.props.visitor.hash}`} />
                <h2>{msg.replies && `Replies (${msg.replies})`}</h2>
              </div>
            }
            {this.loaded() ? (
              <ul id="replies">
                {
                  this.state.replies.map((msg) => (
                    <li id={msg.rid} key={msg.rid} className="msg">
                      <div className="msg-cont">
                        <div className="msg-header">
                          {!msg.user.banned ? (
                            <React.Fragment>
                              <span itemProp="author" itemScope="" itemType="http://schema.org/Person">
                                <Link to={`/${msg.user.uname}/`} itemProp="url" rel="author"><span itemProp="name">{msg.user.uname}</span></Link>
                              </span><Avatar user={msg.user} />
                            </React.Fragment>) : (
                              <React.Fragment>
                                <span>[удалено]:</span><Avatar user={{ uid: 0 }} />
                              </React.Fragment>
                            )
                          }
                          <div className="msg-ts">
                            <a href={`/${msg.user.uname}/${msg.mid}`}>
                              <time itemProp="datePublished dateModified" itemType="http://schema.org/Date" dateTime={msg.timestamp}
                                title={moment.utc(msg.timestamp).local().format('lll')}>
                                {moment.utc(msg.timestamp).fromNow()}
                              </time>
                            </a>
                            {msg.replyto > 0 &&
                              (
                                <a href={`#${msg.replyto}`}> in reply to {msg.to.uname}&nbsp;</a>
                              )}
                          </div>
                        </div>
                        <div className="msg-txt"><p dangerouslySetInnerHTML={{ __html: format(msg.body, msg.mid, (msg.tags || []).indexOf('code') >= 0) }}></p></div>
                        {
                          msg.photo &&
                          <p className="ir"><a href={`//i.juick.com/p/${msg.mid}-${msg.rid}.${msg.attach}`} data-fname={`${msg.mid}-${msg.rid}.${msg.attach}`}>
                            <img itemProp="image" src={`//i.juick.com/p/${msg.mid}-${msg.rid}.${msg.attach}`} alt="" /></a>
                          </p>
                        }
                        <div className="msg-links">
                          {
                            this.props.visitor.uid > 0 ? (
                              <React.Fragment>
                                {this.state.active === msg.rid || <button onClick={this.setActive.bind(this, msg)} className="badge"><Icon name="ei-envelope" size="s" />Reply</button>}
                                {this.state.active === msg.rid && <MessageInput data={msg} onSend={this.postComment} />}
                              </React.Fragment>
                            ) : (
                                <React.Fragment>
                                  <span>&nbsp;&middot;&nbsp;</span>{this.state.active === msg.rid || <button className="a-login">Reply</button>}
                                </React.Fragment>
                              )
                          }
                        </div>
                      </div>
                    </li>
                  ))
                }
              </ul>) : (
                <React.Fragment>
                  <Spinner /><Spinner /><Spinner />
                </React.Fragment>
              )}
          </React.Fragment>
        }
      </React.Fragment>
    )
  }
}

function Recommendations(props) {
  const recomms = props.src;
  return recomms && (
    <div className="msg-recomms">{`Recommended by (${recomms.length}): `}
      {
        recomms.map(it => (
          <Link key={it} to={`/${it}/`}>{it}</Link>
        )).reduce((prev, curr) => [prev, ', ', curr])
      }
    </div>
  ) || null;
}