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