import { useEffect, useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Switch } from "~/components/ui/switch"; import { Button } from "~/components/ui/button"; import { Badge } from "~/components/ui/badge"; import { Separator } from "~/components/ui/separator"; import { Bell, Smartphone, Trash2, Send, AlertTriangle, Loader, Save, } from "lucide-react"; import InstallApp, { isInstalled } from "./install-app"; import { isNotificationEnabled, registerNotification, STORAGE_KEY, unregisterNotification, } from "~/lib/notification"; import { testNotification, unsubscribe, updateSubscription, useNotifications, type Event, } from "~/lib/api"; import { toast } from "sonner"; const EVENTS = [ { id: "SYSTEM", label: "Système", description: "Soyez informé des mises à jour et des nouveautés de Khollisé.", }, { id: "COLLE_ADDED", label: "Colle ajoutée", description: "Recevez une notification lorsqu'une nouvelle colle est ajoutée par votre CDT.", }, { id: "COLLE_REMOVED", label: "Colle supprimée", description: "Recevez une notification lorsqu'une colle est supprimée.", }, { id: "COLLE_UPDATED", label: "Colle modifiée", description: "Soyez averti lorsqu'un colleur modifie le contenu d'une colle.", }, { id: "GRADE_ADDED", label: "Note ajoutée", description: "Recevez une notification lorsqu'une colle est notée.", }, { id: "GRADE_UPDATED", label: "Note modifiée", description: "Soyez averti lorsqu'une note est modifiée.", }, { id: "ROOM_UPDATED", label: "Salle modifiée", description: "Soyez averti lorsqu'une colle change de salle.", }, ]; export default function NotificationSettings() { const [pushEnabled, setPushEnabled] = useState(isNotificationEnabled()); const [isRegistering, setIsRegistering] = useState(false); async function togglePushEnabled() { setIsRegistering(true); if (pushEnabled) { // Unregister notifications try { await unregisterNotification(); setPushEnabled(false); } catch (error) { console.error("Failed to unregister notifications:", error); } finally { setIsRegistering(false); } } else { // Register notifications try { const result = await registerNotification(); if ("error" in result && result.error) { console.error( "Failed to unregister previous notifications:", result.error ); } else { setPushEnabled(true); } } catch (error) { console.error("Failed to unregister previous notifications:", error); } finally { setIsRegistering(false); } } await refetch(); } const [events, setEvents] = useState([]); // TODO: Loader and error handling const { notifications, isError, isLoading, refetch } = useNotifications(); const toggleEvent = (eventId: string) => { setEvents( events.map((event) => event.id === eventId ? { ...event, enabled: !event.enabled } : event ) ); }; const hasEvent = (eventId: string) => { return events.some((event) => event.id === eventId && event.enabled); }; const currentSubscriptionId = localStorage.getItem(STORAGE_KEY); const currentSubscription = notifications.find( (sub) => sub.id.toString() === currentSubscriptionId ); useEffect(() => { setEvents(currentSubscription?.events || []); // Remove pushEnabled if no current subscription (i.e., user unsubscribed) if (!isLoading && !isError && pushEnabled && !currentSubscription) { setPushEnabled(false); localStorage.removeItem(STORAGE_KEY); } }, [currentSubscription]); async function handleDelete(subscriptionId: string) { try { await unsubscribe(subscriptionId); if (subscriptionId === currentSubscriptionId) { localStorage.removeItem(STORAGE_KEY); } await refetch(); toast.success("L'appareil a été désabonné des notifications."); } catch (error) { console.error("Failed to unsubscribe:", error); toast.error("Échec de la désinscription de l'appareil."); } } return (

Paramètres de notifications

Gérez vos préférences de notification et vos appareils abonnés.

{/* Enable Push Notifications Section */}
Activer les notifications
Autorisez cette application à vous envoyer des notifications push.
{isInstalled() ? ( pushEnabled ? (

Notifications

Recevez des notifications même lorsque l’application est fermée.

) : ( ) ) : ( )}
{/* Select Notification Events Section */} {currentSubscription && ( Configurer vos notifications Choisissez les événements qui déclencheront une notification.
{EVENTS.map((event, index) => (

{event.label}

{event.description}

toggleEvent(event.id)} aria-label={`Toggle ${event.label} notifications`} className="flex-shrink-0" />
{index < events.length - 1 && ( )}
))}
)} {/* Manage Subscriptions Section */} Manage Subscriptions View and manage devices that can receive notifications
{notifications.map((subscription, index) => (

{subscription.device}

{subscription.enabled ? ( Active ) : ( Revoked )}
{index < notifications.length - 1 && ( )}
))} {notifications.length === 0 && (

Aucun appareil n'est actuellement abonné aux notifications.

Vous pouvez activer les notifications pour recevoir des alertes sur vos appareils.

)}
); }