Skip to content

Commit 2b83072

Browse files
committed
feat: take workspace project into account
1 parent 355e3d9 commit 2b83072

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

src/webviews/depot.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class WebviewProvider {
2424
this.update({
2525
token: this.context.token,
2626
userInfo: this.context.userInfo,
27+
repoInfo: this.context.repoInfo,
2728
});
2829
}
2930

webviews/components/DepotSelect/index.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import React, { useContext, useEffect } from 'react';
22
import Select from 'react-select';
3+
34
import { DataContext } from '../../reducers/context';
45
import { ACTIONS } from '../../reducers';
56
import { getDepotList } from '../../services';
67
import useAsyncFn from '../../hooks/useAsyncFn';
7-
8+
import { IDepot } from '../../typings/common';
9+
import { getDepotProject } from '../../utils/depot';
810
import style from './style.css';
911

1012
const DepotSelect = () => {
1113
const { state, dispatch } = useContext(DataContext);
1214
const { token, userInfo, refetchDepotList, selectedDepot } = state;
1315

1416
const [getDepotListState, getDepotListFn] = useAsyncFn(getDepotList);
15-
const { loading, value = [] } = getDepotListState;
17+
const { loading, value: depotList = [] } = getDepotListState;
1618

1719
const fetchData = async () => {
1820
try {
@@ -36,6 +38,11 @@ const DepotSelect = () => {
3638
}
3739
}, [refetchDepotList]);
3840

41+
const getOptionLabel = ({ name, depotPath }: IDepot) => {
42+
const project = getDepotProject(depotPath);
43+
return name === project ? name : `${name} (${project})`;
44+
};
45+
3946
return (
4047
<div className={style.root}>
4148
<span>当前仓库:</span>
@@ -50,9 +57,9 @@ const DepotSelect = () => {
5057
payload: item
5158
});
5259
}}
53-
options={value}
54-
getOptionLabel={(item) => `${item.name}`}
55-
getOptionValue={(item) => item.id}
60+
options={depotList}
61+
getOptionLabel={getOptionLabel}
62+
getOptionValue={(item) => item.depotPath}
5663
isLoading={loading}
5764
/>
5865
</div>

webviews/reducers/index.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { IDepot, IMRItem } from '../typings/common';
1+
import { getDepotProject } from '../utils/depot';
2+
import { IDepot, IMRItem, IRepoInfo } from '../typings/common';
23

34
export interface IState {
45
token: string;
56
userInfo: Record<string, any>;
7+
repoInfo: IRepoInfo | null;
68
refetchDepotList: boolean;
79
selectedDepot: IDepot | null;
8-
selectedProjectName: string;
10+
selectedProjectName: string | undefined;
911
selectedMR: IMRItem | null;
1012
}
1113

@@ -20,14 +22,28 @@ export const enum ACTIONS {
2022
SET_SELECTED_MR = 'SET_SELECTED_MR',
2123
}
2224

23-
export const initialState = {
24-
...JSON.parse(window.__CODING__),
25-
refetchDepotList: false,
26-
selectedDepot: null,
27-
selectedProjectName: '',
28-
selectedMR: null
25+
const getInitialState = (): IState => {
26+
const CODING_DATA = JSON.parse(window.__CODING__);
27+
const { repoInfo, userInfo } = CODING_DATA;
28+
const selectedDepot = repoInfo && userInfo && repoInfo.team === userInfo.team
29+
? {
30+
name: repoInfo.repo,
31+
depotPath: `/p/${repoInfo.project}/d/${repoInfo.repo}/git`,
32+
vcsType: 'git'
33+
}
34+
: null;
35+
36+
return {
37+
...CODING_DATA,
38+
refetchDepotList: false,
39+
selectedDepot,
40+
selectedProjectName: repoInfo ? repoInfo.project : '',
41+
selectedMR: null
42+
};
2943
};
3044

45+
export const initialState = getInitialState();
46+
3147
const appReducer = (state: IState, { type, payload }: IAction) => {
3248
switch (type) {
3349
case ACTIONS.REFETCH_DEPOT_LIST:
@@ -36,11 +52,10 @@ const appReducer = (state: IState, { type, payload }: IAction) => {
3652
refetchDepotList: payload
3753
};
3854
case ACTIONS.SET_SELECTED_DEPOT:
39-
const matchRes = payload.depotPath.match(/\/p\/([^/]+)/);
4055
return {
4156
...state,
4257
selectedDepot: payload,
43-
selectedProjectName: matchRes?.[1],
58+
selectedProjectName: getDepotProject(payload.depotPath),
4459
selectedMR: null
4560
};
4661
case ACTIONS.SET_SELECTED_MR:

webviews/typings/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ export interface IMRItem {
4040
author: IUserInfo;
4141
reviewers: IReviewer[];
4242
}
43+
44+
export interface IRepoInfo {
45+
team: string;
46+
project: string;
47+
repo: string;
48+
}

webviews/utils/depot.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const getDepotProject = (depotPath: string) => {
2+
const matchRes = depotPath.match(/\/p\/([^/]+)/);
3+
return matchRes?.[1];
4+
};

0 commit comments

Comments
 (0)