Files: adca29179dee2177af6fefa725c91581ffef5395 / app / page / channelSubscriptions.js
2788 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, Value, Array: MutantArray, onceTrue, computed, map: mutantMap } = require('mutant') |
3 | const sortBy = require('lodash/sortBy') |
4 | const map = require('lodash/map') |
5 | const difference = require('lodash/difference') |
6 | |
7 | exports.gives = nest('app.page.channelSubscriptions') |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.sideNav': 'first', |
11 | 'app.html.topNav': 'first', |
12 | 'app.html.scroller': 'first', |
13 | 'app.html.channelCard': 'first', |
14 | 'history.sync.push': 'first', |
15 | 'keys.sync.id': 'first', |
16 | 'channel.obs.subscribed': 'first', |
17 | 'channel.obs.recent': 'first', |
18 | 'channel.html.link': 'first', |
19 | 'translations.sync.strings': 'first', |
20 | 'sbot.async.friendsGet': 'first', |
21 | 'sbot.pull.userFeed': 'first', |
22 | 'sbot.obs.connection': 'first' |
23 | }) |
24 | |
25 | exports.create = (api) => { |
26 | const otherChannels = MutantArray() |
27 | |
28 | return nest('app.page.channelSubscriptions', function (location) { |
29 | const strings = api.translations.sync.strings() |
30 | const myId = api.keys.sync.id() |
31 | |
32 | const rawSubs = api.channel.obs.subscribed(myId) |
33 | const mySubs = computed(rawSubs, myChannels => [...myChannels.values()].reverse()) |
34 | |
35 | if (location.scope === 'user') { |
36 | return h('Page -channelSubscriptions', { title: strings.home }, [ |
37 | api.app.html.sideNav(location), |
38 | h('div.content', [ |
39 | when(rawSubs.sync, |
40 | [ |
41 | computed(mySubs, mys => mys.length === 0 ? strings.subscriptions.state.noSubscriptions : ''), |
42 | mutantMap(mySubs, api.app.html.channelCard) |
43 | ], |
44 | h('p', strings.loading) |
45 | ) |
46 | ]) |
47 | ]) |
48 | } |
49 | |
50 | if (location.scope === 'friends') { |
51 | // update list of other all channels |
52 | onceTrue( |
53 | api.sbot.obs.connection, |
54 | sbot => { |
55 | sbot.channel.subscriptions((err, c) => { |
56 | if (err) throw err |
57 | let b = map(c, (v, k) => { return {channel: k, users: v} }) |
58 | b = sortBy(b, o => o.users.length) |
59 | let res = b.reverse() |
60 | |
61 | otherChannels.set(res.map(c => c.channel)) |
62 | }) |
63 | } |
64 | ) |
65 | |
66 | const showMoreCounter = Value(1) |
67 | const newChannels = computed([otherChannels, mySubs, showMoreCounter], (other, mine, more) => { |
68 | return difference(other, mine) |
69 | .slice(0, 10 * more) |
70 | }) |
71 | |
72 | return h('Page -channelSubscriptions', { title: strings.home }, [ |
73 | api.app.html.sideNav(location), |
74 | h('div.content', [ |
75 | when(otherChannels, |
76 | [ |
77 | mutantMap(newChannels, api.app.html.channelCard), |
78 | h('Button', { 'ev-click': () => showMoreCounter.set(showMoreCounter() + 1) }, |
79 | strings.showMore |
80 | ) |
81 | ], |
82 | h('p', strings.loading) |
83 | ) |
84 | ]) |
85 | ]) |
86 | } |
87 | }) |
88 | } |
89 |
Built with git-ssb-web