Files: 50a8f45832d95d56a26433d9cd66e7453416a55e / app / html / comments.js
4302 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Array: MutantArray, Value, map, computed, when, resolve, throttle } = require('mutant') |
3 | const get = require('lodash/get') |
4 | |
5 | exports.gives = nest('app.html.comments') |
6 | |
7 | exports.needs = nest({ |
8 | 'about.html.avatar': 'first', |
9 | 'about.obs.name': 'first', |
10 | 'backlinks.obs.for': 'first', |
11 | 'feed.obs.thread': 'first', |
12 | 'message.html.compose': 'first', |
13 | 'message.html.markdown': 'first', |
14 | 'message.html.timeago': 'first', |
15 | 'message.html.likes': 'first', |
16 | 'unread.sync.markRead': 'first', |
17 | 'unread.sync.isUnread': 'first', |
18 | 'translations.sync.strings': 'first', |
19 | }) |
20 | |
21 | exports.create = (api) => { |
22 | return nest('app.html.comments', comments) |
23 | |
24 | function comments (thread) { |
25 | const { messages, channel, lastId: branch } = thread |
26 | const strings = api.translations.sync.strings() |
27 | |
28 | // TODO - move this up into Patchcore |
29 | const messagesTree = computed(throttle(messages, 200), msgs => { |
30 | return msgs |
31 | .filter(msg => forkOf(msg) === undefined) |
32 | .map(threadMsg => { |
33 | const nestedReplies = msgs.filter(msg => forkOf(msg) === threadMsg.key) |
34 | threadMsg.replies = nestedReplies |
35 | return threadMsg |
36 | }) |
37 | }) |
38 | |
39 | const root = computed(messages, ary => ary[0].key) |
40 | |
41 | const meta = { |
42 | type: 'post', |
43 | root, |
44 | branch, |
45 | channel |
46 | } |
47 | // const twoComposers = computed(messages, messages => { |
48 | // return messages.length > 5 |
49 | // }) |
50 | const { compose } = api.message.html |
51 | |
52 | |
53 | return h('Comments', [ |
54 | // when(twoComposers, compose({ meta, shrink: true, canAttach: false })), |
55 | map(messagesTree, msg => Comment(msg, root, branch)), |
56 | compose({ meta, shrink: false, canAttach: true, placeholder: strings.writeComment }), |
57 | ]) |
58 | } |
59 | |
60 | function Comment (msgObs, root, branch) { |
61 | const strings = api.translations.sync.strings() |
62 | const msg = resolve(msgObs) |
63 | |
64 | const raw = get(msg, 'value.content.text') |
65 | var className = api.unread.sync.isUnread(msg) ? ' -unread' : ' -read' |
66 | api.unread.sync.markRead(msg) |
67 | |
68 | if (!get(msg, 'value.content.root')) return |
69 | |
70 | const { author, content } = msg.value |
71 | |
72 | // // TODO - move this upstream into patchcore:feed.obs.thread ?? |
73 | // // OR change strategy to use forks |
74 | // const backlinks = api.backlinks.obs.for(msg.key) |
75 | // const nestedReplies = computed(backlinks, backlinks => { |
76 | // return backlinks.filter(backlinker => { |
77 | // const { type, root } = backlinker.value.content |
78 | // return type === 'post' && root === msg.key |
79 | // }) |
80 | // }) |
81 | |
82 | var nestedReplyCompose = Value(false) |
83 | const toggleCompose = () => nestedReplyCompose.set(!nestedReplyCompose()) |
84 | const nestedReplyComposer = api.message.html.compose({ |
85 | meta: { |
86 | type: 'post', |
87 | root, |
88 | fork: msg.key, |
89 | branch, |
90 | channel: content.channel |
91 | }, |
92 | shrink: false, |
93 | canAttach: true, |
94 | canPreview: false, |
95 | placeholder: strings.writeComment |
96 | }, toggleCompose) |
97 | |
98 | return h('Comment', { className }, [ |
99 | h('div.left', api.about.html.avatar(author, 'tiny')), |
100 | h('div.right', [ |
101 | h('section.context', [ |
102 | h('div.name', api.about.obs.name(author)), |
103 | api.message.html.timeago(msg) |
104 | ]), |
105 | h('section.content', api.message.html.markdown(raw)), |
106 | when(msgObs.replies, |
107 | h('section.replies', |
108 | map(msgObs.replies, NestedComment) |
109 | ) |
110 | ), |
111 | h('section.actions', [ |
112 | h('div.reply', { 'ev-click': toggleCompose }, [ |
113 | h('i.fa.fa-commenting-o'), |
114 | ]), |
115 | api.message.html.likes(msg) |
116 | ]), |
117 | when(nestedReplyCompose, nestedReplyComposer), |
118 | ]) |
119 | ]) |
120 | } |
121 | |
122 | function NestedComment (msgObs) { |
123 | const msg = resolve(msgObs) |
124 | const raw = get(msg, 'value.content.text') |
125 | if (!raw) return |
126 | |
127 | const { author } = msg.value |
128 | |
129 | return h('Comment -nested', [ |
130 | h('div.left'), |
131 | h('div.right', [ |
132 | h('section.context', [ |
133 | h('div.name', api.about.obs.name(author)), |
134 | api.message.html.timeago(msg) |
135 | ]), |
136 | h('section.content', api.message.html.markdown(raw)), |
137 | ]) |
138 | ]) |
139 | |
140 | api.message.html.markdown(raw) |
141 | } |
142 | } |
143 | |
144 | function forkOf (msg) { |
145 | return get(msg, 'value.content.fork') |
146 | } |
147 | |
148 | |
149 | |
150 | |
151 |
Built with git-ssb-web