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