Files: 1399cedf6a9c2dcf4b6662bdeb010225320af71c / app / html / context.js
4121 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | const next = require('pull-next-step') |
5 | const get = require('lodash/get') |
6 | |
7 | exports.gives = nest('app.html.context') |
8 | |
9 | exports.needs = nest({ |
10 | 'about.html.image': 'first', |
11 | 'about.obs.name': 'first', |
12 | 'feed.pull.private': 'first', |
13 | 'feed.pull.rollup': 'first', |
14 | 'keys.sync.id': 'first', |
15 | 'history.sync.push': 'first', |
16 | 'message.html.subject': 'first', |
17 | 'translations.sync.strings': 'first', |
18 | }) |
19 | |
20 | |
21 | exports.create = (api) => { |
22 | return nest('app.html.context', (location) => { |
23 | |
24 | const strings = api.translations.sync.strings() |
25 | const myKey = api.keys.sync.id() |
26 | |
27 | const discover = { |
28 | notifications: Math.floor(Math.random()*5+1), |
29 | imageEl: h('i.fa.fa-binoculars'), |
30 | label: strings.blogIndex.title, |
31 | location: { page: 'blogIndex' }, |
32 | selected: ['blogIndex', 'home'].includes(location.page) |
33 | } |
34 | var nearby = [] |
35 | |
36 | var recentPeersContacted = Dict() |
37 | // TODO - extract as contact.obs.recentPrivate or something |
38 | |
39 | pull( |
40 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
41 | pull.filter(msg => msg.value.content.recps), |
42 | pull.drain(msg => { |
43 | msg.value.content.recps |
44 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
45 | .filter(recp => recp != myKey) |
46 | .forEach(recp => { |
47 | if (recentPeersContacted.has(recp)) return |
48 | |
49 | recentPeersContacted.put(recp, msg) |
50 | }) |
51 | }) |
52 | ) |
53 | |
54 | return h('Context -feed', [ |
55 | LevelOneContext(), |
56 | LevelTwoContext() |
57 | ]) |
58 | |
59 | function LevelOneContext () { |
60 | return h('div.level.-one', [ |
61 | Option(discover), |
62 | map(nearby, Option), // TODO |
63 | map(dictToCollection(recentPeersContacted), ({ key, value }) => { |
64 | const feedId = key() |
65 | const lastMsg = value() |
66 | return Option({ |
67 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
68 | imageEl: api.about.html.image(feedId), // TODO make avatar |
69 | label: api.about.obs.name(feedId), |
70 | location: Object.assign(lastMsg, { feed: feedId }), // QUESION : how should we pass the context, is stapling feed on like this horrible? |
71 | selected: location.feed === feedId |
72 | }) |
73 | }) |
74 | ]) |
75 | } |
76 | |
77 | function LevelTwoContext () { |
78 | const { key, value, feed: targetUser } = location |
79 | const root = get(value, 'content.root', key) |
80 | if (!targetUser) return |
81 | |
82 | var threads = MutantArray() |
83 | |
84 | pull( |
85 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
86 | pull.filter(msg => msg.value.content.recps), |
87 | pull.filter(msg => msg.value.content.recps |
88 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
89 | .some(recp => recp === targetUser) |
90 | ), |
91 | api.feed.pull.rollup(), |
92 | pull.drain(thread => threads.push(thread)) |
93 | ) |
94 | |
95 | return h('div.level.-two', [ |
96 | Option({ |
97 | location: {page: 'threadNew', feed: targetUser}, |
98 | label: h('Button', strings.threadNew.action.new) |
99 | }), |
100 | map(threads, thread => { |
101 | return Option({ |
102 | selected: thread.key === root, |
103 | location: Object.assign(thread, { feed: targetUser }), |
104 | label: api.message.html.subject(thread) |
105 | }) |
106 | }) |
107 | ]) |
108 | } |
109 | |
110 | function Option ({ notifications = 0, imageEl, label, location, selected }) { |
111 | const className = selected ? '-selected' : '' |
112 | const goToLocation = () => api.history.sync.push(location) |
113 | |
114 | if (!imageEl) { |
115 | return h('Option', { className, 'ev-click': goToLocation }, [ |
116 | h('div.label', label) |
117 | ]) |
118 | } |
119 | |
120 | return h('Option', { className }, [ |
121 | h('div.circle', [ |
122 | when(notifications, h('div.alert', notifications)), |
123 | imageEl |
124 | ]), |
125 | h('div.label', { 'ev-click': goToLocation }, label) |
126 | ]) |
127 | } |
128 | }) |
129 | } |
130 | |
131 |
Built with git-ssb-web