aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Thread.js
blob: 52ca1f0f1ab70a5b26b2dcb1c7043118e6deffae (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
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 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() {
    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
    })
  }
  onReply = (msg) => {
    if (msg.mid == this.state.msg.mid) {
      this.setState({
        replies: [...this.state.replies, msg]
      })
    }
  }
  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;
    const loaders = Math.min(msg.replies || 0, 10);
    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>
        <ul id="replies">
          {
            !this.state.loading ? 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 || <span style={linkStyle} onClick={() => this.setActive(msg)}>Reply</span>}
                          {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>
            )) : (
                <React.Fragment>
                  {Array(loaders).fill().map((it, i) => <Spinner key={i} />)}
                </React.Fragment>
              )
          }
        </ul>
      </React.Fragment>
    )
  }
}

const linkStyle = {
  cursor: 'pointer'
}

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