Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
feat: multiple tabs & react-data-grid & toggled autoScroll (#12)
Browse files Browse the repository at this point in the history
* react-data-grid demo

* no margin and no padding for <body>

* more RDG properties

* new code structure & multiple tabs support

* automatically scroll to bottom

* now autoScroll can be toggled

* start & stop -> toggleCapturing
  • Loading branch information
tyn1998 authored Jul 17, 2022
1 parent f9df346 commit b55db74
Show file tree
Hide file tree
Showing 12 changed files with 2,521 additions and 2,579 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"@hot-loader/react-dom": "^17.0.2",
"echarts": "^5.3.2",
"react": "^17.0.2",
"react-data-grid": "^7.0.0-beta.13",
"react-dom": "^17.0.2",
"react-hot-loader": "^4.13.0"
"react-hot-loader": "^4.13.0",
"react-tabs": "^4.2.1"
},
"devDependencies": {
"@babel/core": "^7.17.0",
Expand Down
7 changes: 0 additions & 7 deletions src/pages/Panel/Panel.css

This file was deleted.

71 changes: 0 additions & 71 deletions src/pages/Panel/Panel.jsx

This file was deleted.

Empty file removed src/pages/Panel/index.css
Empty file.
2 changes: 1 addition & 1 deletion src/pages/Panel/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</head>

<body>
<div id="app-container"></div>
<div id="panel"></div>
</body>
</html>
5 changes: 2 additions & 3 deletions src/pages/Panel/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React from 'react';
import { render } from 'react-dom';

import Panel from './Panel';
import './index.css';
import Panel from '../../ui/Panel';

render(<Panel />, window.document.querySelector('#app-container'));
render(<Panel />, window.document.querySelector('#panel'));

if (module.hot) module.hot.accept();
99 changes: 99 additions & 0 deletions src/ui/MessagesTab/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { useState, useEffect, useRef } from 'react';
import { startCapturing, stopCapturing, getMessages } from '../../capturer';
import DataGrid from 'react-data-grid';

const INTERVAL = 500;

const commonColumnProperties = {
resizable: true,
sortable: true,
filterable: true,
};

const columns = [
{ key: 'id', name: 'ID', width: 50, frozen: true },
{ key: 'time', name: 'Time', width: 150, frozen: true },
{ key: 'message', name: 'Message' },
].map((c) => ({ ...c, ...commonColumnProperties }));

const MessagesTab = () => {
const [capturing, setCapturing] = useState(false);
const [messages, setMessages] = useState([]);
const [timer, setTimer] = useState(null);
const [bottomRow, setBottomRow] = useState(-1);
const [autoScroll, setAutoScroll] = useState(true);

const gridRef = useRef(null);

useEffect(() => {
gridRef.current && autoScroll && gridRef.current.scrollToRow(bottomRow);
}, [autoScroll, bottomRow]);

const toggleAutoScroll = () => {
setAutoScroll(!autoScroll);
};

const addMessages = () => {
getMessages()
.then((newMessages) => {
setMessages((oldMessages) => [...oldMessages, ...newMessages]);
if (newMessages.length > 0) {
setBottomRow((oldBottomRow) => oldBottomRow + newMessages.length);
}
})
.catch((error) => {
console.error('Getting messages failed!');
console.error(error.stack || error);
});
};

const toggleCapturing = () => {
if (capturing === true) {
stopCapturing()
.then(() => {
setCapturing(false);
clearInterval(timer);
setTimer(null);
})
.catch((error) => {
console.error('Stoping capturing failed!');
console.error(error.stack || error);
});
} else {
startCapturing()
.then(() => {
setCapturing(true);
setTimer(setInterval(() => addMessages(), INTERVAL));
})
.catch((error) => {
console.error('Starting capturing failed!');
console.error(error.stack || error);
});
}
};

return (
<div>
<button onClick={toggleCapturing}>toggleCapturing</button>
<button onClick={toggleAutoScroll}>toggleAutoScroll</button>

<DataGrid
style={{ fontSize: '10px', height: 'calc(100vh - 78px' }}
ref={gridRef}
columns={columns}
rows={messages.map((msg, index) => {
return {
id: index,
time: msg.time,
message: msg.msg,
};
})}
rowKeyGetter={(row) => row.id}
headerRowHeight={30}
rowHeight={20}
/>
</div>
);
};

export default MessagesTab;
9 changes: 9 additions & 0 deletions src/ui/Panel.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
/* height: 100%; */
/* padding: 0; */
/* margin: 0; */
/* font-size: 13px; */
/* line-height: 1.6; */
/* color: #333; */
/* background-color: transparent; */
}
25 changes: 25 additions & 0 deletions src/ui/Panel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import './Panel.css';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
import MessagesTab from './MessagesTab';

const Panel = () => {
return (
<Tabs forceRenderTabPanel={true}>
<TabList>
<Tab>Messages</Tab>
<Tab>XXX</Tab>
</TabList>

<TabPanel>
<MessagesTab />
</TabPanel>
<TabPanel>
<div>XXX</div>
</TabPanel>
</Tabs>
);
};

export default Panel;
File renamed without changes.
2 changes: 2 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const ASSET_PATH = process.env.ASSET_PATH || '/';

const alias = {
'react-dom': '@hot-loader/react-dom',
'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
'react/jsx-runtime': 'react/jsx-runtime.js',
};

// load the secrets
Expand Down
Loading

0 comments on commit b55db74

Please sign in to comment.