git ssb

10+

Matt McKegg / patchwork



Tree: 01436b0a000173e48f00729b2c1f7b5b3cc4243d

Files: 01436b0a000173e48f00729b2c1f7b5b3cc4243d / modules / app.js

5178 bytesRaw
1
2var h = require('../lib/h')
3var Value = require('@mmckegg/mutant/value')
4var when = require('@mmckegg/mutant/when')
5var computed = require('@mmckegg/mutant/computed')
6var toCollection = require('@mmckegg/mutant/dict-to-collection')
7var MutantDict = require('@mmckegg/mutant/dict')
8var MutantMap = require('@mmckegg/mutant/map')
9var watch = require('@mmckegg/mutant/watch')
10
11var plugs = require('patchbay/plugs')
12
13var screenView = plugs.first(exports.screen_view = [])
14
15var path = require('path')
16var config = require('ssb-config/inject')(process.env.ssb_appname)
17var keys = config.keys = require('ssb-keys').loadOrCreateSync(path.join(config.path, 'secret'))
18
19var insertCss = require('insert-css')
20
21
22exports.app = function () {
23
24 console.log("MAIN SCREEN TURN ON")
25 var id = keys.id
26
27 insertCss(require('../styles'))
28
29 var searchTimer = null
30 var searchBox = h('input.search', {
31 type: 'search',
32 placeholder: 'word, @key, #channel'
33 })
34
35 searchBox.oninput = function () {
36 clearTimeout(searchTimer)
37 searchTimer = setTimeout(doSearch, 500)
38 }
39
40 searchBox.onfocus = function () {
41 if (searchBox.value) {
42 doSearch()
43 }
44 }
45
46 var forwardHistory = []
47 var backHistory = []
48
49 var views = MutantDict({
50 // preload tabs (and subscribe to update notifications)
51 '/public': screenView('/public'),
52 '/private': screenView('/private'),
53 [id]: screenView(id),
54 '/notifications': screenView('/notifications')
55 })
56
57 var lastViewed = {}
58
59 // delete cached view after 30 mins of last seeing
60 setInterval(() => {
61 views.keys().forEach((view) => {
62 if (lastViewed[view] !== true && Date.now() - lastViewed[view] > (30 * 60e3) && view !== currentView()) {
63 views.delete(view)
64 }
65 })
66 }, 60e3)
67
68 var canGoForward = Value(false)
69 var canGoBack = Value(false)
70 var currentView = Value('/public')
71
72 watch(currentView, (view) => {
73 window.location.hash = `#${view}`
74 })
75
76 window.onhashchange = function (ev) {
77 var path = window.location.hash.substring(1)
78 if (path) {
79 setView(path)
80 }
81 }
82
83 var mainElement = h('div.main', MutantMap(toCollection(views), (item) => {
84 return h('div.view', {
85 hidden: computed([item.key, currentView], (a, b) => a !== b)
86 }, [ item.value ])
87 }))
88
89 var root = h('MainWindow', {
90 classList: [ '-' + process.platform ]
91 }, [
92 h('div.top', [
93 h('span.history', [
94 h('a', {
95 'ev-click': goBack,
96 classList: [ when(canGoBack, '-active') ]
97 }, '<'),
98 h('a', {
99 'ev-click': goForward,
100 classList: [ when(canGoForward, '-active') ]
101 }, '>')
102 ]),
103 h('span.nav', [
104 tab('Public', '/public'),
105 tab('Private', '/private')
106 ]),
107 h('span.appTitle', ['Patchwork']),
108 h('span', [ searchBox ]),
109 h('span.nav', [
110 tab('Profile', id),
111 tab('Mentions', '/notifications')
112 ])
113 ]),
114 mainElement
115 ])
116
117 document.body.appendChild(root)
118
119 // scoped
120
121 function tab (name, view) {
122 var instance = views.get(view)
123 lastViewed[view] = true
124 return h('a', {
125 'ev-click': function (ev) {
126 if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) {
127 instance.reload()
128 }
129 },
130 href: `#${view}`,
131 classList: [
132 when(selected(view), '-selected')
133 ]
134 }, [
135 name,
136 when(instance.pendingUpdates, [
137 ' (', instance.pendingUpdates, ')'
138 ])
139 ])
140 }
141
142 function goBack () {
143 if (backHistory.length) {
144 canGoForward.set(true)
145 forwardHistory.push(currentView())
146 currentView.set(backHistory.pop())
147 canGoBack.set(backHistory.length > 0)
148 }
149 }
150
151 function goForward () {
152 if (forwardHistory.length) {
153 backHistory.push(currentView())
154 currentView.set(forwardHistory.pop())
155 canGoForward.set(forwardHistory.length > 0)
156 canGoBack.set(true)
157 }
158 }
159
160 function setView (view) {
161 if (!views.has(view)) {
162 views.put(view, screenView(view))
163 }
164
165 if (lastViewed[view] !== true) {
166 lastViewed[view] = Date.now()
167 }
168
169 if (currentView() && lastViewed[currentView()] !== true) {
170 lastViewed[currentView()] = Date.now()
171 }
172
173 if (view !== currentView()) {
174 canGoForward.set(false)
175 canGoBack.set(true)
176 forwardHistory.length = 0
177 backHistory.push(currentView())
178 currentView.set(view)
179 }
180 }
181
182 function doSearch () {
183 var value = searchBox.value.trim()
184 if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) {
185 setView(value)
186 } else if (value.trim()) {
187 setView(`?${value.trim()}`)
188 } else {
189 setView('/public')
190 }
191 }
192
193 function selected (view) {
194 return computed([currentView, view], (currentView, view) => {
195 return currentView === view
196 })
197 }
198}
199
200function isSame (a, b) {
201 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
202 for (var i = 0; i < a.length; i++) {
203 if (a[i] !== b[i]) {
204 return false
205 }
206 }
207 return true
208 } else if (a === b) {
209 return true
210 }
211
212}
213
214
215

Built with git-ssb-web