Files: 1967ee98ce129f2ab3b072bcdeac1ad9f0990092 / app / page / channel.js
1897 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed } = require('mutant') |
3 | const More = require('hypermore') |
4 | const morphdom = require('morphdom') |
5 | const get = require('lodash/get') |
6 | |
7 | exports.gives = nest('app.page.channel') |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.link': 'first', |
11 | 'app.html.blogCard': 'first', |
12 | 'history.sync.push': 'first', |
13 | 'state.obs.channel': 'first', |
14 | 'translations.sync.strings': 'first', |
15 | }) |
16 | |
17 | function latestUpdate(thread) { |
18 | var m = thread.timestamp |
19 | if(!thread.replies) return m |
20 | |
21 | for(var i = 0; i < thread.replies.length; i++) |
22 | m = Math.max(thread.replies[i].timestamp, m) |
23 | return m |
24 | } |
25 | |
26 | exports.create = (api) => { |
27 | return nest('app.page.channel', function (location) { |
28 | const { channel } = location |
29 | var strings = api.translations.sync.strings() |
30 | |
31 | var channelObs = api.state.obs.channel(channel) |
32 | |
33 | //disable "Show More" button when we are at the last thread. |
34 | var disableShowMore = computed([channelObs], threads => !!threads.ended) |
35 | |
36 | var updates = h('div.threads', []) |
37 | var threadsHtmlObs = More( |
38 | channelObs, |
39 | function render (threads) { |
40 | morphdom(updates, |
41 | h('div.threads', Object.keys(threads.roots) |
42 | .map(id => threads.roots[id]) |
43 | .filter(thread => get(thread, 'value.content.channel') == channel) |
44 | .sort((a, b) => latestUpdate(b) - latestUpdate(a)) |
45 | .map(thread => api.app.html.blogCard(thread)) |
46 | ) |
47 | ) |
48 | return updates |
49 | } |
50 | ) |
51 | |
52 | const Link = api.app.html.link |
53 | |
54 | // TODO change this to -channel |
55 | return h('Page -home', {title: channel}, [ |
56 | Link({ page: 'threadNew', channel }, h('Button -strong', strings.channel.action.newThread)), |
57 | h('div.content', [ threadsHtmlObs ]), |
58 | h('Button -showMore', { |
59 | 'ev-click': threadsHtmlObs.more, |
60 | disabled: disableShowMore |
61 | }, [strings.showMore]) |
62 | ]) |
63 | }) |
64 | } |
65 | |
66 | |
67 |
Built with git-ssb-web