import React, { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { useToast } from '@/components/ui/Toast'; import { useNotificationTypes, useUpdateNotificationPreference, } from '@/api/hooks'; interface NotificationTypeItem { type_key: string; plugin_name: string; category: string; label: string; description: string | null; is_enabled_by_default: boolean; is_enabled: boolean; } export function SettingsNotificationsPage() { const { t } = useTranslation(); const toast = useToast(); const { data, isLoading, error } = useNotificationTypes(); const updateMutation = useUpdateNotificationPreference(); const items: NotificationTypeItem[] = data?.items ?? []; // Group by plugin_name const grouped = useMemo(() => { const map = new Map(); for (const item of items) { const key = item.plugin_name; if (!map.has(key)) map.set(key, []); map.get(key)!.push(item); } return Array.from(map.entries()); }, [items]); const handleToggle = (typeKey: string, isEnabled: boolean) => { updateMutation.mutate( { typeKey, isEnabled }, { onSuccess: () => { toast.success(t('notifications.saved')); }, onError: (err: any) => { toast.error(err.message || t('common.error')); }, } ); }; if (isLoading) { return (

{t('common.loading')}

); } if (error) { return (

{t('common.error')}

); } return (

{t('notifications.title')}

{t('notifications.description')}

{grouped.length === 0 && (

{t('common.noResults')}

)}
{grouped.map(([pluginName, pluginItems]) => (

{pluginName}

{pluginItems.map((item) => (

{item.label}

{item.description && (

{item.description}

)}
handleToggle(item.type_key, checked)} disabled={updateMutation.isPending} ariaLabel={item.label} />
))}
))}
); } function ToggleSwitch({ checked, onChange, disabled, ariaLabel, }: { checked: boolean; onChange: (checked: boolean) => void; disabled?: boolean; ariaLabel: string; }) { return ( ); }