Files: 7e329243c50961d1697aed2c65b864bd8fdee622 / app / page / home.js
1846 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const {threadReduce} = require('ssb-reduce-stream') |
4 | const pull = require('pull-stream') |
5 | |
6 | exports.gives = nest('app.page.home') |
7 | |
8 | exports.needs = nest({ |
9 | 'app.sync.goTo': 'first', |
10 | 'feed.pull.public': 'first' |
11 | }) |
12 | |
13 | function firstLine (text) { |
14 | if(text.length < 80 && !~text.indexOf('\n')) return text |
15 | |
16 | return text.split('\n')[0].substring(0, 80) |
17 | } |
18 | |
19 | exports.create = (api) => { |
20 | return nest('app.page.home', home) |
21 | |
22 | function home (location) { |
23 | // location here can expected to be: { page: 'home' } |
24 | const { goTo } = api.app.sync |
25 | |
26 | var div = h('div', []) |
27 | |
28 | function subject (msg) { |
29 | return firstLine(msg.content.subject || msg.content.text) |
30 | } |
31 | |
32 | function item (name, thread) { |
33 | var reply = thread.replies && thread.replies[thread.replies.length-1] |
34 | if(!thread.value) { |
35 | |
36 | } |
37 | if(!thread.value) return |
38 | return h('div', [ |
39 | h('h2', name), |
40 | h('div.subject', [subject(thread.value)]), |
41 | reply ? h('div.reply', [subject(reply.value)]) : null |
42 | ] |
43 | ) |
44 | } |
45 | |
46 | pull( |
47 | api.feed.pull.public({reverse: true, limit: 1000}), |
48 | pull.through(console.log), |
49 | pull.collect(function (err, messages) { |
50 | var threads = messages.reduce(threadReduce, null) |
51 | for(var k in threads.channels) { |
52 | var id = threads.channels[k] |
53 | if(!threads.roots[id]) throw new Error('missing thread:'+id+' for channel:'+k) |
54 | var el = item(k, threads.roots[id]) |
55 | if(el) |
56 | div.appendChild(el) |
57 | } |
58 | }) |
59 | ) |
60 | |
61 | return div |
62 | |
63 | |
64 | return h('div', [ |
65 | h('h1', 'Home'), |
66 | h('div', { 'ev-click': () => goTo({ page: 'home' }) }, 'Home'), |
67 | h('div', { 'ev-click': () => goTo({ type: 'group', key: '%sadlkjas;lkdjas' }) }, 'Group') |
68 | ]) |
69 | } |
70 | } |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 |
Built with git-ssb-web