-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-workflows.js
More file actions
383 lines (358 loc) · 11.6 KB
/
Copy pathgithub-workflows.js
File metadata and controls
383 lines (358 loc) · 11.6 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
const initializeGitHubWorkflows = ({
createGitHubPrEditorSyncController,
createGitHubChatDrawer,
createGitHubPrDrawer,
createWorkspacesDrawer,
ensureJsxTransformSource,
collectTopLevelDeclarations,
cdnImports,
importFromCdnWithFallback,
githubAiContextState,
byotControls,
getCurrentGitHubToken,
getCurrentSelectedRepository,
aiChatToggle,
aiChatDrawer,
aiChatClose,
aiChatPrompt,
aiChatModel,
aiChatIncludeEditors,
aiChatSend,
aiChatClear,
aiChatStatus,
aiChatRepository,
aiChatMessages,
githubPrToggle,
githubPrDrawer,
githubPrClose,
githubPrRepoSelect,
githubPrBaseBranch,
githubPrHeadBranch,
githubPrTitle,
githubPrBody,
githubPrCommitMessage,
githubPrIncludeAppWrapper,
githubPrSubmit,
openPrTitle,
githubPrStatus,
workspacesToggle,
workspacesDrawer,
workspacesClose,
workspacesStatus,
workspacesSearch,
workspacesSelect,
workspacesOpen,
workspacesRemove,
workspaceStorage,
getActiveWorkspaceRecordId,
setActiveWorkspaceRecordId,
setActiveWorkspaceCreatedAt,
listLocalContextRecords,
refreshLocalContextOptions,
applyWorkspaceRecord,
getWorkspacePrFileCommits,
getEditorSyncTargets,
getRenderMode,
getStyleMode,
setCurrentSelectedRepository,
reconcileWorkspaceTabsWithPushUpdates,
getActivePrContextSyncKey,
prContextUi,
onPrContextStateChange,
onPrContextClosed,
onPrContextDisconnected,
getTokenForVisibility,
closeWorkspacesDrawer,
getActivePrEditorSyncKey,
syncFromActiveContext,
applyRenderMode,
applyStyleMode,
formatActivePrReference,
githubPrContextClose,
githubPrContextDisconnect,
confirmAction,
setStatus,
showAppToast,
setComponentSource,
setStylesSource,
getComponentSource,
getStylesSource,
scheduleRender,
}) => {
const getCurrentWritableRepositories = () =>
githubAiContextState.writableRepositories.length > 0
? [...githubAiContextState.writableRepositories]
: byotControls.getWritableRepositories()
const getTopLevelDeclarations = async source => {
if (typeof source !== 'string' || !source.trim()) {
return []
}
const transformJsxSource = await ensureJsxTransformSource({
cdnImports,
importFromCdnWithFallback,
})
return collectTopLevelDeclarations({ source, transformJsxSource })
}
const prEditorSyncController = createGitHubPrEditorSyncController({
setComponentSource: value => {
setComponentSource(value)
},
setStylesSource: value => {
setStylesSource(value)
},
scheduleRender,
})
const chatDrawerController = createGitHubChatDrawer({
toggleButton: aiChatToggle,
drawer: aiChatDrawer,
closeButton: aiChatClose,
promptInput: aiChatPrompt,
modelSelect: aiChatModel,
includeEditorsContextToggle: aiChatIncludeEditors,
sendButton: aiChatSend,
clearButton: aiChatClear,
statusNode: aiChatStatus,
repositoryNode: aiChatRepository,
messagesNode: aiChatMessages,
getToken: getCurrentGitHubToken,
getSelectedRepository: getCurrentSelectedRepository,
getComponentSource,
setComponentSource,
getStylesSource,
setStylesSource,
scheduleRender,
getRenderMode,
getStyleMode,
getDrawerSide: () => {
return 'right'
},
})
const prDrawerController = createGitHubPrDrawer({
toggleButton: githubPrToggle,
drawer: githubPrDrawer,
closeButton: githubPrClose,
repositorySelect: githubPrRepoSelect,
baseBranchInput: githubPrBaseBranch,
headBranchInput: githubPrHeadBranch,
prTitleInput: githubPrTitle,
prBodyInput: githubPrBody,
commitMessageInput: githubPrCommitMessage,
includeAppWrapperToggle: githubPrIncludeAppWrapper,
submitButton: githubPrSubmit,
titleNode: openPrTitle,
statusNode: githubPrStatus,
getToken: getCurrentGitHubToken,
getSelectedRepository: getCurrentSelectedRepository,
getWritableRepositories: getCurrentWritableRepositories,
setSelectedRepository: setCurrentSelectedRepository,
getFileCommits: getWorkspacePrFileCommits,
getEditorSyncTargets,
getTopLevelDeclarations,
getRenderMode,
getStyleMode,
getDrawerSide: () => {
return 'right'
},
confirmBeforeSubmit: options => {
confirmAction(options)
},
onPullRequestOpened: ({ url, fileUpdates }) => {
const activeContextSyncKey = getActivePrContextSyncKey(
githubAiContextState.activePrContext,
)
if (activeContextSyncKey && activeContextSyncKey === getActivePrEditorSyncKey()) {
prContextUi.markActivePrEditorContentSynced()
}
const message = url
? `Pull request opened: ${url}`
: 'Pull request opened successfully.'
reconcileWorkspaceTabsWithPushUpdates(fileUpdates)
showAppToast(message)
},
onPullRequestCommitPushed: ({ branch, fileUpdates }) => {
reconcileWorkspaceTabsWithPushUpdates(fileUpdates)
const fileCount = Array.isArray(fileUpdates) ? fileUpdates.length : 0
const message =
fileCount > 0
? `Pushed commit to ${branch} (${fileCount} file${fileCount === 1 ? '' : 's'}).`
: `Pushed commit to ${branch}.`
showAppToast(message)
},
onActivePrContextChange: activeContext => {
prContextUi.setActivePrContext(activeContext)
prContextUi.syncAiChatTokenVisibility(getTokenForVisibility())
if (activeContext) {
closeWorkspacesDrawer()
}
if (typeof onPrContextStateChange === 'function') {
onPrContextStateChange(activeContext)
}
},
onSyncActivePrEditorContent: async args => {
const result = await prEditorSyncController.syncFromActiveContext(args)
const syncedContextKey = getActivePrContextSyncKey(args?.activeContext)
if (!syncedContextKey || syncedContextKey !== getActivePrEditorSyncKey()) {
return result
}
if (result?.synced === true) {
prContextUi.markActivePrEditorContentSynced()
syncFromActiveContext({
tabTargets: args?.syncTargets?.tabTargets,
})
}
return result
},
onRestoreRenderMode: mode => {
applyRenderMode({ mode, fromActivePrContext: true })
},
onRestoreStyleMode: mode => {
applyStyleMode({ mode })
},
})
const workspacesDrawerController = createWorkspacesDrawer({
toggleButton: workspacesToggle,
drawer: workspacesDrawer,
closeButton: workspacesClose,
statusNode: workspacesStatus,
searchInput: workspacesSearch,
selectInput: workspacesSelect,
openButton: workspacesOpen,
removeButton: workspacesRemove,
getDrawerSide: () => {
return 'right'
},
onRefreshRequested: listLocalContextRecords,
onOpenSelected: async workspaceId => {
try {
const record = await workspaceStorage.getWorkspaceById(workspaceId)
if (!record) {
await refreshLocalContextOptions()
workspacesDrawerController?.setStatus(
'Stored local context no longer exists.',
'error',
)
return false
}
return applyWorkspaceRecord(record, { silent: false })
} catch {
workspacesDrawerController?.setStatus(
'Could not load selected local context.',
'error',
)
return false
}
},
onRemoveSelected: async workspaceId => {
confirmAction({
title: 'Remove stored local context?',
copy: 'This removes only local workspace metadata and editor content from this browser.',
confirmButtonText: 'Remove',
onConfirm: () => {
void workspaceStorage
.removeWorkspace(workspaceId)
.then(async () => {
if (getActiveWorkspaceRecordId() === workspaceId) {
setActiveWorkspaceRecordId('')
setActiveWorkspaceCreatedAt(null)
}
await refreshLocalContextOptions()
workspacesDrawerController?.setStatus(
'Removed stored local context.',
'neutral',
)
})
.catch(() => {
workspacesDrawerController?.setStatus(
'Could not remove stored local context.',
'error',
)
})
},
})
return false
},
})
prDrawerController.setToken(githubAiContextState.token)
prDrawerController.setSelectedRepository(githubAiContextState.selectedRepository)
prDrawerController.syncRepositories()
prContextUi.setActivePrContext(prDrawerController.getActivePrContext())
if (typeof onPrContextStateChange === 'function') {
onPrContextStateChange(prDrawerController.getActivePrContext())
}
githubPrContextClose?.addEventListener('click', () => {
if (!githubAiContextState.activePrContext) {
return
}
const activePrReference = formatActivePrReference(
githubAiContextState.activePrContext,
)
const referenceLine = activePrReference ? `PR: ${activePrReference}\n` : ''
confirmAction({
title: 'Close pull request on GitHub?',
copy: `${referenceLine}PR title: ${githubAiContextState.activePrContext.prTitle}\nHead branch: ${githubAiContextState.activePrContext.headBranch}\n\nThis will close the pull request on GitHub and clear the active pull request context for the selected repository.`,
confirmButtonText: 'Close PR on GitHub',
onConfirm: () => {
void prDrawerController
.closeActivePullRequestOnGitHub()
.then(result => {
const reference = result?.reference
setStatus(
reference
? `Closed pull request on GitHub and cleared active context (${reference}).`
: 'Closed pull request on GitHub and cleared active context.',
'neutral',
)
if (typeof onPrContextClosed === 'function') {
onPrContextClosed(result)
}
showAppToast(
reference
? `Closed pull request on GitHub and cleared active context (${reference}).`
: 'Closed pull request on GitHub and cleared active context.',
)
})
.catch(error => {
const message =
error instanceof Error
? error.message
: 'Could not close pull request context on GitHub.'
setStatus(`Close context failed: ${message}`, 'error')
showAppToast(`Close context failed: ${message}`)
})
},
})
})
githubPrContextDisconnect?.addEventListener('click', () => {
if (!githubAiContextState.activePrContext) {
return
}
const activePrReference = formatActivePrReference(
githubAiContextState.activePrContext,
)
const referenceLine = activePrReference ? `PR: ${activePrReference}\n` : ''
confirmAction({
title: 'Disconnect PR context?',
copy: `${referenceLine}This will disconnect the active pull request context in this app only.\nYour pull request will stay open on GitHub.\nYour GitHub token and selected repository will stay connected.`,
confirmButtonText: 'Disconnect',
onConfirm: () => {
const result = prDrawerController.disconnectActivePrContext()
const reference = result?.reference
setStatus(
reference
? `Disconnected PR context (${reference}). Pull request remains open on GitHub.`
: 'Disconnected PR context. Pull request remains open on GitHub.',
'neutral',
)
if (typeof onPrContextDisconnected === 'function') {
onPrContextDisconnected(result)
}
},
})
})
return {
chatDrawerController,
prDrawerController,
workspacesDrawerController,
}
}
export { initializeGitHubWorkflows }