Files: a5417350593eb41e7c3b56c8edf96aa895411fb2 / modules / feed-summary.js
3588 bytesRaw
1 | var Value = require('@mmckegg/mutant/value') |
2 | var h = require('@mmckegg/mutant/html-element') |
3 | var when = require('@mmckegg/mutant/when') |
4 | var computed = require('@mmckegg/mutant/computed') |
5 | var MutantArray = require('@mmckegg/mutant/array') |
6 | var Abortable = require('pull-abortable') |
7 | var Scroller = require('../lib/pull-scroll') |
8 | var FeedSummary = require('../lib/feed-summary') |
9 | |
10 | var m = require('../lib/h') |
11 | |
12 | var pull = require('pull-stream') |
13 | |
14 | var plugs = require('patchbay/plugs') |
15 | var message_render = plugs.first(exports.message_render = []) |
16 | var message_link = plugs.first(exports.message_link = []) |
17 | var person = plugs.first(exports.person = []) |
18 | var many_people = plugs.first(exports.many_people = []) |
19 | |
20 | exports.feed_summary = function (getStream, prefix) { |
21 | var sync = Value(false) |
22 | var updates = Value(0) |
23 | |
24 | var updateLoader = m('a.loader', { |
25 | href: '#', |
26 | 'ev-click': refresh |
27 | }, [ |
28 | 'Show ', |
29 | h('strong', [updates]), ' ', |
30 | when(computed(updates, a => a === 1), 'update', 'updates') |
31 | ]) |
32 | |
33 | var content = h('div.column.scroller__content') |
34 | |
35 | var scrollElement = h('div.column.scroller', { |
36 | style: { |
37 | 'overflow': 'auto' |
38 | } |
39 | }, [ |
40 | h('div.scroller__wrapper', [ |
41 | prefix, content |
42 | ]) |
43 | ]) |
44 | |
45 | setTimeout(refresh, 10) |
46 | |
47 | pull( |
48 | getStream({old: false}), |
49 | pull.drain((item) => { |
50 | if (item.value.content.type !== 'vote') { |
51 | updates.set(updates() + 1) |
52 | } |
53 | }) |
54 | ) |
55 | |
56 | var abortLastFeed = null |
57 | |
58 | return MutantArray([ |
59 | when(updates, updateLoader), |
60 | when(sync, scrollElement, m('Loading -large')) |
61 | ]) |
62 | |
63 | // scoped |
64 | |
65 | function refresh () { |
66 | if (abortLastFeed) { |
67 | abortLastFeed() |
68 | } |
69 | updates.set(0) |
70 | sync.set(false) |
71 | content.innerHTML = '' |
72 | |
73 | var abortable = Abortable() |
74 | abortLastFeed = abortable.abort |
75 | |
76 | pull( |
77 | FeedSummary(getStream, 1000, () => { |
78 | sync.set(true) |
79 | }), |
80 | abortable, |
81 | Scroller(scrollElement, content, renderItem, false, false) |
82 | ) |
83 | } |
84 | } |
85 | |
86 | function renderItem (item) { |
87 | if (item.type === 'message') { |
88 | var meta = null |
89 | var replies = item.replies.slice(-3).map(message_render) |
90 | var renderedMessage = item.message ? message_render(item.message) : null |
91 | if (renderedMessage) { |
92 | if (item.lastUpdateType === 'reply' && item.repliesFrom.size) { |
93 | meta = m('div.meta', [ |
94 | many_people(item.repliesFrom), ' replied' |
95 | ]) |
96 | } else if (item.lastUpdateType === 'dig' && item.digs.size) { |
97 | meta = m('div.meta', [ |
98 | many_people(item.digs), ' dug this message' |
99 | ]) |
100 | } |
101 | |
102 | return m('FeedEvent', [ |
103 | meta, |
104 | renderedMessage, |
105 | when(replies.length, [ |
106 | when(item.replies.length > replies.length, |
107 | m('a.full', {href: `#${item.messageId}`}, ['View full thread']) |
108 | ), |
109 | m('div.replies', replies) |
110 | ]) |
111 | ]) |
112 | } else { |
113 | if (item.lastUpdateType === 'reply' && item.repliesFrom.size) { |
114 | meta = m('div.meta', [ |
115 | many_people(item.repliesFrom), ' replied to ', message_link(item.messageId) |
116 | ]) |
117 | } else if (item.lastUpdateType === 'dig' && item.digs.size) { |
118 | meta = m('div.meta', [ |
119 | many_people(item.digs), ' dug ', message_link(item.messageId) |
120 | ]) |
121 | } |
122 | |
123 | if (meta || replies.length) { |
124 | return m('FeedEvent', [ |
125 | meta, m('div.replies', replies) |
126 | ]) |
127 | } |
128 | } |
129 | } else if (item.type === 'follow') { |
130 | return m('FeedEvent -follow', [ |
131 | m('div.meta', [ |
132 | person(item.id), ' followed ', many_people(item.contacts) |
133 | ]) |
134 | ]) |
135 | } |
136 | |
137 | return h('div') |
138 | } |
139 |
Built with git-ssb-web