Files: 66dcda756a8e52b5f22cac3256eb987e6b3226b7 / app / page / addressBook.js
2659 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value, computed, map } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | |
5 | exports.gives = nest('app.page.addressBook') |
6 | |
7 | // declare consts to avoid magic-string errors |
8 | const FRIENDS = 'friends' |
9 | const FOLLOWING = 'following' |
10 | const FOLLOWERS = 'followers' |
11 | const SEARCH = 'search' |
12 | |
13 | exports.needs = nest({ |
14 | 'about.html.avatar': 'first', |
15 | 'about.async.suggest': 'first', |
16 | 'about.obs.name': 'first', |
17 | 'app.html.topNav': 'first', |
18 | // 'app.html.scroller': 'first', |
19 | 'app.html.sideNav': 'first', |
20 | 'app.html.topNav': 'first', |
21 | 'contact.html.follow': 'first', |
22 | 'contact.obs.relationships': 'first', |
23 | 'history.sync.push': 'first', |
24 | 'keys.sync.id': 'first', |
25 | 'translations.sync.strings': 'first' |
26 | }) |
27 | |
28 | exports.create = (api) => { |
29 | return nest('app.page.addressBook', function (location) { |
30 | // location here can expected to be: { page: 'addressBook'} |
31 | |
32 | const strings = api.translations.sync.strings() |
33 | const myKey = api.keys.sync.id() |
34 | const relationships = api.contact.obs.relationships(myKey) |
35 | |
36 | const SECTIONS = [FRIENDS, FOLLOWING, FOLLOWERS, SEARCH] |
37 | const section = location.section || FRIENDS |
38 | if (!SECTIONS.includes(section)) throw new Error('AddressBook location must include valid section, got:', location) |
39 | |
40 | const input = Value() |
41 | |
42 | const suggester = api.about.async.suggest() |
43 | const users = computed([relationships, input], (relationships, input) => { |
44 | if (section === SEARCH) { return suggester(input) } else { |
45 | const sectionRels = relationships[section] |
46 | if (!input) { |
47 | return sectionRels // show all e.g. friends |
48 | .reverse() |
49 | .map(id => { return { id, title: api.about.obs.name(id) } }) |
50 | } else { // show suggestions, and filter just the ones we want e.g. friends |
51 | return suggester(input, relationships.followers) // add extraIds to suggester |
52 | .filter(user => sectionRels.includes(user.id)) |
53 | } |
54 | } |
55 | }) |
56 | |
57 | const goTo = (loc) => () => api.history.sync.push(loc) |
58 | |
59 | return h('Page -addressBook', [ |
60 | api.app.html.sideNav(location, relationships), |
61 | h('Scroller.content', [ |
62 | h('section.top', [ |
63 | api.app.html.topNav(location, input) |
64 | ]), |
65 | h('section.content', [ |
66 | h('div.results', map(users, user => { |
67 | return h('div.result', { 'ev-click': goTo({page: 'userShow', feed: user.id}) }, [ |
68 | api.about.html.avatar(user.id), |
69 | h('div.alias', user.title), |
70 | // h('pre.key', user.id), |
71 | api.contact.html.follow(user.id) |
72 | ]) |
73 | })) |
74 | ]) |
75 | ]) |
76 | ]) |
77 | }) |
78 | } |
79 |
Built with git-ssb-web