forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.tsx
More file actions
72 lines (60 loc) · 2.11 KB
/
Copy pathcontext.tsx
File metadata and controls
72 lines (60 loc) · 2.11 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
import { createContext } from 'react';
import { getMessageHandler, MessageHandler } from './message';
import { PullRequest, getState, setState } from './cache';
import { MergeMethod } from '../src/github/interface';
export class PRContext {
constructor(
public pr: PullRequest = getState(),
public onchange: ((ctx: PullRequest) => void) | null = null,
private _handler: MessageHandler = null) {
if (!_handler) {
console.log('init message handler', this.handleMessage);
this._handler = getMessageHandler(this.handleMessage);
}
}
public checkout = () =>
this.postMessage({ command: 'pr.checkout' })
public exitReviewMode = async () => {
if (!this.pr) { return; }
return this.postMessage({
command: 'pr.checkout-default-branch',
args: this.pr.repositoryDefaultBranch,
});
}
public refresh = () =>
this.postMessage({ command: 'pr.refresh' })
public merge = (args: { title: string, description: string, method: MergeMethod }) =>
this.postMessage({ command: 'pr.merge', args })
public comment = (args: string) =>
this.postMessage({ command: 'pr.comment', args})
setPR = (pr: PullRequest) => {
this.pr = pr;
setState(this.pr);
if (this.onchange) { this.onchange(this.pr); }
return this;
}
updatePR = (pr: Partial<PullRequest>) =>
this.setPR({...this.pr, ...pr })
private postMessage(message: any) {
console.log('sending', message);
return this._handler.postMessage(message);
}
handleMessage = (message: any) => {
console.log('message from host:', message);
switch (message.command) {
case 'pr.initialize':
return this.setPR(message.pullrequest);
case 'update-state':
return this.updatePR({ state: message.state });
case 'pr.update-checkout-status':
return this.updatePR({ isCurrentlyCheckedOut: message.isCurrentlyCheckedOut });
case 'pr.enable-exit':
return this.updatePR({ isCurrentlyCheckedOut: true });
case 'set-scroll':
window.scrollTo(message.scrollPosition.x, message.scrollPosition.y);
}
}
public static instance = new PRContext();
}
const PullRequestContext = createContext<PRContext>(PRContext.instance);
export default PullRequestContext;