Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 531c578

Browse files
committed
fix: setRegionId when regionbar changed in xconsole-context
1 parent 934d556 commit 531c578

File tree

6 files changed

+92
-58
lines changed

6 files changed

+92
-58
lines changed

packages/console-utils/xconsole-context/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alicloud/xconsole-context",
3-
"version": "2.4.34",
3+
"version": "2.4.35",
44
"main": "lib/index.js",
55
"module": "es/index.js",
66
"types": "lib/index.d.ts",

packages/console-utils/xconsole-context/src/region/determineRegionId.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const getIdFromItem = (item: IPayloadRegion): string => {
77
return item.id
88
};
99

10+
/**
11+
* 计算准确的 regionId
12+
* @param id 期望 regionId
13+
* @param currentActiveId 当前 regionId
14+
* @param dataSourceRaw 有效 region 列表
15+
* @param defaultRegionId 默认兜底 regionId
16+
* @returns
17+
*/
1018
export const determineRegionId = (
1119
id: string,
1220
currentActiveId: string,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { generatePath } from 'react-router-dom';
2+
import { match as IMatch } from 'react-router-dom';
3+
4+
import type { IConsoleContextRegionProp } from '../types/index';
5+
6+
/**
7+
* 根据路由匹配符里是否有 regionId 参数来判断是否是 region 化路由
8+
* @param match
9+
* @returns
10+
*/
11+
const hasRegionId = (match: IMatch) => {
12+
// eslint-disable-next-line no-prototype-builtins
13+
return match.params && match.params.hasOwnProperty('regionId');
14+
}
15+
16+
/**
17+
* 重定向至新的 region 化路由
18+
* @param props
19+
* @param nextRegionId
20+
* @returns
21+
*/
22+
export const reroute = (props: IConsoleContextRegionProp<{regionId?: string}>, nextRegionId: string) => {
23+
const { history, match, location } = props;
24+
if (match && match.path && hasRegionId(match)) {
25+
const { path, params } = match;
26+
27+
if (nextRegionId === params.regionId) {
28+
return;
29+
}
30+
31+
const nextPath = generatePath(path, {
32+
...(params || {}),
33+
regionId: nextRegionId
34+
});
35+
// 可能通过 window.history.pushState 改变路由导致 react-router location 对象没有更新
36+
const stuff = window.location.pathname.slice(match.url.length);
37+
38+
history[props.region?.historyAction || 'push']({
39+
pathname: match.isExact ? nextPath : `${nextPath}/${stuff}`.replace('//', '/'),
40+
search: location.search,
41+
hash: location.hash,
42+
});
43+
}
44+
}

packages/console-utils/xconsole-context/src/region/useRcRegionProps.tsx

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
import { useEffect, useState, useContext } from 'react';
2-
import { matchPath, generatePath } from 'react-router-dom';
2+
import { matchPath } from 'react-router-dom';
33
import isFunction from 'lodash/isFunction';
44

55
import ConsoleRegion from './index';
66
import { getActiveId } from './cookies';
7+
import { reroute } from './reroute';
78
import { determineRegionId } from './determineRegionId';
89
import { RegionContext } from '../context/RegionContext';
910
import type { IConsoleContextRegionProp } from '../types/index';
1011
import type { IPayloadRegion } from '../types/ConsoleBase';
1112

1213
type regionFn = (location: any) => IPayloadRegion[];
1314

14-
const hasRegionId = (match) => {
15-
// eslint-disable-next-line no-prototype-builtins
16-
return match.params && match.params.hasOwnProperty('regionId');
17-
}
18-
19-
export const reroute = (props: IConsoleContextRegionProp<{regionId?: string}>, nextRegionId: string) => {
20-
const { history, match, location } = props;
21-
if (match && match.path && hasRegionId(match)) {
22-
const { path, params } = match;
23-
24-
if (nextRegionId === params.regionId) {
25-
return;
26-
}
27-
28-
const nextPath = generatePath(path, {
29-
...(params || {}),
30-
regionId: nextRegionId
31-
});
32-
// 可能通过 window.history.pushState 改变路由导致 react-router location 对象没有更新
33-
const suff = window.location.pathname.slice(match.url.length);
34-
35-
history[props.region?.historyAction || 'push']({
36-
pathname: match.isExact ? nextPath : `${nextPath}/${suff}`.replace('//', '/'),
37-
search: location.search,
38-
hash: location.hash,
39-
});
40-
}
41-
}
42-
4315
/**
4416
* 为了适配新版本的 region, 但是由于
4517
* @param props

packages/console-utils/xconsole-context/src/region/useRegion.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useState, useContext } from 'react';
22
import { matchPath } from 'react-router-dom';
33

44
import ConsoleRegion from './index';
5-
import { reroute } from './useRcRegionProps';
5+
import { reroute } from './reroute';
66
import ConsoleBase from '../console/ConsoleBase';
77
import { determineRegionId } from './determineRegionId';
88
import { RegionContext } from '../context/RegionContext';
@@ -46,35 +46,40 @@ export default (props: IConsoleContextRegionProp<{regionId?: string}>): Region =
4646
};
4747

4848
/**
49-
* 处理路由
49+
* 将路由状态同步到应用全局 region 状态
5050
*/
5151
useEffect(() => {
52-
// 不处理
5352
if (disable) return;
5453

5554
region.setRegions(regionList);
5655
// 如果 regionId 不在 region 列表重定向到 regionId 上
5756
const regionId = determineRegionId(match.params.regionId, currentRegionId, regionList, defaultRegion);
5857

5958
if (currentRegionId !== regionId) {
59+
// 通知 consoleBase 更新 regionId
6060
region.setRegionId(regionId);
61-
setRegionIdWithMemo(regionId)
62-
return reroute(props, regionId);
61+
// 更新全局 regionId
62+
setRegionIdWithMemo(regionId);
63+
reroute(props, regionId);
6364
}
6465
}, [match.params.regionId, currentRegionId, regionList, defaultRegion]);
6566

66-
// 处理 ConsoleBase
67+
/**
68+
* 将 consoleBase 地域选择器状态变化同步到应用全局 region 状态
69+
*/
6770
useEffect(() => {
68-
// 不处理
6971
if (disable) return;
7072

71-
// update the history when region change on the regionbar
73+
// 监听 consoleBase 地域选择器更新并同步
7274
const unsubscribeRegionChange = region.onRegionChange((payload) => {
7375
if (payload.correctedFrom) {
7476
return;
7577
}
78+
7679
const regionId = determineRegionId(payload.id, currentRegionId, regionList, defaultRegion);
80+
7781
if (regionId !== currentRegionId) {
82+
setRegionIdWithMemo(regionId);
7883
reroute(props, regionId);
7984
}
8085
});
@@ -91,33 +96,34 @@ export default (props: IConsoleContextRegionProp<{regionId?: string}>): Region =
9196
exact: true,
9297
strict: true,
9398
});
99+
94100
if (matches) {
95-
region.toggleRegion(true)
96-
region.toggleRegionGlobal(true)
101+
region.toggleRegion(true);
102+
region.toggleRegionGlobal(true);
97103
return true;
98104
}
99105
return false;
100106
})
101107

