git ssb

10+

Matt McKegg / patchwork



Tree: cffa78292325759c294a642e710938fdb67ebbb4

Files: cffa78292325759c294a642e710938fdb67ebbb4 / main-window.js

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

Built with git-ssb-web