git ssb

16+

Dominic / patchbay



Tree: c2dcf64996a3dccefa598cd0ab5f6c2bc849d57d

Files: c2dcf64996a3dccefa598cd0ab5f6c2bc849d57d / about / async / suggest.js

1812 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(
49 computed(recentlyUpdated, (items) => Array.from(items).slice(0, 10)),
50 suggestion,
51 {idle: true}
52 )
53 suggestions = map(contacts, suggestion, {idle: true})
54 watch(recentSuggestions)
55 watch(suggestions)
56 }
57 }
58
59 function suggestion (id) {
60 var name = api.about.obs.name(id)
61 return Struct({
62 title: name,
63 id,
64 subtitle: id.substring(0, 10),
65 value: computed([name, id], mention),
66 image: api.about.obs.imageUrl(id),
67 showBoth: true
68 })
69 }
70}
71
72function mention (name, id) {
73 return `[@${name}](${id})`
74}
75
76

Built with git-ssb-web