102-
if (showGlobal) {
103-
return;
104-
}
108+
if (!showGlobal) {
109+
region.toggleRegion(false);
105110

106-
region.toggleRegion(false)
107-
regionbarVisiblePaths.forEach((showRegionPath) => {
108-
const matches = matchPath(location.pathname, {
109-
path: showRegionPath,
110-
exact: true,
111-
strict: true,
111+
regionbarVisiblePaths.forEach((showRegionPath) => {
112+
const matches = matchPath(location.pathname, {
113+
path: showRegionPath,
114+
exact: true,
115+
strict: true,
116+
});
117+
118+
if (matches) {
119+
region.toggleRegion(true);
120+
}
112121
});
113-
if (matches) {
114-
region.toggleRegion(true)
115-
}
116-
});
122+
}
117123

118124
return () => {
119-
unsubscribeRegionChange()
120-
unsubscribeReady()
125+
unsubscribeRegionChange();
126+
unsubscribeReady();
121127
}
122128
}, [regionList, history, regionbarVisiblePaths, globalVisiblePaths, location.pathname, currentRegionId, defaultRegion]);
123129

packages/console-utils/xconsole-context/src/types/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ export interface RegionConfig {
2828
}
2929

3030
export interface IRegionConfigProps extends RegionConfig {
31-
// region 列表
31+
/**
32+
* region 列表
33+
*/
3234
regionList?: IPayloadRegion[];
3335

34-
// disable 关闭路由同步功能
36+
/**
37+
* disable 关闭路由同步功能
38+
*/
3539
disable?: boolean;
3640
}
3741

0 commit comments

Comments
 (0)