Files: e65b2dcf982e55434b4c16b527d24c1ad7182f89 / app / page / home.js
4633 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const isString = require('lodash/isString') |
4 | const More = require('hypermore') |
5 | const morphdom = require('morphdom') |
6 | const Debounce = require('obv-debounce') |
7 | |
8 | exports.gives = nest('app.page.home') |
9 | |
10 | exports.needs = nest({ |
11 | 'history.sync.push': 'first', |
12 | 'keys.sync.id': 'first', |
13 | 'translations.sync.strings': 'first', |
14 | 'state.obs.threads': 'first', |
15 | 'app.html.threadCard': 'first', |
16 | 'unread.sync.isUnread': 'first' |
17 | }) |
18 | |
19 | function toRecpGroup(msg) { |
20 | //cannocialize |
21 | return Array.isArray(msg.value.content.repcs) && |
22 | msg.value.content.recps.map(function (e) { |
23 | return (isString(e) ? e : e.link) |
24 | }).sort().map(function (id) { |
25 | return id.substring(0, 10) |
26 | }).join(',') |
27 | } |
28 | |
29 | exports.create = (api) => { |
30 | return nest('app.page.home', function (location) { |
31 | // location here can expected to be: { page: 'home'} |
32 | var strings = api.translations.sync.strings() |
33 | |
34 | |
35 | // function filterForThread (thread) { |
36 | // if(thread.value.private) |
37 | // return {private: toRecpGroup(thread)} |
38 | // else if(thread.value.content.channel) |
39 | // return {channel: thread.value.content.channel} |
40 | // } |
41 | |
42 | // function filter (rule, thread) { |
43 | // if(!thread.value) return false |
44 | // if(!rule) return true |
45 | // if(rule.channel) { |
46 | // return rule.channel == thread.value.content.channel |
47 | // } |
48 | // else if(rule.group) |
49 | // return rule.group == thread.value.content.group |
50 | // else if(rule.private) |
51 | // return rule.private == toRecpGroup(thread) |
52 | // else return true |
53 | // } |
54 | |
55 | var morePlease = false |
56 | var threadsObs = api.state.obs.threads() |
57 | |
58 | // DUCT TAPE: debounce the observable so it doesn't |
59 | // update the dom more than 1/second |
60 | threadsObs(function () { |
61 | if(morePlease) threadsObs.more() |
62 | }) |
63 | threadsObsDebounced = Debounce(threadsObs, 1000) |
64 | threadsObsDebounced(function () { |
65 | morePlease = false |
66 | }) |
67 | threadsObsDebounced.more = function () { |
68 | morePlease = true |
69 | requestIdleCallback(threadsObs.more) |
70 | } |
71 | |
72 | var container = h('div.container', []) |
73 | var threadsHtmlObs = More( |
74 | threadsObsDebounced, |
75 | function render (threads) { |
76 | |
77 | function latestUpdate(thread) { |
78 | var m = thread.timestamp |
79 | if(!thread.replies) return m |
80 | |
81 | for(var i = 0; i < thread.replies.length; i++) |
82 | m = Math.max(thread.replies[i].timestamp, m) |
83 | return m |
84 | } |
85 | |
86 | function roots (r) { |
87 | return Object.keys(r || {}).map(function (k) { |
88 | return threads.roots[r[k]] |
89 | }).filter(function (e) { |
90 | return e && e.value |
91 | }) |
92 | } |
93 | |
94 | var groupedThreads = |
95 | roots(threads.private) |
96 | .concat(roots(threads.channels)) |
97 | .concat(roots(threads.groups)) |
98 | .filter(function (thread) { |
99 | return thread.value.content.recps || thread.value.content.channel |
100 | }) |
101 | .map(function (thread) { |
102 | var unread = 0 |
103 | if(api.unread.sync.isUnread(thread)) |
104 | unread ++ |
105 | ;(thread.replies || []).forEach(function (msg) { |
106 | if(api.unread.sync.isUnread(msg)) unread ++ |
107 | }) |
108 | thread.unread = unread |
109 | // console.log('unread', thread.unread, +!!thread.unread) |
110 | return thread |
111 | }) |
112 | .sort(function (a, b) { |
113 | return ( |
114 | //((!!b.unread) - (!!a.unread)) || |
115 | (latestUpdate(b) - latestUpdate(a)) |
116 | ) |
117 | }) |
118 | |
119 | // console.log(groupedThreads.map(function (thread) { |
120 | // return {ch: thread.value.content.channel, unread: thread.unread, thread: thread} |
121 | // })) |
122 | // |
123 | morphdom(container, |
124 | // LEGACY: some of these containers could be removed |
125 | // but they are here to be compatible with the old MCSS. |
126 | h('div.container', [ |
127 | //private section |
128 | h('section.updates -directMessage', [ |
129 | h('div.threads', |
130 | groupedThreads.map(thread => { |
131 | const channel = thread.value.content.channel |
132 | const onClick = channel |
133 | ? (ev) => api.history.sync.push({ channel }) |
134 | : null // threadCard will install default onClick |
135 | |
136 | return api.app.html.threadCard(thread, { onClick }) |
137 | }) |
138 | ) |
139 | ]) |
140 | ]) |
141 | ) |
142 | return container |
143 | } |
144 | ) |
145 | return h('Page -home', {title: strings.home}, [ |
146 | threadsHtmlObs, |
147 | h('button', {'ev-click': threadsHtmlObs.more}, [strings.showMore]) |
148 | ]) |
149 | }) |
150 | } |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 |
Built with git-ssb-web