Files: 6712e94224526c0480342ad6ae1f205266d341a3 / app / html / thread-card.js
2417 bytesRaw
1 | var nest = require('depnest') |
2 | var h = require('mutant/h') |
3 | var isString= require('lodash/isString') |
4 | var maxBy= require('lodash/maxBy') |
5 | |
6 | exports.gives = nest('app.html.threadCard', true) |
7 | |
8 | exports.needs = nest({ |
9 | 'keys.sync.id': 'first', |
10 | 'history.sync.push': 'first', |
11 | 'about.obs.name': 'first', |
12 | 'about.html.avatar': 'first', |
13 | 'message.html.subject': 'first', |
14 | 'translations.sync.strings': 'first', |
15 | 'unread.sync.isUnread': 'first' |
16 | }) |
17 | |
18 | exports.create = function (api) { |
19 | |
20 | //render the icon for a thread. |
21 | //it would be more depjecty to split this |
22 | //into two methods, one in a private plugin |
23 | //one in a channel plugin |
24 | function threadIcon (msg) { |
25 | if(msg.value.private) { |
26 | const myId = api.keys.sync.id() |
27 | |
28 | return msg.value.content.recps |
29 | .map(link => isString(link) ? link : link.link) |
30 | .filter(link => link !== myId) |
31 | .map(api.about.html.avatar) |
32 | } |
33 | else if(msg.value.content.channel) |
34 | return '#'+msg.value.content.channel |
35 | } |
36 | |
37 | |
38 | // REFACTOR: move this to a template? |
39 | function buildRecipientNames (thread) { |
40 | const myId = api.keys.sync.id() |
41 | |
42 | return thread.value.content.recps |
43 | .map(link => isString(link) ? link : link.link) |
44 | .filter(link => link !== myId) |
45 | .map(api.about.obs.name) |
46 | } |
47 | |
48 | return nest('app.html.threadCard', (thread, opts = {}) => { |
49 | var strings = api.translations.sync.strings() |
50 | const { subject } = api.message.html |
51 | |
52 | if(!thread.value) return |
53 | if(!thread.value.content.text) return |
54 | |
55 | const subjectEl = h('div.subject', [ |
56 | opts.nameRecipients |
57 | ? h('div.recps', buildRecipientNames(thread).map(recp => h('div.recp', recp))) |
58 | : null, |
59 | subject(thread) |
60 | ]) |
61 | |
62 | const lastReply = thread.replies && maxBy(thread.replies, r => r.timestamp) |
63 | const replySample = lastReply ? subject(lastReply) : null |
64 | |
65 | const onClick = opts.onClick || function () { api.history.sync.push(thread) } |
66 | const id = `${thread.key.replace(/[^a-z0-9]/gi, '')}` //-${JSON.stringify(opts)}` |
67 | // id is only here to help morphdom morph accurately |
68 | |
69 | var className = thread.unread ? '-unread': '' |
70 | |
71 | return h('ThreadCard', { id, className }, [ |
72 | h('div.context', threadIcon(thread)), |
73 | h('div.content', {'ev-click': onClick}, [ |
74 | subjectEl, |
75 | replySample ? h('div.reply', [ |
76 | h('i.fa.fa-caret-left'), |
77 | replySample |
78 | ]) : null |
79 | ]) |
80 | ]) |
81 | }) |
82 | } |
83 | |
84 | |
85 |
Built with git-ssb-web