aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Message.js
blob: ec9af46f0fc5990a7cc421639b75e0dd8e8f1452 (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
190
191
192
193
194
195
196
197
198
import React, { Fragment, memo, useEffect, useRef } from 'react'

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)

import Icon from './Icon'
import Avatar from './Avatar'
import { UserLink } from './UserInfo'

import { format, embedUrls } from '../utils/embed'
import { useVisitor } from './VisitorContext'

/**
 * @callback ToggleSubscriptionCallback
 * @param { import('../client').Message } message
 */

/**
 * @typedef {object} MessageProps
 * @property { import('../client').Message } data data
 * @property {boolean} isThread
 * @property {ToggleSubscriptionCallback} onToggleSubscription
 */

/**
 * Message component
 * @param {React.PropsWithChildren<{}> & MessageProps} props props
 */
export default function Message({ data, isThread, onToggleSubscription, children }) {
    const [visitor] = useVisitor()
    const isCode = (data.tags || []).indexOf('code') >= 0
    const likesSummary = data.likes ? `${data.likes}` : 'Recommend'
    const commentsSummary = data.replies ? `${data.replies}` : 'Comment'
    /**
     * @type {React.MutableRefObject<HTMLDivElement?>}
     */
    const embedRef = useRef(null)
    /**
     * @type {React.MutableRefObject<HTMLDivElement?>}
     */
    const msgRef = useRef(null)
    useEffect(() => {
        const msg = msgRef.current
        const embed = embedRef.current
        if (msg && embed) {
            embedUrls(msg.querySelectorAll('a'), embed)
            if (!embed.hasChildNodes()) {
                embed.style.display = 'none'
            }
        }
    }, [])
    const canComment = data.user && visitor.uid === data.user.uid || !data.ReadOnly && visitor.uid > 0
    || !data.ReadOnly && !isThread
    return (
        <div className="msg-cont">
            <Recommendations forMessage={data} />
            <header className="h">
                {
                    data.user &&
                    <Avatar user={data.user}>
                        <div className="msg-ts">
                            <a href={`/${data.user.uname}/${data.mid}`}>
                                <time dateTime={data.timestamp}
                                    title={dayjs.utc(data.timestamp).local().format('lll')}>
                                    {dayjs.utc(data.timestamp).fromNow()}
                                </time>
                            </a>
                            {
                                visitor.uid == data.user.uid &&
                                <>
                                    <span>&nbsp;&middot;&nbsp;</span>
                                    <a href={'/post'}>Edit</a>
                                </>
                            }
                        </div>
                    </Avatar>
                }
            </header>
            {
                data.body && data.user && data.mid &&
                <div className="msg-txt" ref={msgRef}>
                    <TagsList user={data.user} data={data.tags || []} />
                    <MessageContainer isCode={isCode} data={{ __html: format(data.body, data.mid.toString(), isCode) }} />
                </div>
            }
            {
                data.photo && data.attachment && data.attachment.small &&
                <div className="msg-media">
                    <a href={`https://i.juick.com/p/${data.mid}.${data.attach}`} data-fname={`${data.mid}.${data.attach}`}>
                        <img src={data.attachment.small.url} width={data.attachment.small.width} height={data.attachment.small.height} alt="Message media" />
                    </a>
                </div>
            }
            <div className="embedContainer" ref={embedRef} />
            {canComment &&
                <nav className="l">
                    {data.user && visitor.uid === data.user.uid ? (
                        <a href={`/${data.user.uname}/${data.mid}`} className="a-like msg-button">
                            <Icon name="ei-heart" size="s" />
                            <span>{likesSummary}</span>
                        </a>
                    ) : visitor.uid > 0 ? (
                        <a href={'/post'} className="a-like msg-button">
                            <Icon name="ei-heart" size="s" />
                            <span>{likesSummary}</span>
                        </a>
                    ) : (
                        <a href="/login" className="a-login msg-button">
                            <Icon name="ei-heart" size="s" />
                            <span>{likesSummary}</span>
                        </a>
                    )}
                    {
                        data.user && canComment && ((
                            isThread ? (
                                <a className="msg-button" onClick={() => { onToggleSubscription(data) }}>
                                    {
                                        data.subscribed ? (<>
                                            <Icon name="ei-check" size="s" />
                                            <span>Subscribed</span>
                                        </>) : (<>
                                            <Icon name="ei-eye" size="s" />
                                            <span>Subscribe</span>
                                        </>)}
                                </a>
                            ) : (
                                <a href={`/${data.user.uname}/${data.mid}`} className="a-comment msg-button">
                                    <Icon name="ei-comment" size="s" />
                                    <span>{commentsSummary}</span>
                                </a>
                            )
                        ))
                    }
                </nav>
            }
            {children}
        </div >
    )
}

/**
 * @param {{isCode: boolean, data: {__html: string}}} props props
 */
function MessageContainer({ isCode, data }) {
    return isCode ? (<pre dangerouslySetInnerHTML={data} />) : (<span dangerouslySetInnerHTML={data} />)
}

/**
 * Tags component
 * @param {{user: import('../client').User, data: string[]}} props props
 */
function Tags({ data, user }) {
    return data.length > 0 ? (
        <span className="msg-tags">
            {
                data.map((tag, index) => (
                    <Fragment key={tag}>
                        {index > 0 && ' '}
                        <a key={tag} href={`/${user.uname}?tag=${tag}`} title={tag}>
                            {tag}
                        </a>
                    </Fragment>
                ))
            }
        </span>
    ) : null
}

const TagsList = memo(Tags)

/**
 *
 * @param {{forMessage: import('../client').Message}} props props
 */
function Recommends({ forMessage }) {
    const { recommendations } = forMessage
    const likes = forMessage.likes || 0
    return recommendations && recommendations.length > 0 && (
        <div className="msg-recomms">{'♡ by '}
            {
                recommendations.map((it, index) => (
                    <Fragment key={it.uri || it.uid}>
                        {index > 0 && ', '}
                        <UserLink key={it.uri || it.uid} user={it} />
                    </Fragment>
                ))
            }
            {
                likes > recommendations.length && (<span>&nbsp;and {likes - recommendations.length} others</span>)
            }
        </div>
    ) || null
}

const Recommendations = memo(Recommends)