Skip to content

Commit 5fa92f6

Browse files
author
fanjin.fjy
committed
feat: Optimize table
1 parent 2d5d226 commit 5fa92f6

14 files changed

Lines changed: 203 additions & 85 deletions

File tree

chat2db-client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"react-monaco-editor": "^0.52.0",
4040
"react-sortablejs": "^6.1.4",
4141
"sql-formatter": "^12.2.1",
42+
"styled-components": "^6.0.1",
4243
"umi": "^4.0.70",
4344
"umi-request": "^1.4.0",
4445
"uuid": "^9.0.0"

chat2db-client/src/components/Console/ChatInput/index.less

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
.chat_wrapper {
1+
.chatWrapper {
22
display: flex;
33
align-items: center;
44
padding: 12px 20px;
55
box-sizing: border-box;
66
border-bottom: 1px solid var(--color-border-secondary);
77
}
88

9-
.chat_ai {
9+
.chatShortcut {
10+
11+
padding: 4px 8px;
12+
color: #8a9099;
13+
font-size: 12px;
14+
line-height: 1;
15+
white-space: nowrap;
16+
background-color: #fffc;
17+
border-radius: 4px;
18+
border: 1px solid #d0d5d8;
19+
20+
margin-right: 10px;
21+
}
22+
23+
.chatAi {
1024
width: 16px;
1125
height: 16px;
1226
margin-right: 14px;
@@ -34,4 +48,16 @@
3448
background-color: var(--color-bg-elevated);
3549
padding: 4px 12px;
3650

51+
}
52+
53+
.aiSelectedTable{
54+
display: flex;
55+
flex-direction: column;
56+
max-width: 140px;
57+
}
58+
59+
.aiSelectedTableTips{
60+
padding-bottom: 4px;
61+
border-bottom: 1px solid var(--color-border-secondary);
62+
margin-bottom: 4px
3763
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import AIImg from '@/assets/img/ai.svg';
44
import { Checkbox, Dropdown, Input, Popover } from 'antd';
55
import i18n from '@/i18n/';
66
import Iconfont from '@/components/Iconfont';
7+
import { WarningOutlined } from '@ant-design/icons';
78

89
interface IProps {
910
value?: string;
@@ -16,16 +17,20 @@ interface IProps {
1617

1718
function ChatInput(props: IProps) {
1819
const onPressEnter = (e: any) => {
19-
if (e.target.value) {
20-
props.onPressEnter && props.onPressEnter(e.target.value);
20+
if (!e.target.value) {
21+
return;
2122
}
23+
props.onPressEnter && props.onPressEnter(e.target.value);
2224
};
2325

2426
const renderSelectTable = () => {
2527
const { tables, selectedTables, onSelectTables } = props;
2628
return tables && tables.length ? (
27-
<div>
28-
<span></span>
29+
<div className={styles.aiSelectedTable}>
30+
<span className={styles.aiSelectedTableTips}>
31+
{/* <WarningOutlined style={{color: 'yellow'}}/> */}
32+
{i18n('chat.input.remain.tooltip')}
33+
</span>
2934
<Checkbox.Group
3035
style={{ width: '120px' }}
3136
options={tables || []}
@@ -44,6 +49,7 @@ function ChatInput(props: IProps) {
4449
const remainCnt = 10;
4550
return (
4651
<div className={styles.suffixBlock}>
52+
{/* <div className={styles.chatShortcut}> ⌘ + ↵ </div> */}
4753
<div className={styles.tableSelectBlock}>
4854
<Popover content={renderSelectTable()} placement="bottom">
4955
<Iconfont code="&#xe618;" />
@@ -55,12 +61,13 @@ function ChatInput(props: IProps) {
5561
};
5662

5763
return (
58-
<div className={styles.chat_wrapper}>
59-
<img className={styles.chat_ai} src={AIImg} />
64+
<div className={styles.chatWrapper}>
65+
<img className={styles.chatAi} src={AIImg} />
6066
<Input
6167
defaultValue={props.value}
6268
bordered={false}
6369
placeholder={i18n('workspace.ai.input.placeholder')}
70+
// onKeyUp={onPressEnter}
6471
onPressEnter={onPressEnter}
6572
suffix={renderSuffix()}
6673
/>

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ function Console(props: IProps) {
8080
}
8181
}, [appendValue]);
8282

83-
const tableListName = useMemo(() => (props.tables || []).map((t) => t.name), [props.tables]);
83+
const tableListName = useMemo(() => {
84+
const tableList = (props.tables || []).map((t) => t.name);
85+
86+
// 默认选中前八个
87+
setSelectedTables(tableList.slice(0, 8));
88+
89+
return tableList;
90+
}, [props.tables]);
8491

8592
const handleAiChat = (content: string, promptType: IPromptType) => {
8693
const { dataSourceId, databaseName, schemaName } = executeParams;
@@ -227,7 +234,7 @@ function Console(props: IProps) {
227234
onSave={saveConsole}
228235
onExecute={executeSQL}
229236
options={props.editorOptions}
230-
// onChange={}
237+
// onChange={}
231238
/>
232239
{/* <Modal open={modelConfig.open}>{modelConfig.content}</Modal> */}
233240
<Drawer open={isAiDrawerOpen} getContainer={false} mask={false} onClose={() => setIsAiDrawerOpen(false)}>

chat2db-client/src/components/SearchResult/TableBox.less

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,49 @@
1717

1818

1919
.statusBar {
20-
position: absolute;
20+
position: sticky;
2121
bottom: 0;
22-
left: 0;
23-
right: 0;
22+
z-index: 30;
2423
padding: 4px 8px;
2524
font-size: 12px;
2625
display: flex;
26+
justify-content: start;
2727
align-items: center;
28+
border-top: 1px solid var(--color-border-secondary);
2829
background-color: var(--color-bg-elevated);
2930
}
3031

3132
.tableItem {
3233
width: 100%;
33-
.f-lines(1);
34+
// .f-lines(1);
3435
cursor: pointer;
36+
display: flex;
37+
align-items: center;
38+
justify-content: space-between;
39+
40+
// height: 100%;
41+
max-height: 120px;
42+
overflow-y: auto;
43+
44+
&::-webkit-scrollbar {
45+
display: none;
46+
}
3547

3648
&:hover .tableHoverBox {
37-
display: flex;
49+
// display: flex !important;
50+
display: block;
3851
}
3952
}
4053

4154
.tableHoverBox {
55+
width: 40px;
4256
display: none;
4357
align-items: center;
44-
position: absolute;
58+
// position: absolute;
4559
background-color: var(--color-bg);
46-
top: 0;
47-
right: 0;
48-
bottom: 0;
60+
// top: 0;
61+
// right: 0;
62+
// bottom: 0;
4963

5064
i {
5165
font-size: 15px;
@@ -54,5 +68,8 @@
5468

5569
i:hover {
5670
color: var(--custom-primary-color);
71+
5772
}
58-
}
73+
}
74+
75+
.table {}

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

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import React, { useEffect, useMemo, useState } from 'react';
22
import { TableDataType } from '@/constants/table';
33
import { IManageResultData, ITableHeaderItem } from '@/typings/database';
44
import { formatDate } from '@/utils/date';
5-
import { message, Modal, Table } from 'antd';
6-
import { applyTransforms, BaseTable, BaseTableProps, collectNodes, makeColumnResizeTransform } from 'ali-react-table';
5+
import { Button, message, Modal, Table } from 'antd';
6+
import antd from 'antd';
7+
import { BaseTable, ArtColumn, useTablePipeline, features } from 'ali-react-table';
78
import Iconfont from '../Iconfont';
89
import classnames from 'classnames';
910
import StateIndicator from '../StateIndicator';
1011
import MonacoEditor from '../Console/MonacoEditor';
12+
import { useTheme } from '@/hooks/useTheme';
13+
import styled from 'styled-components';
1114
import styles from './TableBox.less';
15+
import { ThemeType } from '@/constants';
1216

1317
interface ITableProps {
1418
className?: string;
@@ -20,11 +24,30 @@ interface IViewTableCellData {
2024
name: string;
2125
value: any;
2226
}
27+
// --bgcolor: #333;
28+
// --header-bgcolor: #45494f;
29+
const DarkSupportBaseTable: any = styled(BaseTable)`
30+
&.dark {
31+
--bgcolor: #131418;
32+
--header-bgcolor: #0a0b0c;
33+
--hover-bgcolor: #46484a;
34+
--header-hover-bgcolor: #606164;
35+
--highlight-bgcolor: #191a1b;
36+
--header-highlight-bgcolor: #191a1b;
37+
--color: #dadde1;
38+
--header-color: #dadde1;
39+
--lock-shadow: rgb(37 37 37 / 0.5) 0 0 6px 2px;
40+
--border-color: #3c4045;
41+
}
42+
`;
2343

2444
export default function TableBox(props: ITableProps) {
2545
const { className, data, key } = props;
2646
const { headerList, dataList, duration, description } = data || {};
2747
const [viewTableCellData, setViewTableCellData] = useState<IViewTableCellData | null>(null);
48+
const [appTheme] = useTheme();
49+
50+
const isDarkTheme = useMemo(() => appTheme.backgroundColor === ThemeType.Dark, [appTheme]);
2851

2952
function viewTableCell(data: IViewTableCellData) {
3053
setViewTableCellData(data);
@@ -39,24 +62,28 @@ export default function TableBox(props: ITableProps) {
3962
setViewTableCellData(null);
4063
}
4164

42-
const columns = useMemo(
65+
const columns: ArtColumn[] = useMemo(
4366
() =>
44-
(headerList || []).map((item: any) => ({
45-
title: item.name,
46-
dataIndex: item.name,
67+
(headerList || []).map((item, index) => ({
4768
code: item.name,
69+
name: item.name,
4870
key: item.name,
49-
type: item.dataType,
50-
sorter: (a: any, b: any) => a[item.name] - b[item.name],
51-
render: (value: any) => (
52-
<div className={styles.tableItem}>
53-
<div className={styles.tableHoverBox}>
54-
<Iconfont code="&#xe606;" onClick={viewTableCell.bind(null, { name: item.name, value })} />
55-
<Iconfont code="&#xeb4e;" onClick={copyTableCell.bind(null, { name: item.name, value })} />
71+
lock: index === 0,
72+
width: 120,
73+
// type: item.dataType,
74+
// sorter: (a: any, b: any) => a[item.name] - b[item.name],
75+
render: (value: any, row: any, rowIndex: number) => {
76+
console.log('rowIndex', rowIndex);
77+
return (
78+
<div className={styles.tableItem}>
79+
<div>{value}</div>
80+
<div className={styles.tableHoverBox}>
81+
<Iconfont code="&#xe606;" onClick={viewTableCell.bind(null, { name: item.name, value })} />
82+
<Iconfont code="&#xeb4e;" onClick={copyTableCell.bind(null, { name: item.name, value })} />
83+
</div>
5684
</div>
57-
{value}
58-
</div>
59-
),
85+
);
86+
},
6087
})),
6188
[headerList],
6289
);
@@ -71,11 +98,11 @@ export default function TableBox(props: ITableProps) {
7198
const { dataType: type } = headerList[index] || {};
7299
// console.log('headerList[rowIndex]', headerList[rowIndex]);
73100
if (type === TableDataType.DATETIME && i) {
74-
rowData[columns[index].title] = formatDate(i, 'yyyy-MM-dd hh:mm:ss');
101+
rowData[columns[index].name] = formatDate(i, 'yyyy-MM-dd hh:mm:ss');
75102
} else if (i === null) {
76-
rowData[columns[index].title] = '[null]';
103+
rowData[columns[index].name] = '[null]';
77104
} else {
78-
rowData[columns[index].title] = i;
105+
rowData[columns[index].name] = i;
79106
}
80107
});
81108
rowData.key = rowIndex;
@@ -84,15 +111,32 @@ export default function TableBox(props: ITableProps) {
84111
}
85112
}, [dataList, columns]);
86113

87-
console.log('dataList', dataList);
114+
const pipeline = useTablePipeline()
115+
.input({ dataSource: tableData, columns })
116+
.use(
117+
features.columnResize({
118+
fallbackSize: 120,
119+
minSize: 60,
120+
maxSize: 1080,
121+
// handleBackground: '#ddd',
122+
// handleHoverBackground: '#aaa',
123+
// handleActiveBackground: '#89bff7',
124+
}),
125+
);
88126
return (
89127
<div className={classnames(className, styles.tableBox)}>
90128
{columns.length ? (
91-
<Table pagination={false} columns={columns} dataSource={tableData} scroll={{ y: '100vh' }} size="small" />
129+
// <Table pagination={false} columns={columns} dataSource={tableData} scroll={{ y: '100vh' }} size="small" />
130+
<>
131+
<DarkSupportBaseTable
132+
className={classnames({ dark: isDarkTheme }, props.className, styles.table)}
133+
{...pipeline.getProps()}
134+
/>
135+
<div className={styles.statusBar}>{`结果:${description}. 耗时:${duration}ms`}</div>
136+
</>
92137
) : (
93138
<StateIndicator state="success" text="执行成功" />
94139
)}
95-
<div className={styles.statusBar}>{`结果:${description}. 耗时:${duration}ms`}</div>
96140
<Modal
97141
title={viewTableCellData?.name}
98142
open={!!viewTableCellData?.name}
@@ -116,20 +160,9 @@ export default function TableBox(props: ITableProps) {
116160
options={{
117161
readOnly: true,
118162
}}
119-
></MonacoEditor>
163+
/>
120164
</div>
121165
</Modal>
122166
</div>
123167
);
124168
}
125-
function pipeline(arg0: {
126-
sizes: number;
127-
onChangeSizes: React.Dispatch<React.SetStateAction<number>>;
128-
appendExpander: boolean;
129-
expanderVisibility: string;
130-
disableUserSelectWhenResizing: boolean;
131-
minSize: number;
132-
maxSize: number;
133-
}): import('ali-react-table').Transform<{ columns: any; dataSource: any }> {
134-
throw new Error('Function not implemented.');
135-
}

chat2db-client/src/components/SearchResult/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88

99
.tabs {
10-
padding-top: 10px;
10+
padding-top: 8px;
1111
border-bottom: 1px solid var(--color-border);
1212
}
1313

0 commit comments

Comments
 (0)