aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Thread.js
blob: d25ca228662b6d7e1aec699ca94ebe4d4d27e132 (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
import React from 'react';
import { Link } from 'react-router-dom';
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 Button from './Button';

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

import { getMessages, comment, markReadTracker } from '../api';

export default class Thread extends React.Component {
  constructor(props) {
    super(props);
    const { msg } = (this.props.location.state || {});
    this.state = {
      msg: msg || {},
      replies: [],
      loading: false,
      active: 0
    };
  }
  componentDidMount() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    this.loadReplies();
  }
  loadReplies() {
    this.setState({ replies: [], loading: true })
    const { mid } = this.props.match.params;
    let params = {
      mid: mid
    }
    if (this.props.visitor && this.props.visitor.hash) {
      params.hash = this.props.visitor.hash
    };
    getMessages('/thread', params)
      .then(response => {
        let msg = response.data.shift();
        this.setState({
          msg: msg,
          replies: response.data,
          loading: false,
          active: 0
        })
      }
      ).catch(ex => {
        console.log(ex);
      });
  }
  setActive(msg, event) {
    this.setState({
      active: msg.rid || 0
    })
  }
  postComment = (template) => {
    const { mid, rid, body, attach } = template;
    comment(mid, rid, body, attach).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}>Write a comment...</MessageInput>}
                  <Recommendations src={msg.recommendations} />
                </Message>
              ) : (
                  <Spinner />
                )
            }
          </li>
        </ul>
        {!this.state.loading ? (
          <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(msg)}><Icon name="ei-envelope" size="s" />Reply</Button>}
                            {this.state.active === msg.rid && <MessageInput data={msg} onSend={this.postComment}>Write a comment...</MessageInput>}
                          </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>
    )
  }
}

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