git ssb

10+

Matt McKegg / patchwork



Tree: 273e55604c0b074e6daa694af4a21a47cf258a18

Files: 273e55604c0b074e6daa694af4a21a47cf258a18 / main-window.js

3987 bytesRaw
1var combine = require('depject')
2var entry = require('depject/entry')
3var electron = require('electron')
4var h = require('mutant/h')
5var when = require('mutant/when')
6var onceTrue = require('mutant/once-true')
7var computed = require('mutant/computed')
8var catchLinks = require('./lib/catch-links')
9var insertCss = require('insert-css')
10var nest = require('depnest')
11var LatestUpdate = require('./lib/latest-update')
12var ref = require('ssb-ref')
13
14require('./lib/context-menu-and-spellcheck.js')
15
16module.exports = function (config) {
17 var sockets = combine(
18 overrideConfig(config),
19 require('./modules'),
20 require('./plugs'),
21 require('patchcore'),
22 require('./overrides')
23 )
24
25 var api = entry(sockets, nest({
26 'keys.sync.id': 'first',
27 'sbot.obs.connection': 'first',
28 'sbot.async.get': 'first',
29 'blob.sync.url': 'first',
30 'page.html.render': 'first',
31 'app.html.search': 'first',
32 'app.views': 'first',
33 'app.sync.externalHandler': 'first',
34 'app.html.progressNotifier': 'first',
35 'profile.sheet.edit': 'first'
36 }))
37
38 var id = api.keys.sync.id()
39 var latestUpdate = LatestUpdate()
40
41 // prompt to setup profile on first use
42 onceTrue(api.sbot.obs.connection, (sbot) => {
43 sbot.latestSequence(sbot.id, (_, key) => {
44 if (key == null) {
45 api.profile.sheet.edit()
46 }
47 })
48 })
49
50 var views = api.app.views(api.page.html.render, [
51 '/public', '/private', id, '/mentions'
52 ])
53
54 insertCss(require('./styles'))
55
56 var container = h(`MainWindow -${process.platform}`, [
57 h('div.top', [
58 h('span.history', [
59 h('a', {
60 'ev-click': views.goBack,
61 classList: [ when(views.canGoBack, '-active') ]
62 }),
63 h('a', {
64 'ev-click': views.goForward,
65 classList: [ when(views.canGoForward, '-active') ]
66 })
67 ]),
68 h('span.nav', [
69 tab('Public', '/public'),
70 tab('Private', '/private')
71 ]),
72 h('span.appTitle', ['Patchwork']),
73 h('span', [ api.app.html.search(views.setView) ]),
74 h('span.nav', [
75 tab('Profile', id),
76 tab('Mentions', '/mentions')
77 ])
78 ]),
79 when(latestUpdate,
80 h('div.info', [
81 h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [
82 h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here to download and view more info!'
83 ])
84 ])
85 ),
86 api.app.html.progressNotifier(),
87 views.html
88 ])
89
90 catchLinks(container, (href, external) => {
91 if (external) {
92 electron.shell.openExternal(href)
93 } else if (ref.isBlob(href)) {
94 electron.shell.openExternal(api.blob.sync.url(href))
95 } else if (ref.isMsg(href)) {
96 getExternalHandler(href, (err, handler) => {
97 if (err) throw err
98 if (handler) {
99 handler(href)
100 } else {
101 views.setView(href)
102 }
103 })
104 } else {
105 views.setView(href)
106 }
107 })
108
109 return container
110
111 // scoped
112
113 function getExternalHandler (key, cb) {
114 api.sbot.async.get(key, function (err, value) {
115 if (err) return cb(err)
116 cb(null, api.app.sync.externalHandler({key, value}))
117 })
118 }
119
120 function tab (name, view) {
121 var instance = views.get(view)
122 return h('a', {
123 'ev-click': function (ev) {
124 if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) {
125 instance.reload()
126 }
127 },
128 href: view,
129 classList: [
130 when(selected(view), '-selected')
131 ]
132 }, [
133 name,
134 when(instance.pendingUpdates, [
135 ' (', instance.pendingUpdates, ')'
136 ])
137 ])
138 }
139
140 function selected (view) {
141 return computed([views.currentView, view], (currentView, view) => {
142 return currentView === view
143 })
144 }
145}
146
147function overrideConfig (config) {
148 return [{
149 gives: nest('config.sync.load'),
150 create: function (api) {
151 return nest('config.sync.load', () => config)
152 }
153 }]
154}
155

Built with git-ssb-web