Refactor WakeComponent: update ping interval and starting time handling, streamline ping logic, and improve dependency management

master^2
Tudor Stanciu 2024-11-16 02:03:47 +02:00
parent 09e447f4b3
commit 618bfb38e1
1 changed files with 14 additions and 13 deletions

View File

@ -12,6 +12,9 @@ const initialState = { on: false };
const defaultPingInterval = 1200000; //20 minutes
const defaultStartingTime = 300000; //5 minutes
const pingInterval = parseInt(process.env.REACT_APP_MACHINE_PING_INTERVAL ?? "") || defaultPingInterval;
const startingTime = parseInt(process.env.REACT_APP_MACHINE_STARTING_TIME ?? "") || defaultStartingTime;
type Props = {
machine: Machine;
addLog: (message: string) => void;
@ -24,9 +27,6 @@ const WakeComponent: React.FC<Props> = ({ machine, addLog, disabled }) => {
const { t } = useTranslation();
const pingInterval = process.env.REACT_APP_MACHINE_PING_INTERVAL || defaultPingInterval;
const startingTime = process.env.REACT_APP_MACHINE_STARTING_TIME || defaultStartingTime;
const getCurrentDateTime = useCallback(() => {
const currentDateTime = Date.now();
const result = t("DATE_FORMAT", {
@ -45,11 +45,12 @@ const WakeComponent: React.FC<Props> = ({ machine, addLog, disabled }) => {
setState(prev => ({ ...prev, on: result.success }));
log(`[Ping]: Success: ${result.success}. Status: ${result.status}`);
// if (result.success) {
// setTimeout(() => {
// setTrigger(prev => !prev);
// }, pingInterval);
// }
// trigger the next ping
if (result.success) {
setTimeout(() => {
setTrigger(prev => !prev);
}, pingInterval);
}
},
onError: () => {
// to do: handle error
@ -78,16 +79,16 @@ const WakeComponent: React.FC<Props> = ({ machine, addLog, disabled }) => {
}
}
);
}, [log, startingTime, machine.machineId]);
}, [log, machine.machineId]);
const pingInLoop = useCallback(async () => {
const pingMachine = useCallback(async () => {
if (disabled) return;
pingMachineTrigger({ machineId: machine.machineId });
}, [machine, log, pingInterval, disabled]);
}, [machine.machineId, pingMachineTrigger, disabled]);
useEffect(() => {
pingInLoop();
}, [trigger, pingInLoop]);
pingMachine();
}, [trigger, pingMachine]);
const handleWakeClick = (event: any) => {
wakeMachine();