import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { Bell, MessageSquare, Send, Trash2, RefreshCw, AlertCircle, CheckCircle, Settings, TestTube, } from "lucide-react"; import { apiClient } from "../lib/api"; import LoadingSpinner from "./LoadingSpinner"; import type { NotificationSettings, NotificationService } from "../types/api"; export default function Notifications() { const [testService, setTestService] = useState(""); const [testMessage, setTestMessage] = useState( "Test notification from Leggen", ); const queryClient = useQueryClient(); const { data: settings, isLoading: settingsLoading, error: settingsError, refetch: refetchSettings, } = useQuery({ queryKey: ["notificationSettings"], queryFn: apiClient.getNotificationSettings, }); const { data: services, isLoading: servicesLoading, error: servicesError, refetch: refetchServices, } = useQuery({ queryKey: ["notificationServices"], queryFn: apiClient.getNotificationServices, }); const testMutation = useMutation({ mutationFn: apiClient.testNotification, onSuccess: () => { // Could show a success toast here console.log("Test notification sent successfully"); }, onError: (error) => { console.error("Failed to send test notification:", error); }, }); const deleteServiceMutation = useMutation({ mutationFn: apiClient.deleteNotificationService, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["notificationSettings"] }); queryClient.invalidateQueries({ queryKey: ["notificationServices"] }); }, }); if (settingsLoading || servicesLoading) { return (
); } if (settingsError || servicesError) { return (

Failed to load notifications

Unable to connect to the Leggen API. Please check your configuration and ensure the API server is running.

); } const handleTestNotification = () => { if (!testService) return; testMutation.mutate({ service: testService.toLowerCase(), message: testMessage, }); }; const handleDeleteService = (serviceName: string) => { if ( confirm( `Are you sure you want to delete the ${serviceName} notification service?`, ) ) { deleteServiceMutation.mutate(serviceName.toLowerCase()); } }; return (
{/* Test Notification Section */}

Test Notifications

setTestMessage(e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="Test message..." />
{/* Notification Services */}

Notification Services

Manage your notification services

{!services || services.length === 0 ? (

No notification services configured

Configure notification services in your backend to receive alerts.

) : (
{services.map((service) => (
{service.name.toLowerCase().includes("discord") ? ( ) : service.name.toLowerCase().includes("telegram") ? ( ) : ( )}

{service.name}

{service.enabled ? ( ) : ( )} {service.enabled ? "Enabled" : "Disabled"} {service.configured ? "Configured" : "Not Configured"}
))}
)}
{/* Notification Settings */}

Notification Settings

{settings && (

Filters

{settings.filters.case_insensitive.length > 0 ? settings.filters.case_insensitive.join(", ") : "None"}

{settings.filters.case_sensitive && settings.filters.case_sensitive.length > 0 ? settings.filters.case_sensitive.join(", ") : "None"}

Configure notification settings through your backend API to customize filters and service configurations.

)}
); }