This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
generated from tyn1998/chrome-extension-boilerplate-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multiple tabs & react-data-grid & toggled autoScroll (#12)
* 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
Showing
12 changed files
with
2,521 additions
and
2,579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ | |
</head> | ||
|
||
<body> | ||
<div id="app-container"></div> | ||
<div id="panel"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.