Files: 653e7d95783fcb22649a1ce70a47fc0deb571e3f / message / html / action / bookmark.js
1545 bytesRaw
1 | var { h, computed, when } = require('mutant') |
2 | var nest = require('depnest') |
3 | |
4 | exports.needs = nest({ |
5 | 'keys.sync.id': 'first', |
6 | 'bookmark.obs.bookmark': 'first', |
7 | 'bookmark.async.save': 'first' |
8 | }) |
9 | |
10 | exports.gives = nest('message.html.action') |
11 | |
12 | exports.create = (api) => { |
13 | return nest('message.html.action', msg => { |
14 | var id = api.keys.sync.id() |
15 | var bookmark = api.bookmark.obs.bookmark(msg.key, id) |
16 | var saved = computed([bookmark.tags], tags => isSaved(tags)) |
17 | return when(saved, |
18 | h('a.archive', { |
19 | href: '#', |
20 | 'ev-click': () => save(msg, bookmark.recps(), bookmark.tags(), false) |
21 | }, 'Archive'), |
22 | h('a.save', { |
23 | href: '#', |
24 | 'ev-click': () => save(msg, null, [], true) |
25 | }, 'Save') |
26 | ) |
27 | }) |
28 | |
29 | function save (msg, recps, bookmarkTags, status = true) { |
30 | var currentTags = bookmarkTags || [] |
31 | var tags |
32 | if (status) { |
33 | if (currentTags.includes('Archived')) { |
34 | tags = currentTags.filter(t => t !== 'Archived') |
35 | } else { |
36 | tags = currentTags |
37 | } |
38 | tags.push('Reading List') |
39 | } else { |
40 | tags = currentTags |
41 | tags.push('Archived') |
42 | } |
43 | |
44 | var notes = "" |
45 | if (msg.value && msg.value.content && msg.value.content.text) { |
46 | notes = msg.value.content.text.substring(0, 30) + "..." |
47 | } |
48 | |
49 | api.bookmark.async.save({ |
50 | messageId: msg.key, |
51 | recps, |
52 | notes, |
53 | tags |
54 | }, console.log) |
55 | } |
56 | } |
57 | |
58 | function isSaved (tags) { |
59 | if (!tags || !tags.includes) return |
60 | return tags.length > 0 && !tags.includes('Archived') |
61 | } |
62 |
Built with git-ssb-web