Skip to content

Commit 162c298

Browse files
authored
Merge pull request #2 from cangzhang/feat/mr-detail
2 parents f7502e3 + d2aebe2 commit 162c298

File tree

9 files changed

+207
-85
lines changed

9 files changed

+207
-85
lines changed

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,29 @@
110110
"watch:webviews": "webpack --watch --mode development",
111111
"lint": "eslint . --ext .ts,.tsx"
112112
},
113+
"babel": {
114+
"plugins": [
115+
"babel-plugin-styled-components"
116+
]
117+
},
118+
"resolutions": {
119+
"styled-components": "^5"
120+
},
113121
"dependencies": {
114122
"@risingstack/react-easy-state": "^6.3.0",
115123
"got": "^11.7.0",
116124
"keytar": "^7.0.0",
117-
"ky": "^0.24.0",
118125
"nanoid": "^3.1.16",
119126
"react": "^17.0.0",
120-
"react-dom": "^17.0.0"
127+
"react-dom": "^17.0.0",
128+
"styled-components": "^5.2.1"
121129
},
122130
"devDependencies": {
123131
"@types/react": "^16.9.53",
124132
"@types/react-dom": "^16.9.8",
125133
"@typescript-eslint/eslint-plugin": "^3.0.2",
126134
"@typescript-eslint/parser": "^3.0.2",
135+
"babel-plugin-styled-components": "^1.12.0",
127136
"css-loader": "^5.0.0",
128137
"eslint": "^7.1.0",
129138
"husky": "^5.0.4",

src/codingServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class CodingServer {
322322
}
323323
}
324324

