analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
1 lines • 8.09 kB
Source Map (JSON)
{"version":3,"sources":["../src/hooks/useNotifications.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { createUseNotificationStore } from './useNotificationStore';\nimport {\n NotificationEntityType,\n NotificationApiClient,\n} from '../types/notifications';\nimport type { NotificationGroup, Notification } from '../types/notifications';\nimport { formatTimeAgo } from '../store/notificationStore';\n\n/**\n * Create a comprehensive notifications hook with formatting and navigation\n * This hook provides all notification functionality in a single interface\n *\n * @param apiClient - API client instance for notifications\n * @returns Hook with notification state, actions, and utilities\n */\nexport const createUseNotifications = (apiClient: NotificationApiClient) => {\n const useNotificationStore = createUseNotificationStore(apiClient);\n\n return () => {\n const {\n notifications,\n unreadCount,\n loading,\n error,\n hasMore,\n currentPage,\n fetchNotifications,\n fetchUnreadCount,\n markAsRead,\n markAllAsRead,\n deleteNotification,\n clearNotifications,\n resetError,\n getGroupedNotifications,\n } = useNotificationStore();\n\n /**\n * Handle navigation based on notification entity type and ID\n * Uses globalThis.location.href for cross-framework compatibility\n *\n * @param entityType - Type of entity (ACTIVITY, RECOMMENDEDCLASS)\n * @param entityId - ID of the entity\n * @param onAfterNavigate - Optional callback to execute after navigation\n */\n const handleNavigate = useCallback(\n (\n entityType?: string,\n entityId?: string,\n onAfterNavigate?: () => void\n ) => {\n if (entityType) {\n switch (entityType.toUpperCase()) {\n case NotificationEntityType.ACTIVITY:\n if (entityId) {\n globalThis.location.href = `/conteudo/atividades/${entityId}`;\n }\n break;\n case NotificationEntityType.RECOMMENDEDCLASS:\n // Navigate to list since front-web detail page requires navigation state\n globalThis.location.href = `/plano/consultar`;\n break;\n default:\n break;\n }\n // Execute callback after navigation, if provided\n onAfterNavigate?.();\n }\n },\n []\n );\n\n /**\n * Get action label based on entity type\n *\n * @param entityType - Type of entity (ACTIVITY, RECOMMENDEDCLASS)\n * @returns Action label or undefined\n */\n const getActionLabel = useCallback(\n (entityType?: string): string | undefined => {\n if (!entityType) return 'Ver mais';\n\n switch (entityType.toUpperCase()) {\n case NotificationEntityType.ACTIVITY:\n return 'Ver atividade';\n case NotificationEntityType.RECOMMENDEDCLASS:\n return 'Ver meta';\n default:\n return undefined;\n }\n },\n []\n );\n\n /**\n * Mark notification as read and handle navigation\n *\n * @param id - Notification ID\n * @param entityType - Optional entity type for navigation\n * @param entityId - Optional entity ID for navigation\n * @param onAfterNavigate - Optional callback to execute after navigation\n */\n const markAsReadAndNavigate = useCallback(\n async (\n id: string,\n entityType?: string,\n entityId?: string,\n onAfterNavigate?: () => void\n ) => {\n await markAsRead(id);\n if (entityType && entityId) {\n handleNavigate(entityType, entityId, onAfterNavigate);\n }\n },\n [markAsRead, handleNavigate]\n );\n\n /**\n * Refresh notifications and reset error state\n */\n const refreshNotifications = useCallback(async () => {\n resetError();\n await fetchNotifications();\n }, [resetError, fetchNotifications]);\n\n /**\n * Format a single notification with time and entity data\n *\n * @param notification - The notification to format\n * @returns Formatted notification\n */\n const formatNotification = useCallback(\n (notification: Notification) => ({\n ...notification,\n time: formatTimeAgo(notification.createdAt),\n entityType: notification.entityType || undefined,\n entityId: notification.entityId || undefined,\n }),\n []\n );\n\n /**\n * Format grouped notifications with time and entity data\n * This provides all the formatting logic needed by components\n *\n * @returns Formatted notifications ready for display\n */\n const getFormattedGroupedNotifications = useCallback(() => {\n const groups = getGroupedNotifications();\n return groups.map((group: NotificationGroup) => ({\n ...group,\n notifications: group.notifications.map(formatNotification),\n }));\n }, [getGroupedNotifications, formatNotification]);\n\n return {\n // State\n notifications,\n unreadCount,\n loading,\n error,\n hasMore,\n currentPage,\n\n // Actions\n fetchNotifications,\n fetchUnreadCount,\n markAsRead,\n markAllAsRead,\n deleteNotification,\n clearNotifications,\n resetError,\n markAsReadAndNavigate,\n refreshNotifications,\n\n // Navigation\n handleNavigate,\n\n // Helpers\n getActionLabel,\n getGroupedNotifications,\n getFormattedGroupedNotifications,\n };\n };\n};\n\n/**\n * Create a pre-configured notifications hook\n * This is a convenience function that returns a hook ready to use\n *\n * @param apiClient - API client instance for notifications\n * @returns Pre-configured useNotifications hook\n *\n * @example\n * // In your app setup\n * import { createNotificationsHook } from 'analytica-frontend-lib';\n * import api from './services/api';\n *\n * export const useNotifications = createNotificationsHook(api);\n *\n * // Then use directly in components\n * const { unreadCount, fetchNotifications } = useNotifications();\n */\nexport const createNotificationsHook = (apiClient: NotificationApiClient) => {\n return createUseNotifications(apiClient);\n};\n"],"mappings":";;;;;;;;AAAA,SAAS,mBAAmB;AAgBrB,IAAM,yBAAyB,CAAC,cAAqC;AAC1E,QAAM,uBAAuB,2BAA2B,SAAS;AAEjE,SAAO,MAAM;AACX,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,IAAI,qBAAqB;AAUzB,UAAM,iBAAiB;AAAA,MACrB,CACE,YACA,UACA,oBACG;AACH,YAAI,YAAY;AACd,kBAAQ,WAAW,YAAY,GAAG;AAAA,YAChC;AACE,kBAAI,UAAU;AACZ,2BAAW,SAAS,OAAO,wBAAwB,QAAQ;AAAA,cAC7D;AACA;AAAA,YACF;AAEE,yBAAW,SAAS,OAAO;AAC3B;AAAA,YACF;AACE;AAAA,UACJ;AAEA,4BAAkB;AAAA,QACpB;AAAA,MACF;AAAA,MACA,CAAC;AAAA,IACH;AAQA,UAAM,iBAAiB;AAAA,MACrB,CAAC,eAA4C;AAC3C,YAAI,CAAC,WAAY,QAAO;AAExB,gBAAQ,WAAW,YAAY,GAAG;AAAA,UAChC;AACE,mBAAO;AAAA,UACT;AACE,mBAAO;AAAA,UACT;AACE,mBAAO;AAAA,QACX;AAAA,MACF;AAAA,MACA,CAAC;AAAA,IACH;AAUA,UAAM,wBAAwB;AAAA,MAC5B,OACE,IACA,YACA,UACA,oBACG;AACH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