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