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