Skip to content

Commit 0091f36

Browse files
committed
pref: Add url params
1 parent e90887d commit 0091f36

6 files changed

Lines changed: 123 additions & 55 deletions

File tree

chat2db-client/src/components/Console/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useMemo, useRef, useState, useImperativeHandle, ForwardedRef, forwardRef } from 'react';
22
import { connect } from 'umi';
3-
import { formatParams } from '@/utils/common';
3+
import { formatParams } from '@/utils/url';
44
import connectToEventSource from '@/utils/eventSource';
55
import { Button, Spin, message, Drawer, Modal } from 'antd';
66
import ChatInput, { SyncModelType } from './ChatInput';

chat2db-client/src/components/SearchResult/components/TableBox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import styles from './index.less';
1212

1313
// 工具函数
1414
import { compareStrings } from '@/utils/sort';
15-
import { downloadFile } from '@/utils/common';
15+
import { downloadFile } from '@/utils/file';
1616
import { transformInputValue } from '../../utils';
1717

1818
// 类型定义

chat2db-client/src/pages/main/index.tsx

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Connection from './connection';
2525
import Team from './team';
2626
import Setting from '@/blocks/Setting';
2727

28+
import { getUrlParam, updateQueryStringParameter } from '@/utils/url';
2829
import styles from './index.less';
2930

3031
const navConfig: INavItem[] = [
@@ -58,8 +59,6 @@ const navConfig: INavItem[] = [
5859
},
5960
];
6061

