Files: a6940a3d687c1a19beaf95c047f36bf32fbb8f2e / message / html / layout / inbox.js
3032 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Value } = require('mutant') |
3 | |
4 | exports.gives = nest('message.html.layout') |
5 | |
6 | exports.needs = nest({ |
7 | 'about.html.image': 'first', |
8 | 'keys.sync.id': 'first', |
9 | 'message.html.backlinks': 'first', |
10 | 'message.html.author': 'first', |
11 | 'message.html.markdown': 'first', |
12 | 'message.html.meta': 'map', |
13 | 'message.html.timestamp': 'first', |
14 | 'post.html.subject': 'first', |
15 | 'app.sync.goTo': 'first', // TODO generalise - this is patchbay only |
16 | }) |
17 | |
18 | exports.create = (api) => { |
19 | return nest('message.html.layout', inboxLayout) |
20 | |
21 | function inboxLayout (msgRollup, { layout, content } = {}) { |
22 | if (layout !== 'inbox') return |
23 | |
24 | var rawMessage = Value(null) |
25 | |
26 | const { timestamp, author, meta } = api.message.html |
27 | const { image } = api.about.html |
28 | |
29 | const msgCount = msgRollup.replies.length + 1 |
30 | const rootMsg = msgRollup |
31 | const newMsg = getNewestMsg(msgRollup) |
32 | |
33 | const myId = api.keys.sync.id() |
34 | const recps = msgRollup.value.content.recps |
35 | .map(recp => { |
36 | // TODO check these things are feed links!!! |
37 | if (typeof recp === 'string') return recp |
38 | |
39 | if (recp.link) return recp.link |
40 | }) |
41 | .filter(key => key !== myId) |
42 | .filter(Boolean) |
43 | .reduce((sofar, el) => sofar.includes(el) ? sofar : [...sofar, el], []) //.uniq |
44 | |
45 | const showNewMsg = newMsg && newMsg.value.author !== myId |
46 | |
47 | // const dataset = newMsg |
48 | // ? { root: rootMsg.key, id: newMsg.key } |
49 | // : { id: root.key } |
50 | |
51 | const openMessage = () => api.app.sync.goTo({ key: rootMsg.key }) |
52 | |
53 | const card = h('Message -inbox-card', { // This is required for patchbay keyboard shortcut 'o' |
54 | attributes: { |
55 | tabindex: '0' |
56 | } |
57 | }, [ |
58 | h('section.recps', {}, [ |
59 | h('div.spacer', { className: getSpacerClass(recps) }), |
60 | h('div.recps', { className: getRecpsClass(recps) }, recps.map(image)), |
61 | ]), |
62 | h('section.content', { 'ev-click': openMessage }, [ |
63 | h('header', [ |
64 | h('span.count', `(${msgCount})`), |
65 | api.post.html.subject(rootMsg) |
66 | ]), |
67 | showNewMsg |
68 | ? h('div.update', [ |
69 | h('span.replySymbol', '►'), |
70 | messageContent(newMsg), |
71 | timestamp(newMsg || rootMsg), |
72 | ]) : '' |
73 | ]), |
74 | ]) |
75 | |
76 | return card |
77 | } |
78 | |
79 | function messageContent (msg) { |
80 | if (!msg.value.content || !msg.value.content.text) return |
81 | return api.post.html.subject(msg) |
82 | } |
83 | } |
84 | |
85 | function getNewestMsg (msg) { |
86 | if (!msg.replies || msg.replies.length === 0) return |
87 | |
88 | return msg.replies[msg.replies.length - 1] |
89 | } |
90 | |
91 | function getSpacerClass (recps) { |
92 | switch (recps.length) { |
93 | case 1: |
94 | return '-half' |
95 | case 3: |
96 | return '-half' |
97 | case 4: |
98 | return '-half' |
99 | case 5: |
100 | return '-quarter' |
101 | case 6: |
102 | return '-quarter' |
103 | default: |
104 | return '' |
105 | } |
106 | } |
107 | |
108 | function getRecpsClass (recps) { |
109 | switch (recps.length) { |
110 | case 1: |
111 | return '-inbox-large' |
112 | case 2: |
113 | return '-inbox-large' |
114 | default: |
115 | return '-inbox-small' |
116 | } |
117 | } |
118 | |
119 | |
120 |
Built with git-ssb-web