325-
public async getMRDetail(iid: number) {
325+
public async getMRDetail(iid: string) {
326326
try {
327327
const repoInfo = this._context.workspaceState.get(`repoInfo`) as IRepoInfo;
328328
if (!repoInfo?.team) {
@@ -339,9 +339,11 @@ export class CodingServer {
339339
},
340340
)
341341
.json();
342+
342343
if (diff.code) {
343344
return Promise.reject(diff);
344345
}
346+
345347
return diff;
346348
} catch (err) {
347349
return Promise.reject(err);

src/extension.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ export async function activate(context: vscode.ExtensionContext) {
4747

4848
context.subscriptions.push(vscode.window.registerUriHandler(uriHandler));
4949
context.subscriptions.push(
50-
vscode.commands.registerCommand('codingPlugin.showMROverview', (data: IMRWebViewDetail) => {
51-
Panel.createOrShow(context, data);
52-
Panel.currentPanel?.broadcast(`action.UPDATE_CURRENT_MR`, data);
50+
vscode.commands.registerCommand('codingPlugin.showMROverview', async (mr: IMRWebViewDetail) => {
51+
Panel.createOrShow(context);
52+
const resp = await codingSrv.getMRDetail(mr.iid);
53+
mr.data = resp.data.merge_request;
54+
Panel.currentPanel?.broadcast(`action.UPDATE_CURRENT_MR`, {
55+
...mr,
56+
data: resp.data.merge_request,
57+
});
5358
}),
5459
);
5560
context.subscriptions.push(

src/panel.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as path from 'path';
32

4-
import { IMRWebViewDetail } from './typings/commonTypes';
53
import { getNonce } from './common/utils';
64

75
export class Panel {
@@ -17,7 +15,7 @@ export class Panel {
1715
private readonly _extensionPath: string;
1816
private _disposables: vscode.Disposable[] = [];
1917

20-
public static createOrShow(context: vscode.ExtensionContext, data: IMRWebViewDetail) {
18+
public static createOrShow(context: vscode.ExtensionContext) {
2119
const { extensionUri, extensionPath } = context;
2220
const column = vscode.window.activeTextEditor
2321
? vscode.window.activeTextEditor.viewColumn
@@ -42,7 +40,7 @@ export class Panel {
4240
},
4341
);
4442

45-
Panel.currentPanel = new Panel(panel, extensionUri, extensionPath, data);
43+
Panel.currentPanel = new Panel(panel, extensionUri, extensionPath);
4644
}
4745

4846
public static revive(
@@ -53,13 +51,13 @@ export class Panel {
5351
Panel.currentPanel = new Panel(panel, extensionUri, extensionPath);
5452
}
5553

56-
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri, extensionPath: string, mr?: IMRWebViewDetail) {
54+
private constructor(panel: vscode.WebviewPanel, extensionUri: vscode.Uri, extensionPath: string) {
5755
this._panel = panel;
5856
this._extensionUri = extensionUri;
5957
this._extensionPath = extensionPath;
6058

6159
// Set the webview's initial html content
62-
this._update(mr);
60+
this._update();
6361

6462
// Listen for when the panel is disposed
6563
// This happens when the user closes the panel or when the panel is closed programatically
@@ -117,28 +115,28 @@ export class Panel {
117115
}
118116
}
119117

120-
private _update(data?: IMRWebViewDetail) {
118+
private _update() {
121119
const webview = this._panel.webview;
122120

123121
// Vary the webview's content based on where it is located in the editor.
124122
switch (this._panel.viewColumn) {
125123
case vscode.ViewColumn.Two:
126-
this._updateForCat(webview, data);
124+
this._updateForCat(webview);
127125
return;
128126

129127
case vscode.ViewColumn.Three:
130-
this._updateForCat(webview, data);
128+
this._updateForCat(webview);
131129
return;
132130

133131
case vscode.ViewColumn.One:
134132
default:
135-
this._updateForCat(webview, data);
133+
this._updateForCat(webview);
136134
return;
137135
}
138136
}
139137

140-
private _updateForCat(webview: vscode.Webview, data?: IMRWebViewDetail) {
141-
this._panel.title = `Merge Request ${data?.iid || ``}`;
138+
private _updateForCat(webview: vscode.Webview) {
139+
this._panel.title = `Merge Request`;
142140
this._panel.webview.html = this._getHtmlForWebview(webview);
143141
}
144142

@@ -154,7 +152,7 @@ export class Panel {
154152
<title>Merge Request Overview</title>
155153
<meta name="viewport" content="width=device-width, initial-scale=1.0">
156154
<meta http-equiv="Content-Security-Policy"
157-
content="default-src 'unsafe-eval'; style-src vscode-resource: 'unsafe-inline' http: https: data:;; img-src vscode-resource: https:; script-src 'nonce-${nonce}' 'unsafe-eval'; connect-src https:">
155+
content="default-src 'none'; style-src vscode-resource: 'unsafe-inline' http: https: data:;; img-src vscode-resource: https:; script-src 'nonce-${nonce}' 'unsafe-eval'; connect-src https:">
158156
</head>
159157
<body>
160158
<div id="root"></div>

webviews/App.tsx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,61 @@
11
import React, { useEffect } from 'react';
2+
import styled from 'styled-components';
23

34
import { view } from '@risingstack/react-easy-state';
45
import appStore from './store/appStore';
56
import { actions } from './store/constants';
6-
import { fetchMRDetail } from './service';
7+
8+
const LoadingWrapper = styled.div`
9+
font-size: 16px;
10+
`;
11+
const TitleWrapper = styled.div`
12+
font-size: 20px;
13+
`;
14+
const Row = styled.div`
15+
margin: 16px 0;
16+
`;
17+
const Desc = styled.article`
18+
border: 1px solid gray;
19+
padding: 10px;
20+
`;
721

822
function App() {
9-
const { currentMR, switchMR, setMRDetail } = appStore;
23+
const { currentMR, updateCurrentMR } = appStore;
1024

1125
useEffect(() => {
1226
window.addEventListener(`message`, async (ev) => {
1327
const { type, value } = ev.data;
1428
switch (type) {
1529
case actions.UPDATE_CURRENT_MR: {
16-
switchMR(value);
30+
updateCurrentMR(value);
1731
break;
1832
}
1933
default:
2034
console.log(type, value);
2135
break;
2236
}
2337
});
24-
}, [switchMR]);
38+
}, [updateCurrentMR]);
2539

26-
useEffect(() => {
27-
fetchMRDetail(currentMR.repoInfo, currentMR.iid, currentMR.accessToken).then(r => {
28-
setMRDetail(r.merge_request);
29-
});
30-
}, [currentMR.iid]);
40+
if (!currentMR.iid) {
41+
return <LoadingWrapper>Please select an merge request first.</LoadingWrapper>;
42+
}
3143

44+
const { repoInfo, data } = currentMR;
3245
return (
33-
<>
34-
<h2>#{currentMR?.iid || ``} {currentMR?.data?.title}</h2>
35-
</>
46+
<div>
47+
<TitleWrapper>
48+
{data.title} (<a
49+
href={`https://${repoInfo.team}.coding.net/p/${repoInfo.project}/d/${repoInfo.repo}/git/merge/${currentMR.iid}`}>#{currentMR.iid}</a>)
50+
</TitleWrapper>
51+
<Row>
52+
<code>{data.srcBranch}</code><code>{data.desBranch}</code>
53+
</Row>
54+
<h3>Description:</h3>
55+
<Desc>
56+
<div dangerouslySetInnerHTML={{ __html: data.body }} />
57+
</Desc>
58+
</div>
3659
);
3760
}
3861

webviews/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
declare const acquireVsCodeApi: any;
2+
declare module 'styled-components';

webviews/service.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

webviews/store/appStore.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { store } from '@risingstack/react-easy-state';
22
import { IMRWebViewDetail } from '../../src/typings/commonTypes';
3-
import { IMRDetail } from '../../src/typings/respResult';
43

54
const appStore = store({
65
currentMR: {} as IMRWebViewDetail,
7-
switchMR(data: IMRWebViewDetail) {
6+
updateCurrentMR(data: IMRWebViewDetail) {
87
appStore.currentMR = data;
98
},
10-
setMRDetail(data: IMRDetail) {
11-
appStore.currentMR.data = data;
12-
}
139
});
1410

1511
export default appStore;

0 commit comments

Comments
 (0)