Skip to content

Commit d9efc90

Browse files
authored
Merge pull request cangzhang#9 from cangzhang/feat/adjust
feat: add depot quick pick component
2 parents 1af1316 + 132af6e commit d9efc90

File tree

6 files changed

+114
-60
lines changed

6 files changed

+114
-60
lines changed

src/extension.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ async function activate(context: IContext) {
1717
toast.warn(`请先登录 CODING`);
1818
}
1919

20-
const codingServer = new CodingServer(
21-
{
22-
accessToken: token,
23-
},
24-
repoInfo,
25-
);
20+
const codingServer = new CodingServer({
21+
accessToken: token,
22+
});
2623

2724
dispatch(ACTIONS.SET_CTX, {
2825
context,
@@ -31,6 +28,7 @@ async function activate(context: IContext) {
3128
codingServer,
3229
depots: [],
3330
selectedDepot: null,
31+
selectedMR: null,
3432
token,
3533
userInfo: null,
3634
repoInfo,

src/init.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,36 @@ import toast from './utils/toast';
88
import ACTIONS, { dispatch } from './utils/actions';
99
import { IDepot, IMRItem, IUserInfo } from './typings/common';
1010
import * as DCloudService from './services/dcloud';
11+
import CodingServer from './services/codingServer';
1112

1213
const { registerCommand } = hx.commands;
13-
const { createTreeView } = hx.window;
14+
const { createTreeView, showQuickPick } = hx.window;
1415

1516
export function registerCommands(context: IContext) {
1617
const { codingServer, token } = context;
1718

19+
context.subscriptions.push(
20+
registerCommand('codingPlugin.pickDepot', async function () {
21+
const options: IQuickPickOption[] = [];
22+
context.depots.forEach((depot: IDepot) => {
23+
options.push({
24+
label: depot.name,
25+
depot,
26+
});
27+
});
28+
29+
const result = await showQuickPick(options);
30+
dispatch(ACTIONS.SET_SELECTED_DEPOT, {
31+
context,
32+
value: result.depot,
33+
});
34+
}),
35+
);
36+
1837
context.subscriptions.push(
1938
registerCommand('codingPlugin.mrTreeItemClick', async function ([userInfo, mrItem]: [IUserInfo, IMRItem]) {
39+
if (context.selectedMR?.id === mrItem.id) return;
40+
2041
const matchRes = mrItem.path.match(/\/p\/([^/]+)\/d\/([^/]+)\/git\/merge\/([0-9]+)/);
2142
if (matchRes) {
2243
const [, project, repo, mergeRequestIId] = matchRes;
@@ -30,6 +51,10 @@ export function registerCommands(context: IContext) {
3051
repo,
3152
},
3253
});
54+
dispatch(ACTIONS.SET_SELECTED_MR, {
55+
context,
56+
value: mrItem,
57+
});
3358
}
3459
}),
3560
);
@@ -58,7 +83,13 @@ export function registerCommands(context: IContext) {
5883
const team = context.userInfo.team;
5984
const result = await codingServer.createDepot(team, depot, depot);
6085
if (result) {
61-
toast.info('仓库创建成功');
86+
const res = await toast.info('仓库创建成功,是否切换到该仓库?', ['是', '否']);
87+
if (res === '是') {
88+
dispatch(ACTIONS.SET_SELECTED_DEPOT, {
89+
context,
90+
value: result,
91+
});
92+
}
6293
}
6394
}),
6495
);
@@ -87,15 +118,13 @@ export function createTreeViews(context: IContext) {
87118
);
88119
}
89120

