git ssb

10+

Matt McKegg / patchwork



Tree: e90da2f27b61cf784fcae42a35492b32c3e19af3

Files: e90da2f27b61cf784fcae42a35492b32c3e19af3 / modules / sheet / profiles.js

2764 bytesRaw
1var {h, when, map, computed, Value, lookup} = require('mutant')
2var nest = require('depnest')
3var catchLinks = require('../../lib/catch-links')
4
5exports.needs = nest({
6 'sheet.display': 'first',
7 'keys.sync.id': 'first',
8 'contact.obs.following': 'first',
9 'profile.obs.rank': 'first',
10 'about.html.image': 'first',
11 'about.obs.name': 'first',
12 'app.navigate': 'first',
13 'intl.sync.i18n': 'first'
14})
15
16exports.gives = nest('sheet.profiles')
17
18exports.create = function (api) {
19 const i18n = api.intl.sync.i18n
20 return nest('sheet.profiles', function (ids, title) {
21 api.sheet.display(close => {
22 var currentFilter = Value()
23 var nameLookup = lookup(ids, (id) => {
24 return [id, api.about.obs.name(id)]
25 })
26 var filteredIds = computed([ids, nameLookup, currentFilter], (ids, nameLookup, filter) => {
27 if (filter) {
28 var result = []
29 for (var k in nameLookup) {
30 if (nameLookup[k] && nameLookup[k].toLowerCase().includes(filter.toLowerCase())) {
31 result.push(k)
32 }
33 }
34 return result
35 } else {
36 return ids
37 }
38 })
39 var content = h('div', {
40 style: { padding: '20px' }
41 }, [
42 h('h2', {
43 style: { 'font-weight': 'normal' }
44 }, [
45 title,
46 h('input', {
47 type: 'search',
48 placeholder: 'filter names',
49 'ev-input': function (ev) {
50 currentFilter.set(ev.target.value)
51 },
52 style: {
53 'float': 'right',
54 'font-size': '100%'
55 }
56 })
57 ]),
58 renderContactBlock(filteredIds)
59 ])
60
61 catchLinks(content, (href, external, anchor) => {
62 if (!external) {
63 api.app.navigate(href, anchor)
64 close()
65 }
66 })
67
68 return {
69 content,
70 footer: [
71 h('button -close', {
72 'ev-click': close
73 }, i18n('Close'))
74 ]
75 }
76 })
77 })
78
79 function renderContactBlock (profiles) {
80 var yourId = api.keys.sync.id()
81 var yourFollows = api.contact.obs.following(yourId)
82 profiles = api.profile.obs.rank(profiles)
83 return [
84 h('div', {
85 classList: 'ProfileList'
86 }, [
87 map(profiles, (id) => {
88 var following = computed(yourFollows, f => f.includes(id))
89 return h('a.profile', {
90 href: id,
91 classList: [
92 when(following, '-following')
93 ]
94 }, [
95 h('div.avatar', [api.about.html.image(id)]),
96 h('div.main', [
97 h('div.name', [ api.about.obs.name(id) ])
98 ])
99 ])
100 }, { idle: true, maxTime: 2 })
101 ])
102 ]
103 }
104}
105

Built with git-ssb-web