-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useActivityTimer hook for locking the app
- Loading branch information
Showing
4 changed files
with
90 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 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 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,74 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { App } from "@capacitor/app"; | ||
import { useAppDispatch } from "../../../../store/hooks"; | ||
import { logout } from "../../../../store/reducers/stateCache"; | ||
|
||
const timeout = process.env.NODE_ENV === "development" ? 3600000 : 60000; // 1h/1min | ||
const pauseTimeout = timeout / 2; | ||
const useActivityTimer = () => { | ||
const dispatch = useAppDispatch(); | ||
const [pauseTimestamp, setPauseTimestamp] = useState(0); | ||
const timer = useRef<NodeJS.Timeout | null>(null); | ||
|
||
const clearTimer = () => { | ||
if (timer.current) { | ||
clearTimeout(timer.current); | ||
} | ||
}; | ||
|
||
const setActivityTimer = () => { | ||
clearTimer(); | ||
timer.current = setTimeout(() => { | ||
dispatch(logout()); | ||
}, timeout); | ||
}; | ||
|
||
const handleActivity = () => { | ||
setActivityTimer(); | ||
}; | ||
|
||
useEffect(() => { | ||
const pauseListener = App.addListener("pause", () => { | ||
const now = new Date().getTime(); | ||
setPauseTimestamp(now); | ||
}); | ||
|
||
const resumeListener = App.addListener("resume", () => { | ||
const now = new Date().getTime(); | ||
if (now - pauseTimestamp > pauseTimeout) { | ||
dispatch(logout()); | ||
} | ||
}); | ||
|
||
return () => { | ||
pauseListener.remove(); | ||
resumeListener.remove(); | ||
clearTimer(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
const events = [ | ||
"load", | ||
"mousemove", | ||
"touchstart", | ||
"touchmove", | ||
"click", | ||
"focus", | ||
"keydown", | ||
"scroll", | ||
]; | ||
events.forEach((event) => { | ||
window.addEventListener(event, handleActivity); | ||
}); | ||
|
||
return () => { | ||
events.forEach((event) => { | ||
window.removeEventListener(event, handleActivity); | ||
}); | ||
clearTimer(); | ||
}; | ||
}, []); | ||
}; | ||
|
||
export { useActivityTimer }; |