import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { Bell, MessageSquare, Send, Trash2, RefreshCw, AlertCircle, CheckCircle, Settings, TestTube, Activity, Clock, TrendingUp, User, } from "lucide-react"; import { apiClient } from "../lib/api"; import NotificationsSkeleton from "./NotificationsSkeleton"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "./ui/card"; import { Alert, AlertDescription, AlertTitle } from "./ui/alert"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { Label } from "./ui/label"; import { Badge } from "./ui/badge"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "./ui/select"; import type { NotificationSettings, NotificationService, SyncOperationsResponse, } 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 { data: syncOperations, isLoading: syncOperationsLoading, error: syncOperationsError, refetch: refetchSyncOperations, } = useQuery({ queryKey: ["syncOperations"], queryFn: () => apiClient.getSyncOperations(10, 0), // Get latest 10 operations }); 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 || syncOperationsLoading) { return ; } if (settingsError || servicesError || syncOperationsError) { return ( Failed to load system data

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 (
{/* Sync Operations Section */} Sync Operations Recent synchronization activities {!syncOperations || syncOperations.operations.length === 0 ? (

No sync operations yet

Sync operations will appear here once you start syncing your accounts.

) : (
{syncOperations.operations.slice(0, 5).map((operation) => { const startedAt = new Date(operation.started_at); const isRunning = !operation.completed_at; const duration = operation.duration_seconds ? `${Math.round(operation.duration_seconds)}s` : ""; return (
{isRunning ? ( ) : operation.success ? ( ) : ( )}

{isRunning ? "Sync Running" : operation.success ? "Sync Completed" : "Sync Failed"}

{operation.trigger_type}
{startedAt.toLocaleDateString()}{" "} {startedAt.toLocaleTimeString()} {duration && Duration: {duration}}
{operation.accounts_processed} accounts
{operation.transactions_added} new transactions
{operation.errors.length > 0 && (
{operation.errors.length} errors
)}
); })}
)}
{/* Test Notification Section */} Test Notifications
setTestMessage(e.target.value)} 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.

)}
); }