"use client"; import { useMemo, useReducer, useSyncExternalStore, type FormEvent } from "react"; import { IconBell, IconRefresh } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Checkbox } from "@kandev/ui/checkbox"; import { HoverCard, HoverCardContent, HoverCardTrigger } from "@kandev/ui/hover-card"; import { Input } from "@kandev/ui/input"; import { Separator } from "@kandev/ui/separator"; import { Textarea } from "@kandev/ui/textarea"; import { SettingsPageTemplate } from "@/components/settings/settings-page-template"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip"; import { DEFAULT_NOTIFICATION_EVENTS, EVENT_LABELS } from "@/lib/notifications/events"; import type { NotificationProvider } from "@/lib/types/http"; import { useNotificationsState, useSaveRequest, useNotificationsActions, useIsDirty, type NotificationsState, type AppriseFormMode, } from "@/components/settings/notifications-settings-actions"; type DesktopNotificationsSectionProps = { notificationPermission: NotificationPermission | "unsupported"; onRequestPermission: () => void; onRefreshPermission: () => void; onTestNotification: () => void; }; function DesktopNotificationsSection({ notificationPermission, onRequestPermission, onRefreshPermission, onTestNotification, }: DesktopNotificationsSectionProps) { return (
Desktop Notifications

Notify this device when an agent needs your input.

