-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.jsx
98 lines (87 loc) · 2.94 KB
/
index.jsx
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
'use babel'
import React from 'react'
import mlib from 'ssb-msgs'
import { MsgLink, UserLink } from 'patchkit-links'
import { Block as MdBlock, Inline as MdInline } from 'patchkit-markdown'
import { Json as JsonRaw, Div as DivRaw } from 'patchkit-niceraw'
import * as u from 'patchkit-util'
import * as social from 'patchkit-util/social'
import t from 'patchwork-translations'
function rawAttrString(msg) {
return <JsonRaw obj={Object.assign({id: msg.key}, msg.value)} />
}
export class Block extends React.Component {
static contextTypes = {
user: React.PropTypes.object,
users: React.PropTypes.object
}
render() {
const msg = this.props.msg
const author = msg.value.author
const c = msg.value.content
if (this.props.forceRaw)
return rawAttrString(msg)
try {
switch (c.type) {
case 'post':
if (c.text) return <MdBlock md={c.text} msg={this.props.msg} />
break
case 'contact':
const result = renderContact(msg, this.context)
if (result)
return result
break
}
} catch (e) { console.warn(e) }
return rawAttrString(msg)
}
}
export class Inline extends React.Component {
static contextTypes = {
user: React.PropTypes.object,
users: React.PropTypes.object
}
render() {
const msg = this.props.msg
const author = msg.value.author
const c = msg.value.content
if (this.props.forceRaw)
return <DivRaw key={msg.key} obj={c} />
try {
switch (c.type) {
case 'post':
if (c.text) {
const text = this.props.limit ? u.shortString(c.text, this.props.limit) : c.text
return <MdInline md={text} />
}
break
case 'contact':
const result = renderContact(msg, this.context)
if (result)
return result
break
}
} catch (e) { console.warn(e) }
return <DivRaw key={msg.key} obj={c} />
}
}
function renderContact (msg, context) {
const author = msg.value.author
const c = msg.value.content
const contact = c.contact ? mlib.link(c.contact).link : undefined
if (contact === context.user.id) {
if (c.following) {
if (!social.follows(context.users, context.user.id, author))
return <div><i className="fa fa-user-plus" /> <UserLink id={author} />{t('contact.followedYou')}</div>
else
return <div><i className="fa fa-user-plus" /> <UserLink id={author} /> {t('contact.isNowYourFriend')}</div>
} else {
return <div><i className="fa fa-user-times" /> <UserLink id={author} /> {t('contact.unfollowedYou')}</div>
}
} else {
if (c.following)
return <div><i className="fa fa-user-plus" /> <UserLink id={author} /> {t('contact.followed', {name: u.getName(context.users, contact)})}</div>
else
return <div><i className="fa fa-user-times" /> <UserLink id={author} /> {t('contact.unfollowed', {name: u.getName(context.users, contact)})}</div>
}
}