61-
// const initPageIndex = navConfig.findIndex((t) => `${t.key}` === localStorage.getItem('curPage'));
62-
6362
interface IProps {
6463
mainModel: IMainPageType['state'];
6564
workspaceModel: IWorkspaceModelType['state'];
@@ -75,37 +74,7 @@ function MainPage(props: IProps) {
7574
const [userInfo, setUserInfo] = useState<ILoginUser>();
7675

7776
useEffect(() => {
78-
getUser().then((res) => {
79-
if (res) {
80-
setUserInfo(res);
81-
const hasTeamIcon = navConfig.find((i) => i.key === 'team');
82-
if (res.admin && !hasTeamIcon) {
83-
navConfig.splice(3, 0, {
84-
key: 'team',
85-
icon: '\ue64b',
86-
iconFontSize: 24,
87-
isLoad: false,
88-
component: <Team />,
89-
});
90-
if (localStorage.getItem('curPage') === 'team') {
91-
setActiveNav(navConfig[3]);
92-
}
93-
}
94-
if (!res.admin && hasTeamIcon) {
95-
navConfig.splice(3, 1);
96-
if (localStorage.getItem('curPage') === 'team') {
97-
setActiveNav(navConfig[2]);
98-
}
99-
}
100-
}
101-
});
102-
}, []);
103-
104-
useEffect(() => {
105-
const initPageIndex = navConfig.findIndex((t) => `${t.key}` === localStorage.getItem('curPage'));
106-
const activeIndex = initPageIndex > -1 ? initPageIndex : 2;
107-
navConfig[activeIndex].isLoad = true;
108-
setActiveNav(navConfig[activeIndex]);
77+
handleInitPage();
10978
}, []);
11079

11180
useEffect(() => {
@@ -145,13 +114,48 @@ function MainPage(props: IProps) {
145114
localStorage.setItem('curPage', curPage);
146115
}, [curPage]);
147116

148-
function switchingNav(item: INavItem) {
117+
const handleInitPage = async () => {
118+
const res = await getUser();
119+
if (res) {
120+
setUserInfo(res);
121+
const hasTeamIcon = navConfig.find((i) => i.key === 'team');
122+
if (res.admin && !hasTeamIcon) {
123+
navConfig.splice(3, 0, {
124+
key: 'team',
125+
icon: '\ue64b',
126+
iconFontSize: 24,
127+
isLoad: false,
128+
component: <Team />,
129+
});
130+
if (localStorage.getItem('curPage') === 'team') {
131+
setActiveNav(navConfig[3]);
132+
}
133+
}
134+
if (!res.admin && hasTeamIcon) {
135+
navConfig.splice(3, 1);
136+
if (localStorage.getItem('curPage') === 'team') {
137+
setActiveNav(navConfig[2]);
138+
}
139+
}
140+
}
141+
142+
const urlTab = getUrlParam('tab');
143+
const initPage = urlTab || localStorage.getItem('curPage');
144+
const initPageIndex = navConfig.findIndex((t) => `${t.key}` === initPage);
145+
const activeIndex = initPageIndex > -1 ? initPageIndex : 2;
146+
navConfig[activeIndex].isLoad = true;
147+
setActiveNav(navConfig[activeIndex]);
148+
};
149+
150+
const switchingNav = (item: INavItem) => {
149151
if (item.openBrowser) {
150152
window.open(item.openBrowser, '_blank');
151153
} else {
154+
const newURL = updateQueryStringParameter('tab', item.key);
155+
history.pushState({}, '', newURL);
152156
setActiveNav(item);
153157
}
154-
}
158+
};
155159

156160
const handleLogout = () => {
157161
userLogout().then(() => {

chat2db-client/src/pages/main/workspace/components/WorkspaceLeft/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import TableList from '../TableList';
99
import SaveList from '../SaveList';
1010
import { ExportSizeEnum, ExportTypeEnum } from '@/typings/resultTable';
1111
import { IExportParams } from '@/service/sql';
12-
import { downloadFile } from '@/utils/common';
12+
import { downloadFile } from '@/utils/file';
1313

1414
interface IProps {
1515
className?: string;
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
export function formatParams(obj: { [key: string]: any }) {
2-
const params = new URLSearchParams();
3-
Object.entries(obj).forEach(([key, value]) => {
4-
if (value === undefined || value === null) {
5-
return;
6-
}
7-
if (Array.isArray(value)) {
8-
value.forEach((item) => {
9-
params.append(key, item);
10-
});
11-
} else {
12-
params.append(key, value);
13-
}
14-
});
15-
return params.toString();
16-
}
17-
1+
/**
2+
* 文件下载
3+
* @param url
4+
* @param params
5+
*/
186
export function downloadFile(url: string, params: any) {
197
// 创建POST请求
208
fetch(url, {

chat2db-client/src/utils/url.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* 获取url参数
3+
* @param paramName
4+
* @returns
5+
*/
6+
7+
export function getUrlParam(paramName) {
8+
// 获取当前URL
9+
const currentUrl = window.location.href;
10+
11+
// 获取URL中的查询字符串
12+
const queryString = currentUrl.split('?')[1];
13+
14+
// 如果没有查询字符串,直接返回
15+
if (queryString === undefined) {
16+
return;
17+
}
18+
19+
// 将查询字符串分割成数组
20+
const paramList = queryString.split('&');
21+
22+
// 遍历每个参数
23+
for (let i = 0; i < paramList.length; i++) {
24+
// 检查每个参数的名称是否匹配
25+
const param = paramList[i].split('=')[0];
26+
if (param === paramName) {
27+
// 返回参数值
28+
return paramList[i].split('=')[1];
29+
}
30+
}
31+
32+
// 如果没有找到参数,则返回null
33+
return null;
34+
}
35+
/**
36+
* 更新URL的参数
37+
* @param key
38+
* @param value
39+
* @returns
40+
*/
41+
export function updateQueryStringParameter(key, value) {
42+
const uri = window.location.href;
43+
const host = window.location.host;
44+
if (!value) {
45+
return uri;
46+
}
47+
const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
48+
const separator = uri.indexOf('?') !== -1 ? '&' : '?';
49+
if (uri.match(re)) {
50+
return uri.replace(re, '$1' + key + '=' + value + '$2');
51+
} else {
52+
return host + separator + key + '=' + value;
53+
}
54+
}
55+
56+
/**
57+
* 格式化参数
58+
* @param obj
59+
* @returns
60+
*/
61+
export function formatParams(obj: { [key: string]: any }) {
62+
const params = new URLSearchParams();
63+
Object.entries(obj).forEach(([key, value]) => {
64+
if (value === undefined || value === null) {
65+
return;
66+
}
67+
if (Array.isArray(value)) {
68+
value.forEach((item) => {
69+
params.append(key, item);
70+
});
71+
} else {
72+
params.append(key, value);
73+
}
74+
});
75+
return params.toString();
76+
}

0 commit comments

Comments
 (0)