git ssb

16+

Dominic / patchbay



Tree: 8ae0df05df1185783cf0ee77113ce4bcc4531bdd

Files: 8ae0df05df1185783cf0ee77113ce4bcc4531bdd / about / async / suggest.js

1780 bytesRaw
1var nest = require('depnest')
2var { Struct, map, computed, watch } = require('mutant')
3
4exports.gives = nest('about.async.suggest')
5
6exports.needs = nest({
7 'about.obs': {
8 name: 'first',
9 imageUrl: 'first'
10 },
11 'contact.obs.following': 'first',
12 'feed.obs.recent': 'first',
13 'keys.sync.id': 'first'
14})
15
16exports.create = function (api) {
17 var suggestions = null
18 var recentSuggestions = null
19
20 return nest('about.async.suggest', function () {
21 loadSuggestions()
22 return function (word) {
23 if (!word) {
24 return recentSuggestions()
25 } else {
26 return suggestions().filter((item) => {
27 return item.title.toLowerCase().startsWith(word.toLowerCase())
28 })
29 }
30 }
31 })
32
33 function loadSuggestions () {
34 if (!suggestions) {
35 var id = api.keys.sync.id()
36 var following = api.contact.obs.following(id)
37 var recentlyUpdated = api.feed.obs.recent()
38 var contacts = computed([following, recentlyUpdated], function (a, b) {
39 var result = Array.from(a)
40 b.forEach((item, i) => {
41 if (!result.includes(item)) {
42 result.push(item)
43 }
44 })
45 return result
46 })
47
48 recentSuggestions = map(computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)), suggestion, {idle: true})
49 suggestions = map(contacts, suggestion, {idle: true})
50 watch(recentSuggestions)
51 watch(suggestions)
52 }
53 }
54
55 function suggestion (id) {
56 var name = api.about.obs.name(id)
57 return Struct({
58 title: name,
59 id,
60 subtitle: id.substring(0, 10),
61 value: computed([name, id], mention),
62 image: api.about.obs.imageUrl(id),
63 showBoth: true
64 })
65 }
66}
67
68function mention (name, id) {
69 return `[@${name}](${id})`
70}
71
72

Built with git-ssb-web