Refresh permission status If you do not see notifications, check your OS settings and allow this browser.
{notificationPermission === "denied" && (

Notifications are blocked in your browser. Enable them in site settings, then click Refresh.

)} {notificationPermission === "unsupported" && (

This browser does not support desktop notifications.

)}
); } type NotificationEventsTableProps = { tableProviders: NotificationProvider[]; tableEvents: string[]; onToggleEvent: (provider: NotificationProvider, eventType: string) => void; onTestProvider: (providerId: string) => Promise; }; function NotificationEventsTable({ tableProviders, tableEvents, onToggleEvent, onTestProvider, }: NotificationEventsTableProps) { if (tableProviders.length === 0) { return

No providers configured yet.

; } return (
{tableProviders.map((provider) => ( ))} {tableEvents.map((eventType) => { const meta = EVENT_LABELS[eventType] ?? { title: eventType, description: "Notify when this event occurs.", }; return ( {tableProviders.map((provider) => ( ))} ); })}
Notification type
{provider.name} {provider.type !== "local" && ( Send test notification )}
{meta.title}
{meta.description}
onToggleEvent(provider, eventType)} />
); } function AppriseProviderCardActions({ provider, onOpenForm, onDeleteProvider, onTestProvider, }: { provider: NotificationProvider; onOpenForm: (mode: AppriseFormMode, provider: NotificationProvider) => void; onDeleteProvider: (providerId: string) => void; onTestProvider: (providerId: string) => Promise; }) { return (
Send test notification
); } function AppriseProviderList({ providers, appriseFormMode, activeAppriseId, appriseName, appriseUrls, onNameChange, onUrlsChange, onAppriseNameEdit, onAppriseEdit, onOpenForm, onCloseForm, onDeleteProvider, onTestProvider, onTextareaInput, }: { providers: NotificationProvider[]; appriseFormMode: AppriseFormMode; activeAppriseId: string | null; appriseName: string; appriseUrls: string; onNameChange: (value: string) => void; onUrlsChange: (value: string) => void; onAppriseNameEdit: (providerId: string, value: string) => void; onAppriseEdit: (providerId: string, value: string) => void; onOpenForm: (mode: AppriseFormMode, provider?: NotificationProvider) => void; onCloseForm: () => void; onDeleteProvider: (providerId: string) => void; onTestProvider: (providerId: string) => Promise; onTextareaInput: (event: FormEvent) => void; }) { return ( <> {providers.map((provider) => { const isEditing = appriseFormMode === "edit" && activeAppriseId === provider.id; return (
{isEditing ? ( { onNameChange(value); onAppriseNameEdit(provider.id, value); }} onUrlsChange={(value) => { onUrlsChange(value); onAppriseEdit(provider.id, value); }} onSubmit={onCloseForm} onCancel={onCloseForm} onInput={onTextareaInput} /> ) : (
{provider.name}
Apprise
)}
); })} ); } function useNotificationPermission() { const mounted = useSyncExternalStore( () => () => undefined, () => true, () => false, ); const [, bumpPermission] = useReducer((value) => value + 1, 0); let notificationPermission: NotificationPermission | "unsupported"; if (!mounted) notificationPermission = "default"; else if (typeof Notification === "undefined") notificationPermission = "unsupported"; else notificationPermission = Notification.permission; return { notificationPermission, bumpPermission }; } type ExternalProvidersSectionProps = { appriseAvailable: boolean; appriseProviders: NotificationProvider[]; appriseFormMode: AppriseFormMode; activeAppriseId: string | null; appriseName: string; appriseUrls: string; showAppriseForm: boolean; setAppriseName: (v: string) => void; setAppriseUrls: (v: string) => void; onAppriseNameEdit: (id: string, v: string) => void; onAppriseEdit: (id: string, v: string) => void; onOpenForm: (mode: AppriseFormMode, provider?: NotificationProvider) => void; onCloseForm: () => void; onDeleteProvider: (id: string) => void; onTestProvider: (id: string) => Promise; onTextareaInput: (e: FormEvent) => void; onCreateAppriseProvider: () => Promise; }; function ExternalProvidersSection({ appriseAvailable, appriseProviders, appriseFormMode, activeAppriseId, appriseName, appriseUrls, showAppriseForm, setAppriseName, setAppriseUrls, onAppriseNameEdit, onAppriseEdit, onOpenForm, onCloseForm, onDeleteProvider, onTestProvider, onTextareaInput, onCreateAppriseProvider, }: ExternalProvidersSectionProps) { return (
External Providers

Configure external providers for remote notifications.

{!appriseAvailable && (

Apprise is not installed yet. You can add it later to enable remote notifications.{" "} View installation instructions .

)} {appriseProviders.length === 0 && (

No Apprise providers configured yet.

)} {appriseAvailable && (
{showAppriseForm && appriseFormMode === "create" && ( { await onCreateAppriseProvider(); onCloseForm(); }} onCancel={onCloseForm} onInput={onTextareaInput} /> )}
)}
); } function useTableData(state: NotificationsState) { const { providers, notificationEvents } = state; const tableProviders = useMemo( () => [...providers].sort((a, b) => { if (a.type === b.type) return a.name.localeCompare(b.name); if (a.type === "local") return -1; if (b.type === "local") return 1; return a.type.localeCompare(b.type); }), [providers], ); const tableEvents = useMemo(() => { if (notificationEvents.length > 0) return notificationEvents; const eventSet = new Set(); for (const provider of providers) { for (const event of provider.events ?? []) eventSet.add(event); } return eventSet.size ? Array.from(eventSet) : DEFAULT_NOTIFICATION_EVENTS; }, [notificationEvents, providers]); return { tableProviders, tableEvents }; } export function NotificationsSettings() { const state = useNotificationsState(); const { notificationPermission, bumpPermission } = useNotificationPermission(); const saveRequest = useSaveRequest(state); const actions = useNotificationsActions(state, bumpPermission); const isDirty = useIsDirty(state); const { tableProviders, tableEvents } = useTableData(state); const { providers, appriseAvailable, appriseName, setAppriseName, appriseUrls, setAppriseUrls, showAppriseForm, appriseFormMode, activeAppriseId, } = state; const appriseProviders = providers.filter((provider) => provider.type === "apprise"); const handleSave = async () => { try { await saveRequest.run(); } catch (error) { console.error("[NotificationsSettings] Failed to save notifications", error); } }; return (
Notification Events

Select which providers should receive each notification type.

{tableProviders.length > 0 && ( )}
); } type AppriseProviderFormProps = { mode: AppriseFormMode; name: string; urls: string; onNameChange: (value: string) => void; onUrlsChange: (value: string) => void; onSubmit: () => void | Promise; onCancel: () => void; onInput: (event: FormEvent) => void; }; function AppriseProviderForm({ mode, name, urls, onNameChange, onUrlsChange, onSubmit, onCancel, onInput, }: AppriseProviderFormProps) { return (
Apprise Provider
onNameChange(event.target.value)} placeholder="Provider name" />