-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
index.tsx
349 lines (299 loc) · 10.5 KB
/
index.tsx
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
import '../lib/logging/renderer/install'
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import * as Path from 'path'
import * as moment from 'moment'
import { ipcRenderer, remote } from 'electron'
import { App } from './app'
import {
Dispatcher,
gitAuthenticationErrorHandler,
externalEditorErrorHandler,
openShellErrorHandler,
mergeConflictHandler,
lfsAttributeMismatchHandler,
defaultErrorHandler,
missingRepositoryHandler,
backgroundTaskHandler,
pushNeedsPullHandler,
upstreamAlreadyExistsHandler,
rebaseConflictsHandler,
localChangesOverwrittenHandler,
refusedWorkflowUpdate,
samlReauthRequired,
insufficientGitHubRepoPermissions,
schannelUnableToCheckRevocationForCertificate,
gitCloneErrorHandler,
} from './dispatcher'
import {
AppStore,
GitHubUserStore,
CloningRepositoriesStore,
IssuesStore,
SignInStore,
RepositoriesStore,
TokenStore,
AccountsStore,
PullRequestStore,
} from '../lib/stores'
import { GitHubUserDatabase } from '../lib/databases'
import { URLActionType } from '../lib/parse-app-url'
import { SelectionType, IAppState } from '../lib/app-state'
import { StatsDatabase, StatsStore } from '../lib/stats'
import {
IssuesDatabase,
RepositoriesDatabase,
PullRequestDatabase,
} from '../lib/databases'
import { shellNeedsPatching, updateEnvironmentForProcess } from '../lib/shell'
import { installDevGlobals } from './install-globals'
import { reportUncaughtException, sendErrorReport } from './main-process-proxy'
import { getOS } from '../lib/get-os'
import { getGUID } from '../lib/stats'
import {
enableSourceMaps,
withSourceMappedStack,
} from '../lib/source-map-support'
import { UiActivityMonitor } from './lib/ui-activity-monitor'
import { RepositoryStateCache } from '../lib/stores/repository-state-cache'
import { ApiRepositoriesStore } from '../lib/stores/api-repositories-store'
import { CommitStatusStore } from '../lib/stores/commit-status-store'
import { PullRequestCoordinator } from '../lib/stores/pull-request-coordinator'
if (__DEV__) {
installDevGlobals()
}
if (shellNeedsPatching(process)) {
updateEnvironmentForProcess()
}
enableSourceMaps()
// Tell dugite where to find the git environment,
// see https://github.com/desktop/dugite/pull/85
process.env['LOCAL_GIT_DIRECTORY'] = Path.resolve(__dirname, 'git')
// We're using a polyfill for the upcoming CSS4 `:focus-ring` pseudo-selector.
// This allows us to not have to override default accessibility driven focus
// styles for buttons in the case when a user clicks on a button. This also
// gives better visiblity to individuals who navigate with the keyboard.
//
// See:
// https://github.com/WICG/focus-ring
// Focus Ring! -- A11ycasts #16: https://youtu.be/ilj2P5-5CjI
require('wicg-focus-ring')
// setup this moment.js plugin so we can use easier
// syntax for formatting time duration
const momentDurationFormatSetup = require('moment-duration-format')
momentDurationFormatSetup(moment)
const startTime = performance.now()
if (!process.env.TEST_ENV) {
/* This is the magic trigger for webpack to go compile
* our sass into css and inject it into the DOM. */
require('../../styles/desktop.scss')
}
let currentState: IAppState | null = null
let lastUnhandledRejection: string | null = null
let lastUnhandledRejectionTime: Date | null = null
const sendErrorWithContext = (
error: Error,
context: { [key: string]: string } = {},
nonFatal?: boolean
) => {
error = withSourceMappedStack(error)
console.error('Uncaught exception', error)
if (__DEV__ || process.env.TEST_ENV) {
console.error(
`An uncaught exception was thrown. If this were a production build it would be reported to Central. Instead, maybe give it a lil lookyloo.`
)
} else {
const extra: Record<string, string> = {
osVersion: getOS(),
guid: getGUID(),
...context,
}
try {
if (currentState) {
if (currentState.currentBanner !== null) {
extra.currentBanner = currentState.currentBanner.type
}
if (currentState.currentPopup !== null) {
extra.currentPopup = `${currentState.currentPopup.type}`
}
if (currentState.selectedState !== null) {
extra.selectedState = `${currentState.selectedState.type}`
if (currentState.selectedState.type === SelectionType.Repository) {
extra.selectedRepositorySection = `${
currentState.selectedState.state.selectedSection
}`
}
}
if (currentState.currentFoldout !== null) {
extra.currentFoldout = `${currentState.currentFoldout.type}`
}
if (currentState.showWelcomeFlow) {
extra.inWelcomeFlow = 'true'
}
if (currentState.windowZoomFactor !== 1) {
extra.windowZoomFactor = `${currentState.windowZoomFactor}`
}
if (currentState.errors.length > 0) {
extra.activeAppErrors = `${currentState.errors.length}`
}
if (
lastUnhandledRejection !== null &&
lastUnhandledRejectionTime !== null
) {
extra.lastUnhandledRejection = lastUnhandledRejection
extra.lastUnhandledRejectionTime = lastUnhandledRejectionTime.toString()
}
extra.repositoryCount = `${currentState.repositories.length}`
extra.windowState = currentState.windowState
extra.accounts = `${currentState.accounts.length}`
if (__DARWIN__) {
extra.automaticallySwitchTheme = `${
currentState.automaticallySwitchTheme
}`
}
}
} catch (err) {
/* ignore */
}
sendErrorReport(error, extra, nonFatal)
}
}
process.once('uncaughtException', (error: Error) => {
sendErrorWithContext(error)
reportUncaughtException(error)
})
// See sendNonFatalException for more information
process.on(
'send-non-fatal-exception',
(error: Error, context?: { [key: string]: string }) => {
sendErrorWithContext(error, context, true)
}
)
/**
* Chromium won't crash on an unhandled rejection (similar to how
* it won't crash on an unhandled error). We've taken the approach
* that unhandled errors should crash the app and very likely we
* should do the same thing for unhandled promise rejections but
* that's a bit too risky to do until we've established some sense
* of how often it happens. For now this simply stores the last
* rejection so that we can pass it along with the crash report
* if the app does crash. Note that this does not prevent the
* default browser behavior of logging since we're not calling
* `preventDefault` on the event.
*
* See https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event
*/
window.addEventListener('unhandledrejection', ev => {
if (ev.reason !== null && ev.reason !== undefined) {
try {
lastUnhandledRejection = `${ev.reason}`
lastUnhandledRejectionTime = new Date()
} catch (err) {
/* ignore */
}
}
})
const gitHubUserStore = new GitHubUserStore(
new GitHubUserDatabase('GitHubUserDatabase')
)
const cloningRepositoriesStore = new CloningRepositoriesStore()
const issuesStore = new IssuesStore(new IssuesDatabase('IssuesDatabase'))
const statsStore = new StatsStore(
new StatsDatabase('StatsDatabase'),
new UiActivityMonitor()
)
const signInStore = new SignInStore()
const accountsStore = new AccountsStore(localStorage, TokenStore)
const repositoriesStore = new RepositoriesStore(
new RepositoriesDatabase('Database')
)
const pullRequestStore = new PullRequestStore(
new PullRequestDatabase('PullRequestDatabase'),
repositoriesStore
)
const pullRequestCoordinator = new PullRequestCoordinator(
pullRequestStore,
repositoriesStore
)
const repositoryStateManager = new RepositoryStateCache(repo =>
gitHubUserStore.getUsersForRepository(repo)
)
const apiRepositoriesStore = new ApiRepositoriesStore(accountsStore)
const commitStatusStore = new CommitStatusStore(accountsStore)
const appStore = new AppStore(
gitHubUserStore,
cloningRepositoriesStore,
issuesStore,
statsStore,
signInStore,
accountsStore,
repositoriesStore,
pullRequestCoordinator,
repositoryStateManager,
apiRepositoriesStore
)
appStore.onDidUpdate(state => {
currentState = state
})
const dispatcher = new Dispatcher(
appStore,
repositoryStateManager,
statsStore,
commitStatusStore
)
dispatcher.registerErrorHandler(defaultErrorHandler)
dispatcher.registerErrorHandler(gitCloneErrorHandler)
dispatcher.registerErrorHandler(upstreamAlreadyExistsHandler)
dispatcher.registerErrorHandler(externalEditorErrorHandler)
dispatcher.registerErrorHandler(openShellErrorHandler)
dispatcher.registerErrorHandler(mergeConflictHandler)
dispatcher.registerErrorHandler(lfsAttributeMismatchHandler)
dispatcher.registerErrorHandler(insufficientGitHubRepoPermissions)
dispatcher.registerErrorHandler(schannelUnableToCheckRevocationForCertificate)
dispatcher.registerErrorHandler(gitAuthenticationErrorHandler)
dispatcher.registerErrorHandler(pushNeedsPullHandler)
dispatcher.registerErrorHandler(samlReauthRequired)
dispatcher.registerErrorHandler(backgroundTaskHandler)
dispatcher.registerErrorHandler(missingRepositoryHandler)
dispatcher.registerErrorHandler(localChangesOverwrittenHandler)
dispatcher.registerErrorHandler(rebaseConflictsHandler)
dispatcher.registerErrorHandler(refusedWorkflowUpdate)
document.body.classList.add(`platform-${process.platform}`)
dispatcher.setAppFocusState(remote.getCurrentWindow().isFocused())
ipcRenderer.on('focus', async () => {
const { selectedState } = appStore.getState()
// Refresh the currently selected repository on focus (if
// we have a selected repository, that is not cloning).
if (
selectedState &&
!(selectedState.type === SelectionType.CloningRepository)
) {
await dispatcher.refreshTags(selectedState.repository)
await dispatcher.refreshRepository(selectedState.repository)
}
dispatcher.setAppFocusState(true)
})
ipcRenderer.on('blur', () => {
// Make sure we stop highlighting the menu button (on non-macOS)
// when someone uses Alt+Tab to switch application since we won't
// get the onKeyUp event for the Alt key in that case.
dispatcher.setAccessKeyHighlightState(false)
dispatcher.setAppFocusState(false)
})
ipcRenderer.on(
'url-action',
(event: Electron.IpcRendererEvent, { action }: { action: URLActionType }) => {
dispatcher.dispatchURLAction(action)
}
)
ReactDOM.render(
<App
dispatcher={dispatcher}
appStore={appStore}
repositoryStateManager={repositoryStateManager}
issuesStore={issuesStore}
gitHubUserStore={gitHubUserStore}
startTime={startTime}
/>,
document.getElementById('desktop-app-container')!
)