Files: 89b30291f29ac9c66dd0755935660464050a1699 / app / page / home.js
3819 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed } = require('mutant') |
3 | const {threadReduce} = require('ssb-reduce-stream') |
4 | const pull = require('pull-stream') |
5 | const isObject = require('lodash/isObject') |
6 | const isString = require('lodash/isString') |
7 | const last = require('lodash/last') |
8 | const get = require('lodash/get') |
9 | const More = require('hypermore') |
10 | const morphdom = require('morphdom') |
11 | |
12 | exports.gives = nest('app.page.home') |
13 | |
14 | exports.needs = nest({ |
15 | 'app.html.nav': 'first', |
16 | 'history.sync.push': 'first', |
17 | 'keys.sync.id': 'first', |
18 | 'translations.sync.strings': 'first', |
19 | 'state.obs.threads': 'first', |
20 | 'app.html.threadCard': 'first' |
21 | }) |
22 | |
23 | function toRecpGroup(msg) { |
24 | //cannocialize |
25 | return Array.isArray(msg.value.content.repcs) && |
26 | msg.value.content.recps.map(function (e) { |
27 | return (isString(e) ? e : e.link) |
28 | }).sort().map(function (id) { |
29 | return id.substring(0, 10) |
30 | }).join(',') |
31 | } |
32 | |
33 | exports.create = (api) => { |
34 | return nest('app.page.home', function (location) { |
35 | // location here can expected to be: { page: 'home' } |
36 | var strings = api.translations.sync.strings() |
37 | |
38 | var container = h('div.container', []) |
39 | |
40 | function filterForThread (thread) { |
41 | if(thread.value.private) |
42 | return {private: toRecpGroup(thread)} |
43 | else if(thread.value.content.channel) |
44 | return {channel: thread.value.content.channel} |
45 | } |
46 | |
47 | function filter (rule, thread) { |
48 | if(!thread.value) return false |
49 | if(!rule) return true |
50 | if(rule.channel) { |
51 | return rule.channel == thread.value.content.channel |
52 | } |
53 | else if(rule.group) |
54 | return rule.group == thread.value.content.group |
55 | else if(rule.private) |
56 | return rule.private == toRecpGroup(thread) |
57 | else return true |
58 | } |
59 | |
60 | var morePlease = false |
61 | var threadsObs = api.state.obs.threads() |
62 | var threadsObsDebounced = function (fn) { |
63 | var ts = 0, timer |
64 | threadsObs(function (v) { |
65 | if(Date.now() > ts + 1000) { |
66 | console.log('threads', Object.keys(v.roots).length, v.stats) |
67 | morePlease = false |
68 | ts = Date.now() |
69 | fn(v) |
70 | } |
71 | else { |
72 | clearTimeout(timer) |
73 | timer = setTimeout(function () { |
74 | ts = Date.now() |
75 | morePlease = false |
76 | fn(threadsObs.value) |
77 | }, 1000) |
78 | } |
79 | if(morePlease) threadsObs.more() |
80 | }) |
81 | |
82 | } |
83 | |
84 | threadsObsDebounced.more = function () { |
85 | morePlease = true |
86 | threadsObs.more() |
87 | } |
88 | |
89 | var threadsHtmlObs = More( |
90 | threadsObsDebounced, |
91 | function render (threads) { |
92 | morphdom(container, |
93 | //some of these containers could be removed |
94 | //but they are here to be compatible with the old MCSS. |
95 | h('div.container', [ |
96 | //private section |
97 | h('section.updates -directMessage', [ |
98 | h('div.threads', |
99 | Object.keys(threads.roots) |
100 | .map(function (id) { |
101 | return threads.roots[id] |
102 | }) |
103 | .filter(function (thread) { |
104 | return filter(location.filter, thread) |
105 | }) |
106 | .map(function (thread) { |
107 | var el = api.app.html |
108 | .threadCard(thread, opts) |
109 | if(!location.filter && el) |
110 | el.onclick = function () { |
111 | api.history.sync.push({page: 'home', filter: filterForThread(thread)}) |
112 | } |
113 | return el |
114 | }) |
115 | ) |
116 | ]), |
117 | ]) |
118 | ) |
119 | return container |
120 | } |
121 | ) |
122 | |
123 | return h('Page -home', [ |
124 | h('h1', 'Home'), |
125 | api.app.html.nav(), |
126 | threadsHtmlObs, |
127 | h('button', {'ev-click': threadsHtmlObs.more}, [strings.showMore]) |
128 | ]) |
129 | }) |
130 | } |
131 | |
132 |
Built with git-ssb-web