-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-pr-context-ui.js
More file actions
172 lines (151 loc) · 5.32 KB
/
Copy pathgithub-pr-context-ui.js
File metadata and controls
172 lines (151 loc) · 5.32 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
export const createGitHubPrContextUiController = ({
contextState,
getActivePrContextSyncKey,
githubPrToggle,
githubPrToggleLabel,
githubPrToggleIcon,
githubPrToggleIconPath,
componentPrSyncIcon,
componentPrSyncIconPath,
stylesPrSyncIcon,
stylesPrSyncIconPath,
githubPrContextClose,
githubPrContextDisconnect,
aiChatToggle,
workspacesToggle,
githubPrOpenIcon,
githubPrPushCommitIcon,
closeChatDrawer,
closePrDrawer,
closeWorkspacesDrawer,
}) => {
const setGitHubPrToggleVisual = mode => {
if (
!(githubPrToggle instanceof HTMLButtonElement) ||
!(githubPrToggleLabel instanceof HTMLElement) ||
!(githubPrToggleIcon instanceof SVGElement) ||
!(githubPrToggleIconPath instanceof SVGPathElement)
) {
return
}
const isPushCommitMode = mode === 'push-commit'
const label = isPushCommitMode ? 'Push' : 'Open PR'
const title = isPushCommitMode
? 'Push commit to active pull request branch'
: 'Open pull request'
const icon = isPushCommitMode ? githubPrPushCommitIcon : githubPrOpenIcon
githubPrToggleLabel.textContent = label
githubPrToggle.title = title
githubPrToggle.setAttribute('aria-label', title)
githubPrToggleIcon.setAttribute('viewBox', icon.viewBox)
githubPrToggleIconPath.setAttribute('d', icon.path)
}
const syncEditorPrContextIndicators = shouldShow => {
const iconNodes = [componentPrSyncIcon, stylesPrSyncIcon]
const iconPathNodes = [componentPrSyncIconPath, stylesPrSyncIconPath]
for (const iconPath of iconPathNodes) {
if (iconPath instanceof SVGPathElement) {
iconPath.setAttribute('d', githubPrOpenIcon.path)
}
}
for (const icon of iconNodes) {
if (!(icon instanceof SVGElement)) {
continue
}
icon.setAttribute('viewBox', githubPrOpenIcon.viewBox)
icon.dataset.visible = shouldShow ? 'true' : 'false'
icon.toggleAttribute('hidden', !shouldShow)
}
}
const setActivePrContext = activeContext => {
contextState.activePrContext = activeContext ?? null
const nextSyncKey = getActivePrContextSyncKey(activeContext)
if (!nextSyncKey) {
contextState.activePrEditorSyncKey = ''
contextState.hasSyncedActivePrEditorContent = false
} else if (contextState.activePrEditorSyncKey !== nextSyncKey) {
contextState.activePrEditorSyncKey = nextSyncKey
contextState.hasSyncedActivePrEditorContent = false
}
const hasActiveContext = Boolean(activeContext?.prTitle)
const shouldShowEditorSyncIndicators =
hasActiveContext && contextState.hasSyncedActivePrEditorContent
setGitHubPrToggleVisual(hasActiveContext ? 'push-commit' : 'open-pr')
syncEditorPrContextIndicators(shouldShowEditorSyncIndicators)
if (!hasActiveContext) {
githubPrContextClose?.setAttribute('hidden', '')
githubPrContextDisconnect?.setAttribute('hidden', '')
return
}
githubPrContextClose?.removeAttribute('hidden')
githubPrContextDisconnect?.removeAttribute('hidden')
}
const markActivePrEditorContentSynced = () => {
const hasActiveContext = Boolean(contextState.activePrContext?.prTitle)
if (!hasActiveContext) {
return
}
contextState.hasSyncedActivePrEditorContent = true
syncEditorPrContextIndicators(true)
}
const syncAiChatTokenVisibility = token => {
const hasToken = typeof token === 'string' && token.trim().length > 0
if (hasToken) {
if (workspacesToggle instanceof HTMLButtonElement) {
workspacesToggle.disabled = false
}
if (aiChatToggle instanceof HTMLElement) {
aiChatToggle.hidden = false
}
if (githubPrToggle instanceof HTMLElement) {
githubPrToggle.hidden = false
}
if (!contextState.activePrContext) {
if (workspacesToggle instanceof HTMLElement) {
workspacesToggle.hidden = false
}
}
if (contextState.activePrContext) {
githubPrContextClose?.removeAttribute('hidden')
githubPrContextDisconnect?.removeAttribute('hidden')
if (workspacesToggle instanceof HTMLElement) {
workspacesToggle.hidden = true
}
} else {
githubPrContextClose?.setAttribute('hidden', '')
githubPrContextDisconnect?.setAttribute('hidden', '')
}
return
}
if (aiChatToggle instanceof HTMLElement) {
aiChatToggle.hidden = true
}
aiChatToggle?.setAttribute('aria-expanded', 'false')
if (workspacesToggle instanceof HTMLButtonElement) {
workspacesToggle.disabled = true
}
contextState.activePrContext = null
contextState.activePrEditorSyncKey = ''
contextState.hasSyncedActivePrEditorContent = false
syncEditorPrContextIndicators(false)
setGitHubPrToggleVisual('open-pr')
if (githubPrToggle instanceof HTMLElement) {
githubPrToggle.hidden = true
}
githubPrToggle?.setAttribute('aria-expanded', 'false')
if (workspacesToggle instanceof HTMLElement) {
workspacesToggle.hidden = true
}
workspacesToggle?.setAttribute('aria-expanded', 'false')
githubPrContextClose?.setAttribute('hidden', '')
githubPrContextDisconnect?.setAttribute('hidden', '')
closeChatDrawer?.()
closePrDrawer?.()
void closeWorkspacesDrawer?.()
}
return {
markActivePrEditorContentSynced,
setActivePrContext,
syncAiChatTokenVisibility,
}
}