mirror of https://github.com/openspug/spug
# 添加web终端搜索功能
parent
0fc0479619
commit
e604283141
|
@ -5,12 +5,12 @@
|
||||||
*/
|
*/
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { observer } from 'mobx-react';
|
import { observer } from 'mobx-react';
|
||||||
import { Tabs, Tree, Spin } from 'antd';
|
import { Tabs, Tree, Input, Spin } from 'antd';
|
||||||
import { FolderOutlined, FolderOpenOutlined, CloudServerOutlined } from '@ant-design/icons';
|
import { FolderOutlined, FolderOpenOutlined, CloudServerOutlined, SearchOutlined } from '@ant-design/icons';
|
||||||
import { NotFound, AuthButton } from 'components';
|
import { NotFound, AuthButton } from 'components';
|
||||||
import Terminal from './Terminal';
|
import Terminal from './Terminal';
|
||||||
import FileManager from './FileManager';
|
import FileManager from './FileManager';
|
||||||
import { http, hasPermission } from 'libs';
|
import { http, hasPermission, includes } from 'libs';
|
||||||
import styles from './index.module.less';
|
import styles from './index.module.less';
|
||||||
import LogoSpugText from 'layout/logo-spug-txt.png';
|
import LogoSpugText from 'layout/logo-spug-txt.png';
|
||||||
import lds from 'lodash';
|
import lds from 'lodash';
|
||||||
|
@ -19,7 +19,9 @@ import lds from 'lodash';
|
||||||
function WebSSH(props) {
|
function WebSSH(props) {
|
||||||
const [visible, setVisible] = useState(false);
|
const [visible, setVisible] = useState(false);
|
||||||
const [fetching, setFetching] = useState(true);
|
const [fetching, setFetching] = useState(true);
|
||||||
|
const [rawTreeData, setRawTreeData] = useState([]);
|
||||||
const [treeData, setTreeData] = useState([]);
|
const [treeData, setTreeData] = useState([]);
|
||||||
|
const [searchValue, setSearchValue] = useState();
|
||||||
const [hosts, setHosts] = useState([]);
|
const [hosts, setHosts] = useState([]);
|
||||||
const [activeId, setActiveId] = useState();
|
const [activeId, setActiveId] = useState();
|
||||||
const [hostId, setHostId] = useState();
|
const [hostId, setHostId] = useState();
|
||||||
|
@ -28,11 +30,30 @@ function WebSSH(props) {
|
||||||
window.document.title = 'Spug web terminal'
|
window.document.title = 'Spug web terminal'
|
||||||
window.addEventListener('beforeunload', leaveTips)
|
window.addEventListener('beforeunload', leaveTips)
|
||||||
http.get('/api/host/group/?with_hosts=1')
|
http.get('/api/host/group/?with_hosts=1')
|
||||||
.then(res => setTreeData(res.treeData))
|
.then(res => setRawTreeData(res.treeData))
|
||||||
.finally(() => setFetching(false))
|
.finally(() => setFetching(false))
|
||||||
return () => window.removeEventListener('beforeunload', leaveTips)
|
return () => window.removeEventListener('beforeunload', leaveTips)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (searchValue) {
|
||||||
|
const newTreeData = []
|
||||||
|
const loop = (data) => {
|
||||||
|
for (let item of data) {
|
||||||
|
if (item.children) {
|
||||||
|
loop(item.children)
|
||||||
|
} else if (item.isLeaf && includes(item.title, searchValue)) {
|
||||||
|
newTreeData.push(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop(rawTreeData)
|
||||||
|
setTreeData(newTreeData)
|
||||||
|
} else {
|
||||||
|
setTreeData(rawTreeData)
|
||||||
|
}
|
||||||
|
}, [rawTreeData, searchValue])
|
||||||
|
|
||||||
function leaveTips(e) {
|
function leaveTips(e) {
|
||||||
e.returnValue = '确定要离开页面?'
|
e.returnValue = '确定要离开页面?'
|
||||||
}
|
}
|
||||||
|
@ -97,6 +118,8 @@ function WebSSH(props) {
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.hosts}>
|
<div className={styles.hosts}>
|
||||||
<Spin spinning={fetching}>
|
<Spin spinning={fetching}>
|
||||||
|
<Input allowClear className={styles.search} prefix={<SearchOutlined style={{color: '#999'}}/>}
|
||||||
|
placeholder="输入检索" onChange={e => setSearchValue(e.target.value)}/>
|
||||||
<Tree.DirectoryTree
|
<Tree.DirectoryTree
|
||||||
defaultExpandAll
|
defaultExpandAll
|
||||||
expandAction="doubleClick"
|
expandAction="doubleClick"
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.hosts {
|
.hosts {
|
||||||
margin-top: 12px;
|
|
||||||
:global(.ant-tree-node-content-wrapper) {
|
:global(.ant-tree-node-content-wrapper) {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
|
@ -28,6 +27,11 @@
|
||||||
:global(.ant-tree) {
|
:global(.ant-tree) {
|
||||||
background-color: #fafafa;
|
background-color: #fafafa;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.search {
|
||||||
|
margin: 12px 10px;
|
||||||
|
width: 260px;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.content {
|
.content {
|
||||||
|
|
Loading…
Reference in New Issue