Files: 0f3f8657225d9eb6e00c551660cc3a05e760c3bd / app / page / addressBook.js
2532 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 | }) |
23 | |
24 | exports.create = (api) => { |
25 | return nest('app.page.addressBook', function (location) { |
26 | // location here can expected to be: { page: 'addressBook', section: '...'} |
27 | location = Object.assign({ section: SEARCH }, location) |
28 | |
29 | const myKey = api.keys.sync.id() |
30 | const relationships = api.contact.obs.relationships(myKey) |
31 | |
32 | const SECTIONS = [FRIENDS, FOLLOWING, FOLLOWERS, SEARCH] |
33 | const section = location.section |
34 | if (!SECTIONS.includes(section)) throw new Error('AddressBook location must include valid section, got:', location) |
35 | |
36 | const input = Value() |
37 | |
38 | const suggester = api.about.async.suggest() |
39 | const users = computed([relationships, input], (relationships, input) => { |
40 | if (section === SEARCH) { return suggester(input) } else { |
41 | const sectionRels = relationships[section] |
42 | if (!input) { |
43 | return sectionRels // show all e.g. friends |
44 | .reverse() |
45 | .map(id => { return { id, title: api.about.obs.name(id) } }) |
46 | } else { // show suggestions, and filter just the ones we want e.g. friends |
47 | return suggester(input, relationships.followers) // add extraIds to suggester |
48 | .filter(user => sectionRels.includes(user.id)) |
49 | } |
50 | } |
51 | }) |
52 | |
53 | const goTo = (loc) => () => api.history.sync.push(loc) |
54 | |
55 | return h('Page -addressBook', [ |
56 | api.app.html.sideNav(location, relationships), |
57 | h('Scroller.content', [ |
58 | h('section.top', [ |
59 | api.app.html.topNav(location, input) |
60 | ]), |
61 | h('section.content', [ |
62 | h('div.results', map(users, user => { |
63 | return h('div.result', { 'ev-click': goTo({page: 'userShow', feed: user.id}) }, [ |
64 | api.about.html.avatar(user.id), |
65 | h('div.alias', user.title), |
66 | // h('pre.key', user.id), |
67 | api.contact.html.follow(user.id) |
68 | ]) |
69 | })) |
70 | ]) |
71 | ]) |
72 | ]) |
73 | }) |
74 | } |
75 |
Built with git-ssb-web