Skip to content

Commit

Permalink
feat(UI): implement auto mining (#17)
Browse files Browse the repository at this point in the history
Description
---
Implement auto mining behavior based on user activity 
Motivation and Context

How Has This Been Tested?
---
Manually

What process can a PR reviewer use to test or verify this change?
---
Open our app, click on Auto Miner switch button and do move mouse inside
app for 30 seconds

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
Misieq01 authored Aug 5, 2024
1 parent 4db3b0d commit ef35e97
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
"react-idle-timer": "^5.7.2",
"zustand": "^4.5.4"
},
"devDependencies": {
Expand Down
49 changes: 48 additions & 1 deletion src/containers/SideBar/Miner/components/AutoMiner.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
import { FormGroup, Switch, Stack, Typography } from '@mui/material';
import { AutoMinerContainer } from '../styles';
import useAppStateStore from '../../../../store/appStateStore';
import { useIdleTimer } from 'react-idle-timer'
import React from 'react';

function AutoMiner() {

const { setBackground, startMining, stopMining,setIsMining,isAutoMining,setIsAutoMining } = useAppStateStore(state => ({
startMining: state.startMining,
stopMining: state.stopMining,
setBackground: state.setBackground,
setIsMining: state.setIsMining,
setIsAutoMining: state.setIsAutoMining,
isAutoMining: state.isAutoMining,

}));

const enableAutoMining = () => {
startMining();
setIsMining(true);
setBackground('mining');
};
const disableAutoMining = () => {
stopMining();
setIsMining(false);
setBackground('idle');
};


const { start, pause } = useIdleTimer({ timeout: 1000 * 30,startManually: true, onIdle: enableAutoMining, onActive:disableAutoMining, events: ["mousemove"]});


React.useEffect(() => {
if (isAutoMining) {
start();
} else {
pause();
}
}, [isAutoMining]);

const handleAutoMining = () => {
if (isAutoMining) {
setIsAutoMining(false)
disableAutoMining();
} else {
setIsAutoMining(true);
}
};


return (
<Stack direction="column" spacing={1}>
<AutoMinerContainer>
Expand All @@ -12,7 +59,7 @@ function AutoMiner() {
</Typography>
</Stack>
<FormGroup>
<Switch focusVisibleClassName=".Mui-focusVisible" disableRipple />
<Switch focusVisibleClassName=".Mui-focusVisible" disableRipple checked={isAutoMining} onChange={handleAutoMining}/>
</FormGroup>
</AutoMinerContainer>
</Stack>
Expand Down
4 changes: 4 additions & 0 deletions src/store/appStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface AppState {
setWallet: (value: { balance: number }) => void;
isMining: boolean;
setIsMining: (value: boolean) => void;
isAutoMining: boolean;
setIsAutoMining: (value: boolean) => void;
sidebarOpen: boolean;
setSidebarOpen: (value: boolean) => void;

Expand Down Expand Up @@ -74,6 +76,8 @@ const useAppStateStore = create<AppState>((set) => ({
setWallet: (value) => set({ wallet: value }),
isMining: false,
setIsMining: (value) => set({ isMining: value }),
isAutoMining: false,
setIsAutoMining: (value) => set({ isAutoMining: value }),
sidebarOpen: false,
setSidebarOpen: (value) => set({ sidebarOpen: value }),

Expand Down

0 comments on commit ef35e97

Please sign in to comment.