|
1 | | -// ToastAlert.tsx |
2 | | -import React, { useState, useEffect } from 'react'; |
3 | | -import * as Toast from '@radix-ui/react-toast'; |
4 | | -import { cn } from "@/lib/utils"; // Utilitaire pour gérer les classes conditionnelles (ShadCN). |
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import { useToast } from '@/hooks/use-toast'; |
| 3 | +import { ToastAction } from '@/components/ui/toast'; |
5 | 4 |
|
6 | | -const ToastAlert: React.FC = () => { |
7 | | - const [isOnline, setIsOnline] = useState(navigator.onLine); |
8 | | - const [open, setOpen] = useState(!isOnline); |
| 5 | +const OfflineAlert = () => { |
| 6 | + const { toast } = useToast(); |
| 7 | + const [isOnline, setIsOnline] = useState<boolean>(navigator.onLine); |
| 8 | + const [hasNotified, setHasNotified] = useState<boolean>(false); |
9 | 9 |
|
10 | 10 | useEffect(() => { |
| 11 | + // Gestion de l'événement "en ligne" |
11 | 12 | const handleOnline = () => { |
12 | 13 | setIsOnline(true); |
13 | | - setOpen(false); |
| 14 | + setHasNotified(false); |
| 15 | + toast({ |
| 16 | + duration: 2000, |
| 17 | + variant: "default", |
| 18 | + title: "Back Online!", |
| 19 | + description: "You are reconnected to the internet.", |
| 20 | + }); |
14 | 21 | }; |
15 | 22 |
|
| 23 | + // Gestion de l'événement "hors ligne" |
16 | 24 | const handleOffline = () => { |
17 | 25 | setIsOnline(false); |
18 | | - setOpen(true); |
| 26 | + if (!hasNotified) { |
| 27 | + toast({ |
| 28 | + duration: 4000, |
| 29 | + variant: "destructive", |
| 30 | + title: "No Internet Connection", |
| 31 | + description: |
| 32 | + "You are currently offline. Check your connection to continue.", |
| 33 | + action: ( |
| 34 | + <ToastAction altText="Retry">Retry</ToastAction> |
| 35 | + ), |
| 36 | + }); |
| 37 | + setHasNotified(true); // Empêche l'affichage répété de l'alerte |
| 38 | + } |
19 | 39 | }; |
20 | 40 |
|
| 41 | + // Ajout des gestionnaires d'événements |
21 | 42 | window.addEventListener('online', handleOnline); |
22 | 43 | window.addEventListener('offline', handleOffline); |
23 | 44 |
|
| 45 | + // Nettoyage des gestionnaires d'événements |
24 | 46 | return () => { |
25 | 47 | window.removeEventListener('online', handleOnline); |
26 | 48 | window.removeEventListener('offline', handleOffline); |
27 | 49 | }; |
28 | | - }, []); |
| 50 | + }, [toast, hasNotified, isOnline]); |
29 | 51 |
|
30 | | - return ( |
31 | | - <Toast.Provider> |
32 | | - <Toast.Root |
33 | | - open={open} |
34 | | - onOpenChange={setOpen} |
35 | | - className={cn( |
36 | | - "fixed bottom-4 left-4 z-50 w-72 rounded-lg bg-red-500 p-4 text-white shadow-lg", |
37 | | - "data-[state=open]:animate-slide-in data-[state=closed]:animate-slide-out" |
38 | | - )} |
39 | | - > |
40 | | - <Toast.Title className="font-bold text-lg"> |
41 | | - Pas de connexion Internet |
42 | | - </Toast.Title> |
43 | | - <Toast.Description className="mt-2 text-sm"> |
44 | | - Vous êtes actuellement hors ligne. Vérifiez votre connexion pour continuer. |
45 | | - </Toast.Description> |
46 | | - </Toast.Root> |
47 | | - <Toast.Viewport className="fixed bottom-4 left-4 z-50" /> |
48 | | - </Toast.Provider> |
49 | | - ); |
| 52 | + return null; |
50 | 53 | }; |
51 | 54 |
|
52 | | -export default ToastAlert; |
| 55 | +export default OfflineAlert; |
0 commit comments