-
-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathFooter.tsx
More file actions
462 lines (422 loc) · 15.4 KB
/
Footer.tsx
File metadata and controls
462 lines (422 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import { WebApplication } from '@/Application/WebApplication'
import { WebApplicationGroup } from '@/Application/WebApplicationGroup'
import { AbstractComponent } from '@/Components/Abstract/PureComponent'
import { destroyAllObjectProperties, preventRefreshing } from '@/Utils'
import {
ApplicationEvent,
ApplicationDescriptor,
WebAppEvent,
StatusServiceEvent,
HttpErrorResponseBody,
getErrorMessageFromErrorResponseBody,
PreferencePaneId,
} from '@standardnotes/snjs'
import {
STRING_NEW_UPDATE_READY,
STRING_CONFIRM_APP_QUIT_DURING_UPGRADE,
STRING_UPGRADE_ACCOUNT_CONFIRM_TEXT,
STRING_UPGRADE_ACCOUNT_CONFIRM_TITLE,
STRING_UPGRADE_ACCOUNT_CONFIRM_BUTTON,
} from '@/Constants/Strings'
import { alertDialog, confirmDialog } from '@standardnotes/ui-services'
import Icon from '@/Components/Icon/Icon'
import SyncResolutionMenu from '@/Components/SyncResolutionMenu/SyncResolutionMenu'
import { Fragment } from 'react'
import { AccountMenuPane } from '../AccountMenu/AccountMenuPane'
import { EditorEventSource } from '@/Types/EditorEventSource'
import QuickSettingsButton from './QuickSettingsButton'
import AccountMenuButton from './AccountMenuButton'
import StyledTooltip from '../StyledTooltip/StyledTooltip'
import UpgradeNow from './UpgradeNow'
import PreferencesButton from './PreferencesButton'
import VaultSelectionButton from './VaultSelectionButton'
type Props = {
application: WebApplication
applicationGroup: WebApplicationGroup
}
type State = {
outOfSync: boolean
dataUpgradeAvailable: boolean
hasPasscode: boolean
descriptors: ApplicationDescriptor[]
showBetaWarning: boolean
showSyncResolution: boolean
newUpdateAvailable: boolean
offline: boolean
hasError: boolean
arbitraryStatusMessage?: string
failedSyncError?: string
}
class Footer extends AbstractComponent<Props, State> {
public user?: unknown
private didCheckForOffline = false
private completedInitialSync = false
private showingDownloadStatus = false
private webEventListenerDestroyer: () => void
private removeStatusObserver!: () => void
constructor(props: Props) {
super(props, props.application)
this.state = {
hasError: false,
offline: true,
outOfSync: false,
dataUpgradeAvailable: false,
hasPasscode: false,
descriptors: props.applicationGroup.getDescriptors(),
showBetaWarning: false,
showSyncResolution: false,
newUpdateAvailable: false,
}
this.webEventListenerDestroyer = props.application.addWebEventObserver((event, data) => {
const statusService = this.application.status
switch (event) {
case WebAppEvent.NewUpdateAvailable:
this.onNewUpdateAvailable()
break
case WebAppEvent.EditorDidFocus:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((data as any).eventSource === EditorEventSource.UserInteraction) {
this.closeAccountMenu()
}
break
case WebAppEvent.BeganBackupDownload:
statusService.setMessage('Saving local backup…')
break
case WebAppEvent.EndedBackupDownload: {
const successMessage = 'Successfully saved backup.'
const errorMessage = 'Unable to save local backup.'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
statusService.setMessage((data as any).success ? successMessage : errorMessage)
const twoSeconds = 2000
setTimeout(() => {
if (statusService.message === successMessage || statusService.message === errorMessage) {
statusService.setMessage('')
}
}, twoSeconds)
break
}
}
})
}
override deinit() {
this.removeStatusObserver()
;(this.removeStatusObserver as unknown) = undefined
this.webEventListenerDestroyer()
;(this.webEventListenerDestroyer as unknown) = undefined
super.deinit()
destroyAllObjectProperties(this)
}
override componentDidMount(): void {
super.componentDidMount()
this.removeStatusObserver = this.application.status.addEventObserver((event, message) => {
if (event !== StatusServiceEvent.MessageChanged) {
return
}
this.setState({
arbitraryStatusMessage: message,
})
})
}
reloadUpgradeStatus() {
this.application
.checkForSecurityUpdate()
.then((available) => {
this.setState({
dataUpgradeAvailable: available,
})
})
.catch(console.error)
}
override async onAppLaunch() {
super.onAppLaunch().catch(console.error)
this.reloadPasscodeStatus().catch(console.error)
this.reloadUser()
this.reloadUpgradeStatus()
this.updateOfflineStatus()
this.findErrors()
}
reloadUser() {
this.user = this.application.sessions.getUser()
}
async reloadPasscodeStatus() {
const hasPasscode = this.application.hasPasscode()
this.setState({
hasPasscode: hasPasscode,
})
}
override async onAppKeyChange() {
super.onAppKeyChange().catch(console.error)
this.reloadPasscodeStatus().catch(console.error)
}
override onAppEvent(eventName: ApplicationEvent, data?: unknown) {
switch (eventName) {
case ApplicationEvent.KeyStatusChanged:
this.reloadUpgradeStatus()
break
case ApplicationEvent.EnteredOutOfSync:
this.setState({
outOfSync: true,
})
break
case ApplicationEvent.ExitedOutOfSync:
this.setState({
outOfSync: false,
})
break
case ApplicationEvent.CompletedFullSync:
if (!this.completedInitialSync) {
this.application.status.setMessage('')
this.completedInitialSync = true
}
if (!this.didCheckForOffline) {
this.didCheckForOffline = true
if (this.state.offline && this.application.items.getNoteCount() === 0) {
this.application.accountMenuController.setShow(true)
}
}
this.findErrors()
this.updateOfflineStatus()
this.setState({
failedSyncError: undefined,
})
break
case ApplicationEvent.SyncStatusChanged:
this.updateSyncStatus()
break
case ApplicationEvent.FailedSync:
this.updateSyncStatus()
this.findErrors()
this.updateOfflineStatus()
this.setState({
failedSyncError: getErrorMessageFromErrorResponseBody(
data as HttpErrorResponseBody,
'Sync error. Please try again later.',
),
})
break
case ApplicationEvent.LocalDataIncrementalLoad:
case ApplicationEvent.LocalDataLoaded:
this.updateLocalDataStatus()
break
case ApplicationEvent.SignedIn:
case ApplicationEvent.SignedOut:
this.reloadUser()
break
case ApplicationEvent.WillSync:
if (!this.completedInitialSync) {
this.application.status.setMessage('Syncing…')
}
break
}
}
updateSyncStatus() {
const statusManager = this.application.status
const syncStatus = this.application.sync.getSyncStatus()
const stats = syncStatus.getStats()
if (syncStatus.hasError()) {
statusManager.setMessage('Unable to Sync')
} else if (stats.downloadCount > 20) {
const text = `Downloading ${stats.downloadCount} items. Keep app open.`
statusManager.setMessage(text)
this.showingDownloadStatus = true
} else if (this.showingDownloadStatus) {
this.showingDownloadStatus = false
statusManager.setMessage('Download Complete.')
setTimeout(() => {
statusManager.setMessage('')
}, 2000)
} else if (stats.uploadTotalCount > 20) {
const completionPercentage =
stats.uploadCompletionCount === 0 ? 0 : stats.uploadCompletionCount / stats.uploadTotalCount
const stringPercentage = completionPercentage.toLocaleString(undefined, {
style: 'percent',
})
statusManager.setMessage(`Syncing ${stats.uploadTotalCount} items (${stringPercentage} complete)`)
} else {
statusManager.setMessage('')
}
}
updateLocalDataStatus() {
const statusManager = this.application.status
const syncStatus = this.application.sync.getSyncStatus()
const stats = syncStatus.getStats()
const encryption = this.application.isEncryptionAvailable()
if (stats.localDataDone) {
statusManager.setMessage('')
return
}
const notesString = `${stats.localDataCurrent}/${stats.localDataTotal} items...`
const loadingStatus = encryption ? `Decrypting ${notesString}` : `Loading ${notesString}`
statusManager.setMessage(loadingStatus)
}
updateOfflineStatus() {
this.setState({
offline: this.application.sessions.isSignedOut(),
})
}
findErrors() {
this.setState({
hasError: this.application.sync.getSyncStatus().hasError(),
})
}
securityUpdateClickHandler = async () => {
if (
await confirmDialog({
title: STRING_UPGRADE_ACCOUNT_CONFIRM_TITLE,
text: STRING_UPGRADE_ACCOUNT_CONFIRM_TEXT,
confirmButtonText: STRING_UPGRADE_ACCOUNT_CONFIRM_BUTTON,
})
) {
preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_UPGRADE, async () => {
await this.application.upgradeProtocolVersion()
}).catch(console.error)
}
}
accountMenuClickHandler = () => {
this.application.accountMenuController.toggleShow()
}
syncResolutionClickHandler = () => {
this.setState({
showSyncResolution: !this.state.showSyncResolution,
})
}
closeAccountMenu = () => {
this.application.accountMenuController.setShow(false)
this.application.accountMenuController.setCurrentPane(AccountMenuPane.GeneralMenu)
}
lockClickHandler = () => {
this.application.lock().catch(console.error)
}
onNewUpdateAvailable = () => {
this.setState({
newUpdateAvailable: true,
})
}
newUpdateClickHandler = () => {
this.setState({
newUpdateAvailable: false,
})
this.application.alerts.alert(STRING_NEW_UPDATE_READY).catch(console.error)
}
betaMessageClickHandler = () => {
alertDialog({
title: 'You are using a beta version of the app',
text: 'If you wish to go back to a stable version, make sure to sign out ' + 'of this beta app first.',
}).catch(console.error)
}
clickOutsideAccountMenu = () => {
this.application.accountMenuController.closeAccountMenu()
}
openPreferences = (openWhatsNew: boolean) => {
if (openWhatsNew) {
this.application.preferencesController.setCurrentPane(PreferencePaneId.WhatsNew)
}
this.application.preferencesController.openPreferences()
}
override render() {
return (
<div className="sn-component">
<footer
id="footer-bar"
className="z-footer-bar hidden h-8 w-full select-none items-center justify-between border-t border-border bg-contrast px-3 text-text md:flex"
>
<div className="left flex h-full flex-shrink-0">
<div className="sk-app-bar-item relative z-footer-bar-item ml-0 select-none">
<AccountMenuButton
hasError={this.state.hasError}
controller={this.application.accountMenuController}
mainApplicationGroup={this.props.applicationGroup}
onClickOutside={this.clickOutsideAccountMenu}
toggleMenu={this.accountMenuClickHandler}
user={this.user}
/>
</div>
<div className="relative z-footer-bar-item select-none">
<PreferencesButton openPreferences={this.openPreferences} />
</div>
<div className="relative z-footer-bar-item select-none">
<QuickSettingsButton application={this.application} />
</div>
<div className="relative z-footer-bar-item ml-1.5 select-none">
<VaultSelectionButton />
</div>
<UpgradeNow
application={this.application}
featuresController={this.application.featuresController}
subscriptionContoller={this.application.subscriptionController}
/>
{this.state.showBetaWarning && (
<Fragment>
<div className="relative z-footer-bar-item ml-3 flex select-none items-center border-l border-solid border-border pl-3">
<a onClick={this.betaMessageClickHandler} className="no-decoration title text-xs font-bold">
You are using a beta version of the app
</a>
</div>
</Fragment>
)}
</div>
<div className="center max-h-full overflow-hidden px-4">
{this.state.arbitraryStatusMessage && (
<div className="relative z-footer-bar-item max-h-full select-none items-center overflow-hidden text-ellipsis whitespace-nowrap text-xs font-bold text-neutral">
{this.state.arbitraryStatusMessage}
</div>
)}
</div>
<div className="right flex h-full flex-shrink-0">
{this.state.failedSyncError && (
<div className="relative z-footer-bar-item flex select-none items-center text-xs font-bold text-neutral">
Sync error: {this.state.failedSyncError}
</div>
)}
{this.state.dataUpgradeAvailable && (
<div
onClick={this.securityUpdateClickHandler}
className="relative z-footer-bar-item flex select-none items-center text-xs font-bold text-success"
>
Encryption upgrade available.
</div>
)}
{this.state.newUpdateAvailable && (
<div
onClick={this.newUpdateClickHandler}
className="relative z-footer-bar-item ml-3 flex select-none items-center text-xs font-bold text-info"
>
New update available.
</div>
)}
{(this.state.outOfSync || this.state.showSyncResolution) && (
<div className="relative z-footer-bar-item ml-3 flex flex-shrink-0 select-none items-center">
{this.state.outOfSync && (
<div onClick={this.syncResolutionClickHandler} className="text-xs font-bold text-warning">
Potentially Out of Sync
</div>
)}
{this.state.showSyncResolution && (
<SyncResolutionMenu close={this.syncResolutionClickHandler} application={this.application} />
)}
</div>
)}
{this.state.offline && (
<div className="relative z-footer-bar-item ml-3 flex flex-shrink-0 select-none items-center text-xs font-bold">
Offline
</div>
)}
{this.state.hasPasscode && (
<StyledTooltip label="Lock application">
<div
id="lock-item"
onClick={this.lockClickHandler}
title="Locks application and wipes unencrypted data from memory."
className="relative z-footer-bar-item ml-3 flex cursor-pointer select-none items-center border-l border-solid border-border pl-2 hover:text-info"
>
<Icon type="lock-filled" size="custom" className="h-4.5 w-4.5" />
</div>
</StyledTooltip>
)}
</div>
</footer>
</div>
)
}
}
export default Footer