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
|
import { memo, useEffect, useRef } from 'react';
import { Link } from 'react-router-dom';
import moment from 'moment';
import Icon from './Icon';
import Avatar from './Avatar';
import { UserLink } from './UserInfo';
import { format, embedUrls } from '../utils/embed';
import './Message.css';
/**
* Message component
* @param {{data: import('../api').Message, visitor: import('../api').User, children: React.ReactElement}} props
*/
export default function Message({ data, visitor, children }) {
const isCode = (data.tags || []).indexOf('code') >= 0;
const likesSummary = data.likes ? `${data.likes}` : 'Recommend';
const commentsSummary = data.replies ? `${data.replies}` : 'Comment';
/**
* @type {React.RefObject<HTMLDivElement>}
*/
const embedRef = useRef();
/**
* @type {React.RefObject<HTMLDivElement>}
*/
const msgRef = useRef();
useEffect(() => {
if (msgRef.current) {
embedUrls(msgRef.current.querySelectorAll('a'), embedRef.current);
if (!embedRef.current.hasChildNodes()) {
embedRef.current.style.display = 'none';
}
}
}, []);
const canComment = visitor.uid === data.user.uid || !data.ReadOnly;
return (
<div className="msg-cont">
<Recommendations forMessage={data} />
<header className="h">
<Avatar user={data.user}>
<div className="msg-ts">
<Link to={{ pathname: `/${data.user.uname}/${data.mid}`, state: { msg: data } }}>
<time dateTime={data.timestamp}
title={moment.utc(data.timestamp).local().format('lll')}>
{moment.utc(data.timestamp).fromNow()}
</time>
</Link>
{
visitor.uid == data.user.uid &&
<>
<span> · </span>
<Link to={{ pathname: '/post', state: { draft: data }}}>Edit</Link>
</>
}
</div>
</Avatar>
<TagsList user={data.user} data={data.tags || []} />
</header>
{
data.body &&
<div className="msg-txt" ref={msgRef}>
<MessageContainer isCode={isCode} data={{ __html: format(data.body, data.mid.toString(), isCode) }} />
</div>
}
{
data.photo &&
<div className="msg-media">
<a href={`//i.juick.com/p/${data.mid}.${data.attach}`} data-fname={`${data.mid}.${data.attach}`}>
<img src={`//i.juick.com/photos-512/${data.mid}.${data.attach}`} alt="" />
</a>
</div>
}
<div className="embedContainer" ref={embedRef} />
<nav className="l">
{visitor.uid === data.user.uid ? (
<Link to={{ pathname: `/${data.user.uname}/${data.mid}` }} className="a-like msg-button">
<Icon name="ei-heart" size="s" />
<span>{likesSummary}</span>
</Link>
) : visitor.uid > 0 ? (
<Link to={{ pathname: '/post', search: `?body=!+%23${data.mid}` }} className="a-like msg-button">
<Icon name="ei-heart" size="s" />
<span>{likesSummary}</span>
</Link>
) : (
<a href="/login" className="a-login msg-button">
<Icon name="ei-heart" size="s" />
<span>{likesSummary}</span>
</a>
)}
{canComment && (
<>
<Link to={{ pathname: `/${data.user.uname}/${data.mid}`, state: { msg: data } }} className="a-comment msg-button">
<Icon name="ei-comment" size="s" />
<span>{commentsSummary}</span>
</Link>
</>
)}
</nav>
{children}
</div>
);
}
/**
* @param {{isCode: boolean, data: {__html: string}}} props
*/
function MessageContainer({ isCode, data }) {
return isCode ? (<pre dangerouslySetInnerHTML={data} />) : (<p dangerouslySetInnerHTML={data} />);
}
/**
* Tags component
* @param {{user: import('../api').User, data: string[]}} props
*/
function Tags({ data, user }) {
return data.length > 0 && (
<div className="msg-tags">
{
data.map(tag => {
return (<Link key={tag} to={{ pathname: `/${user.uname}`, search: `?tag=${tag}` }} title={tag}>{tag}</Link>);
// @ts-ignore
}).reduce((prev, curr) => [prev, ', ', curr])
}
</div>
);
}
const TagsList = memo(Tags);
function Recommends({ forMessage }) {
const { likes, recommendations } = forMessage;
return recommendations && recommendations.length > 0 && (
<div className="msg-recomms">{'♡ by '}
{
recommendations.map(it => (
<UserLink key={it.uri || it.uid} user={it} />
)).reduce((prev, curr) => [prev, ', ', curr])
}
{
likes > recommendations.length && (<span> and {likes - recommendations.length} others</span>)
}
</div>
) || null;
}
const Recommendations = memo(Recommends);
|