Files: e82f383b2cc56a63e4afa6eb79a83f8b874713f6 / 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++) { m = Math.max(thread.replies[i].timestamp, m) } |
22 | return m |
23 | } |
24 | |
25 | exports.create = (api) => { |
26 | return nest('app.page.channel', function (location) { |
27 | const { channel } = location |
28 | var strings = api.translations.sync.strings() |
29 | |
30 | var channelObs = api.state.obs.channel(channel) |
31 | |
32 | // disable "Show More" button when we are at the last thread. |
33 | var disableShowMore = computed([channelObs], threads => !!threads.ended) |
34 | |
35 | var updates = h('div.threads', []) |
36 | var threadsHtmlObs = More( |
37 | channelObs, |
38 | function render (threads) { |
39 | morphdom(updates, |
40 | h('div.threads', Object.keys(threads.roots) |
41 | .map(id => threads.roots[id]) |
42 | .filter(thread => get(thread, 'value.content.channel') == channel) |
43 | .sort((a, b) => latestUpdate(b) - latestUpdate(a)) |
44 | .map(thread => api.app.html.blogCard(thread)) |
45 | ) |
46 | ) |
47 | return updates |
48 | } |
49 | ) |
50 | |
51 | const Link = api.app.html.link |
52 | |
53 | // TODO change this to -channel |
54 | return h('Page -home', {title: channel}, [ |
55 | Link({ page: 'threadNew', channel }, h('Button -strong', strings.channel.action.newThread)), |
56 | h('div.content', [ threadsHtmlObs ]), |
57 | h('Button -showMore', { |
58 | 'ev-click': threadsHtmlObs.more, |
59 | disabled: disableShowMore |
60 | }, [strings.showMore]) |
61 | ]) |
62 | }) |
63 | } |
64 |
Built with git-ssb-web