hooks/useNetworkStatus.ts
Annotation non disponible
Lance npm run annotate (nécessite ANTHROPIC_API_KEY dans .env.local) pour générer une annotation française par Claude Haiku 4.5.
Concepts détectés — comprends la théorie
Hooks React
5 occurrencesCe fichier utilise des hooks React. Les hooks sont la façon moderne de gérer l'état et les effets dans React. Voir l'architecture mobile pour le pattern complet.
Voir l'article général
Appel API (fetch)
1 occurrenceCe fichier fait des appels HTTP. Voir la convention API wari.pro pour les patterns de gestion d'erreur et de Bearer token.
Voir l'article général
1 export
useNetworkStatus
Code source· typescript
import { useEffect, useState } from 'react';
import { API_URL } from '@/lib/constants';
const PING_URL = `${API_URL}/mobile/health`;
const PING_INTERVAL_MS = 30000;
const PING_TIMEOUT_MS = 3000;
export function useNetworkStatus() {
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
let mounted = true;
const check = async () => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), PING_TIMEOUT_MS);
try {
const r = await fetch(PING_URL, { method: 'GET', signal: controller.signal });
if (mounted) setIsOnline(r.ok);
} catch {
if (mounted) setIsOnline(false);
} finally {
clearTimeout(timer);
}
};
check();
const interval = setInterval(check, PING_INTERVAL_MS);
return () => {
mounted = false;
clearInterval(interval);
};
}, []);
return isOnline;
}
import { useEffect, useState } from 'react';
import { API_URL } from '@/lib/constants';
const PING_URL = `${API_URL}/mobile/health`;
const PING_INTERVAL_MS = 30000;
const PING_TIMEOUT_MS = 3000;
export function useNetworkStatus() {
const [isOnline, setIsOnline] = useState(true);
useEffect(() => {
let mounted = true;
const check = async () => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), PING_TIMEOUT_MS);
try {
const r = await fetch(PING_URL, { method: 'GET', signal: controller.signal });
if (mounted) setIsOnline(r.ok);
} catch {
if (mounted) setIsOnline(false);
} finally {
clearTimeout(timer);
}
};
check();
const interval = setInterval(check, PING_INTERVAL_MS);
return () => {
mounted = false;
clearInterval(interval);
};
}, []);
return isOnline;
}