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