-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawer.js
More file actions
603 lines (501 loc) · 16.4 KB
/
Copy pathdrawer.js
File metadata and controls
603 lines (501 loc) · 16.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
const toSafeText = value => (typeof value === 'string' ? value.trim() : '')
const localRepositoryFilterValue = '__local__'
const localWorkspaceScopeValue = 'local'
const repositoryWorkspaceScopeValue = 'repository'
const drawerUiState = {
localEmpty: 'local-empty',
localWithWorkspaces: 'local-with-workspaces',
repositoryEmpty: 'repository-empty',
repositoryWithWorkspaces: 'repository-with-workspaces',
}
const toSafeWorkspaceScope = workspace => {
const scope = toSafeText(workspace?.workspaceScope).toLowerCase()
if (scope === repositoryWorkspaceScopeValue) {
return repositoryWorkspaceScopeValue
}
if (scope === localWorkspaceScopeValue) {
return localWorkspaceScopeValue
}
return toSafeText(workspace?.repo)
? repositoryWorkspaceScopeValue
: localWorkspaceScopeValue
}
const toWorkspaceLabel = workspace => {
const hasTitle = toSafeText(workspace?.prTitle)
if (hasTitle) {
return hasTitle
}
const hasHead = toSafeText(workspace?.head)
if (hasHead) {
return hasHead
}
return toSafeText(workspace?.id) || 'workspace'
}
export const createWorkspacesDrawer = ({
toggleButton,
drawer,
closeButton,
statusNode,
repositorySelect,
getActiveWorkspaceId,
initializeButton,
newButton,
selectInput,
openButton,
renameButton,
removeButton,
getDrawerSide,
getActiveWorkspaceDisplayLabel,
getRepositoryFilterOptions,
getSelectedRepositoryFilter,
onRepositoryFilterChange,
onRefreshRequested,
onInitializeWorkspace,
onCreateWorkspace,
onOpenSelected,
onRenameSelected,
onRemoveSelected,
} = {}) => {
let open = false
let entries = []
let selectedId = ''
let selectedRepositoryFilter = localRepositoryFilterValue
let hasUserSelectedRepositoryFilter = false
let currentUiState = drawerUiState.localEmpty
const getNormalizedRepositoryFilter = value => {
const normalized = toSafeText(value)
return normalized || localRepositoryFilterValue
}
const isInactiveWithoutPrNumber = workspace => {
const state = toSafeText(workspace?.prContextState).toLowerCase()
const hasPrNumber =
typeof workspace?.prNumber === 'number' && Number.isFinite(workspace.prNumber)
return state === 'inactive' && !hasPrNumber
}
const shouldRenderAsLocalEntry = workspace => {
if (toSafeWorkspaceScope(workspace) === localWorkspaceScopeValue) {
return true
}
const activeWorkspaceId =
typeof getActiveWorkspaceId === 'function'
? toSafeText(getActiveWorkspaceId())
: toSafeText(selectedId)
return (
toSafeText(workspace?.id) === activeWorkspaceId &&
isInactiveWithoutPrNumber(workspace)
)
}
const getFilteredEntriesByRepository = () => {
const normalizedRepositoryFilter = getNormalizedRepositoryFilter(
selectedRepositoryFilter,
)
if (normalizedRepositoryFilter === localRepositoryFilterValue) {
return entries.filter(entry => shouldRenderAsLocalEntry(entry))
}
return entries.filter(entry => {
if (toSafeText(entry?.repo) !== normalizedRepositoryFilter) {
return false
}
if (toSafeWorkspaceScope(entry) !== repositoryWorkspaceScopeValue) {
return false
}
return true
})
}
const getUiState = ({ repositoryFilter, hasStoredWorkspaces }) => {
const isLocalScope = repositoryFilter === localRepositoryFilterValue
if (isLocalScope) {
return hasStoredWorkspaces
? drawerUiState.localWithWorkspaces
: drawerUiState.localEmpty
}
return hasStoredWorkspaces
? drawerUiState.repositoryWithWorkspaces
: drawerUiState.repositoryEmpty
}
const setStatus = (text, level = 'neutral') => {
if (!(statusNode instanceof HTMLElement)) {
return
}
statusNode.textContent = text
statusNode.dataset.level = level
}
const updateActions = () => {
const normalizedSelectedId =
selectInput instanceof HTMLSelectElement
? toSafeText(selectInput.value)
: toSafeText(selectedId)
const activeWorkspaceId =
typeof getActiveWorkspaceId === 'function' ? toSafeText(getActiveWorkspaceId()) : ''
const hasSelection = normalizedSelectedId.length > 0
const selectedEntry = entries.find(
entry => toSafeText(entry?.id) === normalizedSelectedId,
)
const selectedWorkspaceScope = toSafeWorkspaceScope(selectedEntry)
const isSelectedLocalWorkspace =
hasSelection && selectedWorkspaceScope === localWorkspaceScopeValue
const isSelectedWorkspaceActive =
hasSelection &&
Boolean(activeWorkspaceId) &&
normalizedSelectedId === activeWorkspaceId
const canRenameWorkspace =
typeof onRenameSelected === 'function' &&
isSelectedLocalWorkspace &&
!isSelectedWorkspaceActive
const canCreateWorkspace = typeof onCreateWorkspace === 'function'
const canInitializeWorkspace = typeof onInitializeWorkspace === 'function'
const hasStoredWorkspaces =
currentUiState === drawerUiState.localWithWorkspaces ||
currentUiState === drawerUiState.repositoryWithWorkspaces
const isLocalUiState =
currentUiState === drawerUiState.localWithWorkspaces ||
currentUiState === drawerUiState.localEmpty
const showInitialize = currentUiState === drawerUiState.repositoryEmpty
const showNewWorkspace = !showInitialize
const workspaceField = selectInput?.closest('label')
if (workspaceField instanceof HTMLElement) {
workspaceField.toggleAttribute('hidden', !hasStoredWorkspaces)
}
const actionsRow =
openButton?.closest('.workspaces-drawer__actions') ??
removeButton?.closest('.workspaces-drawer__actions')
if (actionsRow instanceof HTMLElement) {
actionsRow.toggleAttribute('hidden', !hasStoredWorkspaces)
}
if (initializeButton instanceof HTMLButtonElement) {
initializeButton.toggleAttribute('hidden', !showInitialize)
initializeButton.disabled = !canInitializeWorkspace
}
if (newButton instanceof HTMLButtonElement) {
newButton.toggleAttribute('hidden', !showNewWorkspace)
newButton.disabled = !canCreateWorkspace
}
if (openButton instanceof HTMLButtonElement) {
openButton.disabled = !hasSelection
}
if (renameButton instanceof HTMLButtonElement) {
renameButton.toggleAttribute('hidden', !isLocalUiState)
renameButton.disabled = !canRenameWorkspace
}
if (removeButton instanceof HTMLButtonElement) {
removeButton.disabled = !hasSelection || isSelectedWorkspaceActive
}
}
const renderOptions = () => {
if (!(selectInput instanceof HTMLSelectElement)) {
return
}
const repositoryFilteredEntries = getFilteredEntriesByRepository()
const filteredEntries = repositoryFilteredEntries
const hasStoredWorkspaces = filteredEntries.length > 0
const normalizedRepositoryFilter = getNormalizedRepositoryFilter(
selectedRepositoryFilter,
)
currentUiState = getUiState({
repositoryFilter: normalizedRepositoryFilter,
hasStoredWorkspaces,
})
if (!hasStoredWorkspaces) {
updateActions()
return
}
selectInput.replaceChildren()
const placeholder = document.createElement('option')
placeholder.value = ''
placeholder.textContent = 'Select a stored workspace'
placeholder.disabled = true
placeholder.selected = !filteredEntries.some(entry => entry.id === selectedId)
selectInput.append(placeholder)
const activeWorkspaceId =
typeof getActiveWorkspaceId === 'function' ? toSafeText(getActiveWorkspaceId()) : ''
for (const entry of filteredEntries) {
const option = document.createElement('option')
option.value = toSafeText(entry.id)
const activeWorkspaceDisplayLabel =
option.value &&
option.value === activeWorkspaceId &&
typeof getActiveWorkspaceDisplayLabel === 'function'
? toSafeText(getActiveWorkspaceDisplayLabel(entry))
: ''
option.textContent = activeWorkspaceDisplayLabel || toWorkspaceLabel(entry)
option.selected = option.value === selectedId
selectInput.append(option)
}
const hasSelectedFilteredEntry = filteredEntries.some(
entry => entry.id === selectedId,
)
if (!hasSelectedFilteredEntry) {
selectInput.value = ''
}
updateActions()
}
const syncRepositoryFilterOptions = () => {
if (!(repositorySelect instanceof HTMLSelectElement)) {
return
}
const options =
typeof getRepositoryFilterOptions === 'function'
? getRepositoryFilterOptions()
: [{ value: localRepositoryFilterValue, label: 'Local' }]
const normalizedOptions = Array.isArray(options)
? options
.map(option => ({
value: toSafeText(option?.value),
label: toSafeText(option?.label),
}))
.filter(option => option.value && option.label)
: []
const hasLocalOption = normalizedOptions.some(
option => option.value === localRepositoryFilterValue,
)
const hasWritableRepositoryOptions = normalizedOptions.some(
option => option.value !== localRepositoryFilterValue,
)
const repositoryOptions = hasLocalOption
? normalizedOptions
: [{ value: localRepositoryFilterValue, label: 'Local' }, ...normalizedOptions]
const requestedFilter = hasUserSelectedRepositoryFilter
? selectedRepositoryFilter
: typeof getSelectedRepositoryFilter === 'function'
? getSelectedRepositoryFilter()
: selectedRepositoryFilter
const nextSelectedFilter = getNormalizedRepositoryFilter(requestedFilter)
if (!hasWritableRepositoryOptions) {
hasUserSelectedRepositoryFilter = false
}
repositorySelect.replaceChildren(
...repositoryOptions.map(option => {
const optionNode = document.createElement('option')
optionNode.value = option.value
optionNode.textContent = option.label
optionNode.selected = option.value === nextSelectedFilter
return optionNode
}),
)
repositorySelect.disabled = !hasWritableRepositoryOptions
if (!hasWritableRepositoryOptions) {
selectedRepositoryFilter = localRepositoryFilterValue
repositorySelect.value = localRepositoryFilterValue
return
}
if (repositoryOptions.some(option => option.value === nextSelectedFilter)) {
selectedRepositoryFilter = nextSelectedFilter
repositorySelect.value = nextSelectedFilter
return
}
selectedRepositoryFilter = localRepositoryFilterValue
repositorySelect.value = localRepositoryFilterValue
}
const refresh = async ({ preserveSelection = true } = {}) => {
if (typeof onRefreshRequested !== 'function') {
entries = []
selectedId = ''
renderOptions()
return entries
}
try {
const nextEntries = await onRefreshRequested()
entries = Array.isArray(nextEntries) ? nextEntries : []
} catch {
entries = []
selectedId = ''
setStatus('Could not refresh stored workspaces.', 'error')
renderOptions()
return entries
}
syncRepositoryFilterOptions()
if (!preserveSelection) {
selectedId = ''
}
if (!entries.some(entry => toSafeText(entry?.id) === selectedId)) {
const activeWorkspaceId =
typeof getActiveWorkspaceId === 'function'
? toSafeText(getActiveWorkspaceId())
: ''
const hasActiveWorkspaceEntry = entries.some(
entry => toSafeText(entry?.id) === activeWorkspaceId,
)
selectedId = hasActiveWorkspaceEntry ? activeWorkspaceId : ''
}
renderOptions()
return entries
}
const setOpen = async nextOpen => {
const nextState = nextOpen === true
open = nextState
if (
!(toggleButton instanceof HTMLButtonElement) ||
!(drawer instanceof HTMLElement)
) {
return
}
const preferredSide = getDrawerSide?.() === 'left' ? 'left' : 'right'
drawer.classList.toggle('workspaces-drawer--left', preferredSide === 'left')
drawer.classList.toggle('workspaces-drawer--right', preferredSide !== 'left')
toggleButton.setAttribute('aria-expanded', open ? 'true' : 'false')
drawer.toggleAttribute('hidden', !open)
if (!open) {
return
}
try {
await refresh()
} catch {
open = false
toggleButton.setAttribute('aria-expanded', 'false')
drawer.toggleAttribute('hidden', true)
setStatus('Could not open workspaces drawer.', 'error')
return
}
updateActions()
const workspaceField = selectInput?.closest('label')
if (workspaceField instanceof HTMLElement && !workspaceField.hasAttribute('hidden')) {
selectInput?.focus()
return
}
if (
initializeButton instanceof HTMLButtonElement &&
!initializeButton.hasAttribute('hidden')
) {
initializeButton.focus()
return
}
newButton?.focus()
}
const closeDrawer = () => {
open = false
if (toggleButton instanceof HTMLButtonElement) {
toggleButton.setAttribute('aria-expanded', 'false')
}
if (drawer instanceof HTMLElement) {
drawer.toggleAttribute('hidden', true)
}
}
toggleButton?.addEventListener('click', () => {
void setOpen(!open)
})
closeButton?.addEventListener('click', () => {
void setOpen(false)
})
repositorySelect?.addEventListener('change', async () => {
selectedRepositoryFilter = getNormalizedRepositoryFilter(repositorySelect.value)
hasUserSelectedRepositoryFilter = true
if (typeof onRepositoryFilterChange === 'function') {
await onRepositoryFilterChange(selectedRepositoryFilter)
}
await refresh({ preserveSelection: false })
})
selectInput?.addEventListener('change', () => {
selectedId = toSafeText(selectInput.value)
updateActions()
})
initializeButton?.addEventListener('click', async () => {
if (typeof onInitializeWorkspace !== 'function') {
return
}
let initialized = false
try {
initialized = await onInitializeWorkspace(
getNormalizedRepositoryFilter(selectedRepositoryFilter),
)
} catch {
setStatus('Could not initialize workspace.', 'error')
return
}
if (!initialized) {
return
}
closeDrawer()
selectedId = ''
setStatus('Initialized workspace.', 'neutral')
})
newButton?.addEventListener('click', async () => {
if (typeof onCreateWorkspace !== 'function') {
return
}
let created = false
try {
created = await onCreateWorkspace(
getNormalizedRepositoryFilter(selectedRepositoryFilter),
)
} catch {
setStatus('Could not create workspace.', 'error')
return
}
if (!created) {
return
}
selectedId =
typeof getActiveWorkspaceId === 'function' ? toSafeText(getActiveWorkspaceId()) : ''
setStatus('Created workspace.', 'neutral')
await refresh({ preserveSelection: Boolean(selectedId) })
closeDrawer()
})
openButton?.addEventListener('click', async () => {
const id = toSafeText(selectedId)
if (!id || typeof onOpenSelected !== 'function') {
return
}
selectedId = id
let opened = false
try {
opened = await onOpenSelected(id)
} catch {
setStatus('Could not load selected workspace.', 'error')
return
}
if (!opened) {
return
}
closeDrawer()
setStatus('Loaded workspace.', 'neutral')
})
renameButton?.addEventListener('click', async () => {
const id = toSafeText(selectedId)
if (!id || typeof onRenameSelected !== 'function') {
return
}
selectedId = id
let renamed = false
try {
renamed = await onRenameSelected(id)
} catch {
setStatus('Could not rename stored workspace.', 'error')
return
}
if (!renamed) {
return
}
await refresh({ preserveSelection: true })
setStatus('Renamed workspace.', 'neutral')
})
removeButton?.addEventListener('click', async () => {
const id = toSafeText(selectedId)
if (!id || typeof onRemoveSelected !== 'function') {
return
}
let removed = false
try {
removed = await onRemoveSelected(id)
} catch {
setStatus('Could not remove selected workspace.', 'error')
return
}
if (!removed) {
return
}
selectedId = ''
setStatus('Removed stored workspace.', 'neutral')
await refresh({ preserveSelection: false })
})
return {
setOpen,
isOpen: () => open,
refresh,
syncRepositoryFilterOptions,
setStatus,
setSelectedId: id => {
selectedId = toSafeText(id)
renderOptions()
},
}
}