Skip to content

Commit e4f2bed

Browse files
authored
Merge pull request #683 from actiontech/feature/cb-issue
feat: change CloudBeaver auto-redirect behavior from new tab to current window
2 parents 85b3b88 + 172feeb commit e4f2bed

File tree

5 files changed

+55
-78
lines changed

5 files changed

+55
-78
lines changed

packages/base/src/page/CloudBeaver/__snapshots__/index.ce.test.tsx.snap

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,8 @@ exports[`test base/page/CloudBeaver should match snapshot when sql query is disa
1717
<div
1818
class="ant-spin-nested-loading"
1919
>
20-
<div>
21-
<div
22-
aria-busy="true"
23-
aria-live="polite"
24-
class="ant-spin ant-spin-spinning"
25-
>
26-
<span
27-
class="ant-spin-dot ant-spin-dot-spin"
28-
>
29-
<i
30-
class="ant-spin-dot-item"
31-
/>
32-
<i
33-
class="ant-spin-dot-item"
34-
/>
35-
<i
36-
class="ant-spin-dot-item"
37-
/>
38-
<i
39-
class="ant-spin-dot-item"
40-
/>
41-
</span>
42-
</div>
43-
</div>
4420
<div
45-
class="ant-spin-container ant-spin-blur"
21+
class="ant-spin-container"
4622
>
4723
<section
4824
class="css-l3x35e"

packages/base/src/page/CloudBeaver/__snapshots__/index.test.tsx.snap

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,8 @@ exports[`test base/page/CloudBeaver should match snapshot when sql query is disa
1717
<div
1818
class="ant-spin-nested-loading"
1919
>
20-
<div>
21-
<div
22-
aria-busy="true"
23-
aria-live="polite"
24-
class="ant-spin ant-spin-spinning"
25-
>
26-
<span
27-
class="ant-spin-dot ant-spin-dot-spin"
28-
>
29-
<i
30-
class="ant-spin-dot-item"
31-
/>
32-
<i
33-
class="ant-spin-dot-item"
34-
/>
35-
<i
36-
class="ant-spin-dot-item"
37-
/>
38-
<i
39-
class="ant-spin-dot-item"
40-
/>
41-
</span>
42-
</div>
43-
</div>
4420
<div
45-
class="ant-spin-container ant-spin-blur"
21+
class="ant-spin-container"
4622
>
4723
<section
4824
class="css-l3x35e"

packages/base/src/page/CloudBeaver/index.ce.test.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,24 @@ describe('test base/page/CloudBeaver', () => {
4848
expect(screen.getByText('打开SQL工作台')).toBeInTheDocument();
4949

5050
fireEvent.click(screen.getByText('打开SQL工作台'));
51+
expect(global.open).toHaveBeenCalledTimes(0);
52+
53+
await act(async () => jest.advanceTimersByTime(3000));
54+
5155
expect(global.open).toHaveBeenCalledTimes(1);
5256
expect(global.open).toHaveBeenCalledWith(
5357
enableSqlQueryUrlData.sql_query_root_uri
5458
);
5559
});
5660

57-
it('should auto jump to cloud beaver when "OPEN_CLOUD_BEAVER_URL_PARAM_NAME" in location search', async () => {
58-
global.open = jest.fn();
61+
it('should auto replace to cloud beaver when "OPEN_CLOUD_BEAVER_URL_PARAM_NAME" in location search', async () => {
62+
const originLocation = window.location;
63+
Object.defineProperty(window, 'location', {
64+
value: {
65+
...originLocation
66+
},
67+
writable: true
68+
});
5969

6070
baseSuperRender(<CloudBeaver />, undefined, {
6171
routerProps: {
@@ -67,8 +77,6 @@ describe('test base/page/CloudBeaver', () => {
6777

6878
await act(async () => jest.advanceTimersByTime(3000));
6979

70-
expect(global.open).not.toHaveBeenCalled();
71-
7280
cleanup();
7381

7482
getSqlQueryUrlSpy.mockImplementation(() =>
@@ -85,9 +93,6 @@ describe('test base/page/CloudBeaver', () => {
8593
});
8694
await act(async () => jest.advanceTimersByTime(3000));
8795

88-
expect(global.open).toHaveBeenCalledTimes(1);
89-
expect(global.open).toHaveBeenCalledWith(
90-
enableSqlQueryUrlData.sql_query_root_uri
91-
);
96+
expect(window.location.href).toBe(enableSqlQueryUrlData.sql_query_root_uri);
9297
});
9398
});

packages/base/src/page/CloudBeaver/index.test.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ jest.mock('react-redux', () => {
2121
};
2222
});
2323

24+
const originLocation = window.location;
25+
Object.defineProperty(window, 'location', {
26+
value: {
27+
...originLocation
28+
},
29+
writable: true
30+
});
31+
2432
describe('test base/page/CloudBeaver', () => {
2533
let getSqlQueryUrlSpy: jest.SpyInstance;
2634
const mockDispatch = jest.fn();
@@ -82,15 +90,17 @@ describe('test base/page/CloudBeaver', () => {
8290
expect(screen.getByText('打开SQL工作台')).toBeInTheDocument();
8391

8492
fireEvent.click(screen.getByText('打开SQL工作台'));
93+
expect(global.open).toHaveBeenCalledTimes(0);
94+
95+
await act(async () => jest.advanceTimersByTime(3000));
96+
8597
expect(global.open).toHaveBeenCalledTimes(1);
8698
expect(global.open).toHaveBeenCalledWith(
8799
enableSqlQueryUrlData.sql_query_root_uri
88100
);
89101
});
90102

91-
it('should auto jump to cloud beaver when "OPEN_CLOUD_BEAVER_URL_PARAM_NAME" in location search', async () => {
92-
global.open = jest.fn();
93-
103+
it('should auto replace to cloud beaver when "OPEN_CLOUD_BEAVER_URL_PARAM_NAME" in location search', async () => {
94104
superRender(<CloudBeaver />, undefined, {
95105
routerProps: {
96106
initialEntries: [
@@ -101,8 +111,6 @@ describe('test base/page/CloudBeaver', () => {
101111

102112
await act(async () => jest.advanceTimersByTime(3000));
103113

104-
expect(global.open).not.toHaveBeenCalled();
105-
106114
cleanup();
107115

108116
getSqlQueryUrlSpy.mockImplementation(() =>
@@ -119,9 +127,6 @@ describe('test base/page/CloudBeaver', () => {
119127
});
120128
await act(async () => jest.advanceTimersByTime(3000));
121129

122-
expect(global.open).toHaveBeenCalledTimes(1);
123-
expect(global.open).toHaveBeenCalledWith(
124-
enableSqlQueryUrlData.sql_query_root_uri
125-
);
130+
expect(window.location.href).toBe(enableSqlQueryUrlData.sql_query_root_uri);
126131
});
127132
});

packages/base/src/page/CloudBeaver/index.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useRequest } from 'ahooks';
22
import { Card, Space, Typography, Spin } from 'antd';
3-
import { useRef, useState } from 'react';
3+
import { useEffect, useState } from 'react';
44
import { useTranslation } from 'react-i18next';
55
import cloudBeaver from '@actiontech/shared/lib/api/base/service/CloudBeaver';
66
import {
@@ -20,28 +20,39 @@ import { ROUTE_PATHS } from '@actiontech/shared/lib/data/routePaths';
2020

2121
const CloudBeaver = () => {
2222
const { t } = useTranslation();
23-
const cloudBeaverUrl = useRef('');
2423
const extractQueries = useTypedQuery();
2524

2625
const [getOperationLogsLoading, setGetOperationLogsLoading] = useState(false);
2726

28-
const { data, loading } = useRequest(() => {
29-
return cloudBeaver.GetSQLQueryConfiguration().then((res) => {
30-
if (res?.data.data?.enable_sql_query) {
31-
cloudBeaverUrl.current = res?.data.data.sql_query_root_uri as string;
27+
const { data, loading, runAsync } = useRequest(
28+
() => {
29+
return cloudBeaver.GetSQLQueryConfiguration().then((res) => {
30+
return res.data.data;
31+
});
32+
},
33+
{
34+
manual: true
35+
}
36+
);
3237

38+
useEffect(() => {
39+
runAsync().then((res) => {
40+
if (res?.enable_sql_query && res.sql_query_root_uri) {
3341
const params = extractQueries(ROUTE_PATHS.BASE.CLOUD_BEAVER.index);
3442

3543
if (params?.open_cloud_beaver === 'true') {
36-
window.open(cloudBeaverUrl.current);
44+
window.location.href = res.sql_query_root_uri;
3745
}
3846
}
39-
return res.data.data;
4047
});
41-
});
48+
}, [extractQueries, runAsync]);
4249

4350
const openCloudBeaver = () => {
44-
window.open(cloudBeaverUrl.current);
51+
runAsync().then((res) => {
52+
if (res?.enable_sql_query && res.sql_query_root_uri) {
53+
window.open(res.sql_query_root_uri);
54+
}
55+
});
4556
};
4657

4758
return (
@@ -50,7 +61,11 @@ const CloudBeaver = () => {
5061
title={t('dmsCloudBeaver.pageTitle')}
5162
extra={
5263
<EmptyBox if={!!data?.enable_sql_query}>
53-
<BasicButton type="primary" onClick={openCloudBeaver}>
64+
<BasicButton
65+
loading={loading}
66+
type="primary"
67+
onClick={openCloudBeaver}
68+
>
5469
{t('dmsCloudBeaver.jumpToCloudBeaver')}
5570
</BasicButton>
5671
</EmptyBox>

0 commit comments

Comments
 (0)