mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-04-10 16:30:18 +02:00
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
// React
|
|
import { useEffect, useRef } from 'react';
|
|
import DeviceInfo from 'react-native-device-info';
|
|
// Context
|
|
import { useTeamConnexion } from "../context/teamConnexionContext";
|
|
// Hook
|
|
import { useSocketCommands } from "./useSocketCommands";
|
|
|
|
export const useSendDeviceInfo = () => {
|
|
const { emitBattery, emitDeviceInfo } = useSocketCommands();
|
|
const { loggedIn } = useTeamConnexion();
|
|
const isMounted = useRef(true);
|
|
|
|
useEffect(() => {
|
|
isMounted.current = true;
|
|
|
|
if (!loggedIn) return;
|
|
|
|
const sendInfo = async () => {
|
|
const [brand, model, name] = await Promise.all([
|
|
DeviceInfo.getBrand(),
|
|
DeviceInfo.getModel(),
|
|
DeviceInfo.getDeviceName()
|
|
]);
|
|
if (!isMounted) return;
|
|
emitDeviceInfo({model: brand + " " + model, name: name});
|
|
};
|
|
|
|
const sendBattery = async () => {
|
|
const level = await DeviceInfo.getBatteryLevel();
|
|
if (!isMounted) return;
|
|
emitBattery(Math.round(level * 100));
|
|
};
|
|
|
|
sendInfo();
|
|
sendBattery();
|
|
|
|
const batteryCheckInterval = setInterval(() => sendBattery(), 5*60*1000); // 5 minutes
|
|
|
|
return () => {
|
|
isMounted.current = false;
|
|
clearInterval(batteryCheckInterval);
|
|
};
|
|
}, [emitBattery, emitDeviceInfo, loggedIn]);
|
|
|
|
return null;
|
|
};
|