-
Notifications
You must be signed in to change notification settings - Fork 733
Expand file tree
/
Copy pathheader.tsx
More file actions
348 lines (315 loc) · 9.66 KB
/
header.tsx
File metadata and controls
348 lines (315 loc) · 9.66 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import React, { useContext, useState } from 'react';
import { copilotEventToStatus, CopilotPRStatus, mostRecentCopilotEvent } from '../../src/common/copilot';
import { CopilotStartedEvent, TimelineEvent } from '../../src/common/timelineEvent';
import { GithubItemStateEnum, StateReason } from '../../src/github/interface';
import { CodingAgentContext, OverviewContext, PullRequest } from '../../src/github/views';
import PullRequestContext from '../common/context';
import { useStateProp } from '../common/hooks';
import { ContextDropdown } from './contextDropdown';
import { copilotErrorIcon, copilotInProgressIcon, copilotSuccessIcon, copyIcon, editIcon, issueClosedIcon, issueIcon, loadingIcon, mergeIcon, prClosedIcon, prDraftIcon, prOpenIcon } from './icon';
import { AuthorLink, Avatar } from './user';
export function Header({
canEdit,
state,
head,
base,
title,
titleHTML,
number,
url,
author,
isCurrentlyCheckedOut,
isDraft,
isIssue,
repositoryDefaultBranch,
events,
owner,
repo,
busy,
stateReason
}: PullRequest) {
const [currentTitle, setCurrentTitle] = useStateProp(title);
const [inEditMode, setEditMode] = useState(false);
const codingAgentEvent = mostRecentCopilotEvent(events);
return (
<>
<Title
title={currentTitle}
titleHTML={titleHTML}
number={number}
url={url}
inEditMode={inEditMode}
setEditMode={setEditMode}
setCurrentTitle={setCurrentTitle}
canEdit={canEdit}
owner={owner}
repo={repo}
/>
<Subtitle state={state} stateReason={stateReason} head={head} base={base} author={author} isIssue={isIssue} isDraft={isDraft} codingAgentEvent={codingAgentEvent} />
<div className="header-actions">
<ButtonGroup
isCurrentlyCheckedOut={isCurrentlyCheckedOut}
isIssue={isIssue}
repositoryDefaultBranch={repositoryDefaultBranch}
owner={owner}
repo={repo}
number={number}
busy={busy}
/>
<CancelCodingAgentButton canEdit={canEdit} codingAgentEvent={codingAgentEvent} />
</div>
</>
);
}
function Title({ title, titleHTML, number, url, inEditMode, setEditMode, setCurrentTitle, canEdit, owner, repo }) {
const { setTitle } = useContext(PullRequestContext);
const titleForm = (
<form
className="editing-form title-editing-form"
onSubmit={async evt => {
evt.preventDefault();
try {
const txt = (evt.target as any)[0].value;
await setTitle(txt);
setCurrentTitle(txt);
} finally {
setEditMode(false);
}
}}
>
<input type="text" style={{ width: '100%' }} defaultValue={title} ></input>
<div className="form-actions">
<button type="button" className="secondary" onClick={() => setEditMode(false)}>
Cancel
</button>
<button type="submit">Update</button>
</div>
</form>
);
const context: OverviewContext = {
'preventDefaultContextMenuItems': true,
owner,
repo,
number
};
context['github:copyMenu'] = true;
const displayTitle = (
<div className="overview-title">
<h2>
<span dangerouslySetInnerHTML={{ __html: titleHTML }} />
{' '}
<a href={url} title={url} data-vscode-context={JSON.stringify(context)}>
#{number}
</a>
</h2>
{canEdit ?
<button title="Rename" onClick={setEditMode} className="icon-button">
{editIcon}
</button>
: null}
</div>
);
const editableTitle = inEditMode ? titleForm : displayTitle;
return editableTitle;
}
function ButtonGroup({ isCurrentlyCheckedOut, isIssue, repositoryDefaultBranch, owner, repo, number, busy }) {
const { refresh, copyPrLink } = useContext(PullRequestContext);
return (
<div className="button-group">
<CheckoutButton {...{ isCurrentlyCheckedOut, isIssue, repositoryDefaultBranch, owner, repo, number }} />
{!isIssue ? (
<button title="Copy link" onClick={copyPrLink} className="secondary">
{copyIcon}
</button>
) : null}
<button title="Refresh with the latest data from GitHub" onClick={refresh} className="secondary">
Refresh
</button>
{busy ? (
<div className='spinner'>
{loadingIcon}
</div>
) : null}
</div>
);
}
function CancelCodingAgentButton({ canEdit, codingAgentEvent }: { canEdit: boolean, codingAgentEvent: TimelineEvent | undefined }) {
const { cancelCodingAgent, updatePR, openSessionLog } = useContext(PullRequestContext);
const [isBusy, setBusy] = useState(false);
const cancel = async () => {
if (!codingAgentEvent) {
return;
}
setBusy(true);
const result = await cancelCodingAgent(codingAgentEvent);
if (result.events.length > 0) {
updatePR(result);
}
setBusy(false);
};
// Extract sessionLink from the coding agent event
const sessionLink = (codingAgentEvent as CopilotStartedEvent)?.sessionLink;
if (!codingAgentEvent || copilotEventToStatus(codingAgentEvent) !== CopilotPRStatus.Started) {
return null;
}
const context: CodingAgentContext = {
'preventDefaultContextMenuItems': true,
...sessionLink
};
context['github:codingAgentMenu'] = true;
const actions: { label: string; value: string; action: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void }[] = [];
if (sessionLink) {
actions.push({
label: 'View Session',
value: '',
action: () => openSessionLog(sessionLink)
});
}
if (canEdit) {
actions.unshift({
label: 'Cancel Coding Agent',
value: '',
action: cancel
});
}
return <ContextDropdown
optionsContext={() => JSON.stringify(context)}
defaultAction={actions[0].action}
defaultOptionLabel={() => isBusy ? (
<>
<span className='loading-button'>{loadingIcon}</span>
{actions[0].label}
</>
) : actions[0].label}
defaultOptionValue={() => actions[0].value}
allOptions={() => {
return actions;
}}
optionsTitle={actions[0].label}
disabled={isBusy}
hasSingleAction={false}
spreadable={false}
isSecondary={true}
/>;
}
function Subtitle({ state, stateReason, isDraft, isIssue, author, base, head, codingAgentEvent }) {
const { text, color, icon } = getStatus(state, isDraft, isIssue, stateReason);
const copilotStatus = copilotEventToStatus(codingAgentEvent);
let copilotStatusIcon: JSX.Element | undefined;
if (copilotStatus === CopilotPRStatus.Started) {
copilotStatusIcon = copilotInProgressIcon;
} else if (copilotStatus === CopilotPRStatus.Completed) {
copilotStatusIcon = copilotSuccessIcon;
} else if (copilotStatus === CopilotPRStatus.Failed) {
copilotStatusIcon = copilotErrorIcon;
}
return (
<div className="subtitle">
<div id="status" className={`status-badge-${color}`}>
<span className='icon'>{icon}</span>
<span>{text}</span>
</div>
<div className="author">
{<Avatar for={author} substituteIcon={copilotStatusIcon} />}
<div className="merge-branches">
<AuthorLink for={author} /> {!isIssue ? (<>
{getActionText(state)} into{' '}
<code className="branch-tag">{base}</code> from <code className="branch-tag">{head}</code>
</>) : null}
</div>
</div>
</div>
);
}
const CheckoutButton = ({ isCurrentlyCheckedOut, isIssue, repositoryDefaultBranch, owner, repo, number }) => {
const { exitReviewMode, checkout, openChanges } = useContext(PullRequestContext);
const [isBusy, setBusy] = useState(false);
const onClick = async (command: string) => {
try {
setBusy(true);
switch (command) {
case 'checkout':
await checkout();
break;
case 'exitReviewMode':
await exitReviewMode();
break;
case 'openChanges':
await openChanges();
break;
default:
throw new Error(`Can't find action ${command}`);
}
} finally {
setBusy(false);
}
};
if (isIssue) {
return null;
}
const context: OverviewContext = {
'preventDefaultContextMenuItems': true,
owner,
repo,
number
};
context['github:checkoutMenu'] = true;
const actions: { label: string; value: string; action: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void }[] = [];
if (isCurrentlyCheckedOut) {
actions.push({
label: `Checkout '${repositoryDefaultBranch}'`,
value: '',
action: () => onClick('exitReviewMode')
});
} else {
actions.push({
label: 'Checkout',
value: '',
action: () => onClick('checkout')
});
}
actions.push({
label: 'Open Changes',
value: '',
action: () => onClick('openChanges')
});
return <ContextDropdown
optionsContext={() => JSON.stringify(context)}
defaultAction={actions[0].action}
defaultOptionLabel={() => actions[0].label}
defaultOptionValue={() => actions[0].value}
allOptions={() => {
return actions;
}}
optionsTitle={actions[0].label}
disabled={isBusy}
hasSingleAction={false}
spreadable={false}
/>;
};
export function getStatus(state: GithubItemStateEnum, isDraft: boolean, isIssue: boolean, stateReason: StateReason) {
const closed = isIssue ? issueClosedIcon : prClosedIcon;
const open = isIssue ? issueIcon : prOpenIcon;
if (state === GithubItemStateEnum.Merged) {
return { text: 'Merged', color: 'merged', icon: mergeIcon };
} else if (state === GithubItemStateEnum.Open) {
return isDraft ? { text: 'Draft', color: 'draft', icon: prDraftIcon } : { text: 'Open', color: 'open', icon: open };
} else {
let closedColor: string = 'closed';
if (isIssue) {
closedColor = stateReason !== 'COMPLETED' ? 'draft' : 'merged';
}
return { text: 'Closed', color: closedColor, icon: closed };
}
}
function getActionText(state: GithubItemStateEnum) {
if (state === GithubItemStateEnum.Merged) {
return 'merged changes';
} else {
return 'wants to merge changes';
}
}