// frontend/src/api/hooks/useNotification.ts /** * Notification Operations Hooks */ import { useState, useCallback } from 'react'; import { notificationService } from '../services'; import type { NotificationCreate, NotificationResponse, NotificationTemplate, NotificationStats, BulkNotificationRequest, } from '../types'; export const useNotification = () => { const [notifications, setNotifications] = useState([]); const [templates, setTemplates] = useState([]); const [stats, setStats] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const sendNotification = useCallback(async ( tenantId: string, notification: NotificationCreate ): Promise => { try { setIsLoading(true); setError(null); const sentNotification = await notificationService.sendNotification(tenantId, notification); setNotifications(prev => [sentNotification, ...prev]); return sentNotification; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to send notification'; setError(message); throw error; } finally { setIsLoading(false); } }, []); const sendBulkNotifications = useCallback(async ( tenantId: string, request: BulkNotificationRequest ): Promise => { try { setIsLoading(true); setError(null); await notificationService.sendBulkNotifications(tenantId, request); } catch (error) { const message = error instanceof Error ? error.message : 'Failed to send bulk notifications'; setError(message); throw error; } finally { setIsLoading(false); } }, []); const getNotifications = useCallback(async (tenantId: string): Promise => { try { setIsLoading(true); setError(null); const response = await notificationService.getNotifications(tenantId); setNotifications(response.data); return response.data; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to get notifications'; setError(message); throw error; } finally { setIsLoading(false); } }, []); const getTemplates = useCallback(async (tenantId: string): Promise => { try { setIsLoading(true); setError(null); const response = await notificationService.getTemplates(tenantId); setTemplates(response.data); return response.data; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to get templates'; setError(message); throw error; } finally { setIsLoading(false); } }, []); const createTemplate = useCallback(async ( tenantId: string, template: Omit ): Promise => { try { setIsLoading(true); setError(null); const newTemplate = await notificationService.createTemplate(tenantId, template); setTemplates(prev => [newTemplate, ...prev]); return newTemplate; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to create template'; setError(message); throw error; } finally { setIsLoading(false); } }, []); const getNotificationStats = useCallback(async (tenantId: string): Promise => { try { setIsLoading(true); setError(null); const notificationStats = await notificationService.getNotificationStats(tenantId); setStats(notificationStats); return notificationStats; } catch (error) { const message = error instanceof Error ? error.message : 'Failed to get notification stats'; setError(message); throw error; } finally { setIsLoading(false); } }, []); return { notifications, templates, stats, isLoading, error, sendNotification, sendBulkNotifications, getNotifications, getTemplates, createTemplate, getNotificationStats, clearError: () => setError(null), }; };