Files: fea2881ae175c88bac926fcff65d32598742e9a1 / message / html / comment.js
3220 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, map, when, resolve } = require('mutant') |
3 | const get = require('lodash/get') |
4 | |
5 | exports.gives = nest('message.html.comment') |
6 | |
7 | exports.needs = nest({ |
8 | 'about.html.avatar': 'first', |
9 | 'about.obs.name': 'first', |
10 | // 'backlinks.obs.for': 'first', |
11 | 'message.html.compose': 'first', |
12 | 'message.html.markdown': 'first', |
13 | 'message.html.timeago': 'first', |
14 | 'message.html.likes': 'first', |
15 | 'unread.sync.markRead': 'first', |
16 | 'unread.sync.isUnread': 'first', |
17 | 'translations.sync.strings': 'first' |
18 | }) |
19 | |
20 | exports.create = (api) => { |
21 | return nest('message.html.comment', Comment) |
22 | |
23 | function Comment ({ comment: msgObs, replies, branch }) { |
24 | const strings = api.translations.sync.strings() |
25 | const msg = resolve(msgObs) |
26 | |
27 | const raw = get(msg, 'value.content.text') |
28 | var className = api.unread.sync.isUnread(msg) ? ' -unread' : ' -read' |
29 | api.unread.sync.markRead(msg) |
30 | |
31 | var root = get(msg, 'value.content.root') |
32 | if (!root) return |
33 | |
34 | const { author, content } = msg.value |
35 | |
36 | // // TODO - move this upstream into patchcore:feed.obs.thread ?? |
37 | // // OR change strategy to use forks |
38 | // const backlinks = api.backlinks.obs.for(msg.key) |
39 | // const nestedReplies = computed(backlinks, backlinks => { |
40 | // return backlinks.filter(backlinker => { |
41 | // const { type, root } = backlinker.value.content |
42 | // return type === 'post' && root === msg.key |
43 | // }) |
44 | // }) |
45 | |
46 | var nestedReplyCompose = Value(false) |
47 | const toggleCompose = () => nestedReplyCompose.set(!nestedReplyCompose()) |
48 | const nestedReplyComposer = api.message.html.compose({ |
49 | meta: { |
50 | type: 'post', |
51 | root, |
52 | fork: msg.key, |
53 | branch, |
54 | channel: content.channel |
55 | }, |
56 | shrink: false, |
57 | canAttach: true, |
58 | canPreview: false, |
59 | placeholder: strings.writeComment |
60 | }, toggleCompose) |
61 | |
62 | return h('Comment', { className }, [ |
63 | h('div.left', api.about.html.avatar(author, 'tiny')), |
64 | h('div.right', [ |
65 | h('section.context', [ |
66 | h('div.name', api.about.obs.name(author)), |
67 | api.message.html.timeago(msg) |
68 | ]), |
69 | h('section.content', api.message.html.markdown(raw)), |
70 | when(replies, |
71 | h('section.replies', |
72 | map( |
73 | replies, |
74 | NestedComment, |
75 | { comparer: (a, b) => a === b } |
76 | ) |
77 | ) |
78 | ), |
79 | h('section.actions', [ |
80 | h('div.reply', { 'ev-click': toggleCompose }, [ |
81 | h('i.fa.fa-commenting-o') |
82 | ]), |
83 | api.message.html.likes(msg) |
84 | ]), |
85 | when(nestedReplyCompose, nestedReplyComposer) |
86 | ]) |
87 | ]) |
88 | } |
89 | |
90 | function NestedComment (msgObs) { |
91 | const msg = resolve(msgObs) |
92 | const raw = get(msg, 'value.content.text') |
93 | if (!raw) return |
94 | |
95 | const { author } = msg.value |
96 | |
97 | return h('Comment -nested', [ |
98 | h('div.left'), |
99 | h('div.right', [ |
100 | h('section.context', [ |
101 | h('div.name', api.about.obs.name(author)), |
102 | api.message.html.timeago(msg) |
103 | ]), |
104 | h('section.content', api.message.html.markdown(raw)) |
105 | ]) |
106 | ]) |
107 | } |
108 | } |
109 | |
110 | function forkOf (msg) { |
111 | return get(msg, 'value.content.fork') |
112 | } |
113 | |
114 |
Built with git-ssb-web