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