git ssb

10+

Matt McKegg / patchwork



Tree: c2d8083ad55e777a6e7b82aaa57f2aaa27f5a607

Files: c2d8083ad55e777a6e7b82aaa57f2aaa27f5a607 / plugs / message / html / layout / default.js

3551 bytesRaw
1const { h, map, computed } = require('mutant')
2var nest = require('depnest')
3var ref = require('ssb-ref')
4
5exports.needs = nest({
6 'profile.html.person': 'first',
7 'message.obs.backlinks': 'first',
8 'message.obs.name': 'first',
9 'message.obs.forks': 'first',
10 'message.obs.author': 'first',
11 'message.html': {
12 link: 'first',
13 meta: 'map',
14 action: 'map',
15 timestamp: 'first'
16 },
17 'about.html.image': 'first'
18})
19
20exports.gives = nest('message.html.layout')
21
22exports.create = function (api) {
23 return nest('message.html.layout', layout)
24
25 function layout (msg, opts) {
26 if (!(opts.layout === undefined || opts.layout === 'default')) return
27
28 var backlinks = opts.backlinks ? api.message.obs.backlinks(msg.key) : []
29 var forks = msg.value.content.root ? api.message.obs.forks(msg.key) : []
30
31 var classList = ['Message']
32 var replyInfo = null
33
34 if (msg.value.content.root) {
35 classList.push('-reply')
36 var branch = msg.value.content.branch
37 if (branch) {
38 if (!opts.previousId || (opts.previousId && last(branch) && opts.previousId !== last(branch))) {
39 replyInfo = h('span', ['in reply to ', api.message.html.link(last(branch))])
40 }
41 }
42 } else if (msg.value.content.project) {
43 replyInfo = h('span', ['on ', api.message.html.link(msg.value.content.project)])
44 }
45
46 if (opts.priority === 2) {
47 classList.push('-new')
48 }
49
50 return h('div', {
51 classList
52 }, [
53 messageHeader(msg, {
54 replyInfo,
55 priority: opts.priority
56 }),
57 h('section', [opts.content]),
58 computed(msg.key, (key) => {
59 if (ref.isMsg(key)) {
60 return h('footer', [
61 h('div.actions', [
62 api.message.html.action(msg)
63 ])
64 ])
65 }
66 }),
67 map(forks, msgId => {
68 return h('a.backlink', {
69 href: msgId,
70 title: msgId
71 }, [
72 h('strong', [
73 authorLink(msgId), ' forked this discussion:'
74 ]), ' ',
75 api.message.obs.name(msgId)
76 ])
77 }),
78 map(backlinks, msgId => {
79 return h('a.backlink', {
80 href: msgId,
81 title: msgId
82 }, [
83 h('strong', [
84 authorLink(msgId), ' referenced this message:'
85 ]), ' ',
86 api.message.obs.name(msgId)
87 ])
88 })
89 ])
90
91 // scoped
92
93 function messageHeader (msg, {replyInfo, priority}) {
94 var additionalMeta = []
95 if (opts.priority >= 2) {
96 additionalMeta.push(h('span.flag -new', {title: 'New Message'}))
97 }
98 return h('header', [
99 h('div.main', [
100 h('a.avatar', {href: `${msg.value.author}`}, [
101 api.about.html.image(msg.value.author)
102 ]),
103 h('div.main', [
104 h('div.name', [
105 api.profile.html.person(msg.value.author)
106 ]),
107 h('div.meta', [
108 api.message.html.timestamp(msg), ' ',
109 replyInfo
110 ])
111 ])
112 ]),
113 h('div.meta', [
114 api.message.html.meta(msg),
115 additionalMeta
116 ])
117 ])
118 }
119
120 function authorLink (msgId) {
121 var author = api.message.obs.author(msgId)
122 return computed(author, author => {
123 if (author) {
124 return api.profile.html.person(author)
125 } else {
126 return 'Someone'
127 }
128 })
129 }
130 }
131}
132
133function last (array) {
134 if (Array.isArray(array)) {
135 return array[array.length - 1]
136 } else {
137 return array
138 }
139}
140

Built with git-ssb-web