upd
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user