This commit is contained in:
2023-10-23 17:46:39 +05:00
parent 44bae35cc9
commit efd370e79e
4 changed files with 45 additions and 40 deletions
+27 -2
View File
@@ -1,13 +1,38 @@
import { useEffect, useState } from "react";
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useRef, useState } from "react";
import useModalStore from "../../stores/useModalStore";
import ky from "ky";
import { useParams } from "react-router-dom";
function AFKTimerModal() {
const setModal = useModalStore((state) => state.setModal);
const [afkTimer, setAfkTimer] = useState<number>(60);
const afkTimerRef = useRef<number>();
afkTimerRef.current = afkTimer;
const params = useParams();
async function endSession() {
await ky.post(
`${import.meta.env.VITE_COORD_URL}/active_sessions/${params.id}/end`
);
}
async function checkAFK(interval: number) {
console.log(afkTimerRef.current);
if (afkTimerRef.current && afkTimerRef.current <= 1) {
clearInterval(interval);
await endSession();
window.location.reload();
return;
}
setAfkTimer((prev) => prev - 1);
}
useEffect(() => {
const interval = setInterval(() => {
setAfkTimer((prev) => prev - 1);
checkAFK(interval);
}, 1000);
return () => {