90-
export function workspaceInit() {
91-
hx.workspace.onDidChangeWorkspaceFolders(function (event: any) {
92-
if (event.added) {
93-
event.added.forEach((item: any) => console.log('新增了项目: ', item.name));
94-
}
95-
96-
if (event.removed) {
97-
event.removed.forEach((item: any) => console.log('移除了项目: ', item.name));
98-
}
121+
export function workspaceInit(context: IContext) {
122+
hx.workspace.onDidChangeWorkspaceFolders(async function () {
123+
const repoInfo = await CodingServer.getRepoParams();
124+
dispatch(ACTIONS.SET_REPO_INFO, {
125+
context,
126+
value: repoInfo,
127+
});
99128
});
100129
}
101130

@@ -136,6 +165,6 @@ async function initCredentials(context: IContext) {
136165
export default function init(context: IContext) {
137166
registerCommands(context);
138167
createTreeViews(context);
139-
workspaceInit();
168+
workspaceInit(context);
140169
initCredentials(context);
141170
}

src/services/codingServer.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ import toast from '../utils/toast';
99

1010
export default class CodingServer {
1111
_session: ISessionData;
12-
_repo!: IRepoInfo;
1312

14-
constructor(session: ISessionData, repo?: IRepoInfo) {
13+
constructor(session: ISessionData) {
1514
this._session = session;
16-
17-
if (repo) {
18-
this._repo = repo;
19-
}
2015
}
2116

2217
get session() {
@@ -104,7 +99,7 @@ export default class CodingServer {
10499
}
105100
}
106101

107-
async getDepotList(team: string, project: string = this._repo?.project) {
102+
async getDepotList(team: string, project: string) {
108103
// TODO: 使用新接口
109104
try {
110105
const result = await axios({

src/trees/depotMR.ts

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IDepot, IMRItem, IReviewer, IRepoInfo } from '../typings/common';
66

77
interface IItem extends ITreeItem {
88
_create: boolean;
9+
_pick: boolean;
910
_auth: boolean;
1011
_login: boolean;
1112
_disabled: boolean;
@@ -22,10 +23,19 @@ const LOGIN = [
2223
},
2324
];
2425

26+
const AUTH = [
27+
{
28+
name: 'CODING 授权',
29+
_auth: true,
30+
_isDepot: true,
31+
},
32+
];
33+
2534
const getCommand = (element: IElement) => {
2635
if (element.children || element._disabled) return '';
2736
if (element._login) return 'codingPlugin.login';
2837
if (element._auth) return 'codingPlugin.auth';
38+
if (element._pick) return 'codingPlugin.pickDepot';
2939
if (element._create) return 'codingPlugin.createDepot';
3040
if (element.vcsType) return 'codingPlugin.depotTreeItemClick';
3141
return 'codingPlugin.mrTreeItemClick';
@@ -59,7 +69,7 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
5969

6070
async getData(repoInfo?: IRepoInfo) {
6171
const { codingServer } = this.context;
62-
const promises = [codingServer.getDepotList(this.user.team)];
72+
const promises = [codingServer.getDepotList(this.user.team, this.context.repoInfo?.project)];
6373
if (repoInfo) {
6474
promises.push(codingServer.getMrList(repoInfo));
6575
}
@@ -91,7 +101,47 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
91101
};
92102
}
93103

104+
async getMenus(repoInfo?: IRepoInfo) {
105+
const {
106+
depots,
107+
MRList: [createdList, reviewerList, others],
108+
} = await this.getData(repoInfo);
109+
110+
return [
111+
{
112+
name: '+ 创建仓库',
113+
_create: true,
114+
_isDepot: true,
115+
},
116+
{
117+
name: `切换仓库(仓库总数:${depots.length})`,
118+
_pick: true,
119+
_isDepot: true,
120+
},
121+
{
122+
title: `合并请求列表(当前仓库:${repoInfo?.repo || '-'})`,
123+
_disabled: true,
124+
},
125+
{
126+
title: `我创建的 (${createdList?.length})`,
127+
children: createdList,
128+
},
129+
{
130+
title: `需要我 Review 的 (${reviewerList?.length})`,
131+
children: reviewerList,
132+
},
133+
{
134+
title: `其他(${others?.length})`,
135+
children: others,
136+
},
137+
];
138+
}
139+
94140
async getChildren(element: IElement) {
141+
if (!this.context.token) {
142+
return Promise.resolve(AUTH);
143+
}
144+
95145
if (!this.user) {
96146
try {
97147
this.user = await this.getUser();
@@ -113,40 +163,8 @@ class DepotMRTreeDataProvider extends hx.TreeDataProvider {
113163
const repoInfo = await this.getRepoInfo();
114164

115165
try {
116-
const {
117-
depots,
118-
MRList: [createdList, reviewerList, others],
119-
} = await this.getData(repoInfo);
120-
121-
return Promise.resolve([
122-
{
123-
name: '+ 创建仓库',
124-
_create: true,
125-
_isDepot: true,
126-
},
127-
{
128-
name: '仓库列表',
129-
children: depots,
130-
_isDepot: true,
131-
},
132-
{
133-
title: `合并请求列表(当前仓库:${repoInfo?.repo || '-'})`,
134-
children: [
135-
{
136-
title: `我创建的 (${createdList?.length})`,
137-
children: createdList,
138-
},
139-
{
140-
title: `需要我 Review 的 (${reviewerList?.length})`,
141-
children: reviewerList,
142-
},
143-
{
144-
title: `其他(${others?.length})`,
145-
children: others,
146-
},
147-
],
148-
},
149-
]);
166+
const menus = await this.getMenus(repoInfo);
167+
return Promise.resolve(menus);
150168
} catch (err) {
151169
const { code } = err;
152170
if (code === 1100 || code === 1400) {

src/typings/hbuilderx.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ interface ITreeItem {
2424
name?: string;
2525
children?: ITreeItem[];
2626
}
27+
28+
interface IQuickPickOption {
29+
label: string;
30+
description?: string;
31+
[key: string]: any;
32+
}

src/utils/actions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const enum ACTIONS {
22
SET_DEPOTS = 'SET_DEPOTS',
33
SET_SELECTED_DEPOT = 'SET_SELECTED_DEPOT',
4+
SET_SELECTED_MR = 'SET_SELECTED_MR',
45
SET_USER_INFO = 'SET_USER_INFO',
6+
SET_REPO_INFO = 'SET_REPO_INFO',
57
SET_CTX = 'SET_CTX',
68
SET_MR_CUSTOM_EDITOR = 'SET_MR_CUSTOM_EDITOR',
79
}
@@ -19,9 +21,15 @@ export const dispatch = (type: ACTIONS, { context, value }: IPayload) => {
1921
case ACTIONS.SET_SELECTED_DEPOT:
2022
context.selectedDepot = value;
2123
break;
24+
case ACTIONS.SET_SELECTED_MR:
25+
context.selectedMR = value;
26+
break;
2227
case ACTIONS.SET_USER_INFO:
2328
context.userInfo = value;
2429
break;
30+
case ACTIONS.SET_REPO_INFO:
31+
context.repoInfo = value;
32+
break;
2533
case ACTIONS.SET_CTX:
2634
context.ctx = value;
2735
break;

0 commit comments

Comments
 (0)