analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
127 lines (125 loc) • 3.45 kB
JavaScript
import {
createUseNotificationStore
} from "./chunk-MEN33LGL.mjs";
import {
formatTimeAgo
} from "./chunk-6Y3UKXTF.mjs";
// src/hooks/useNotifications.ts
import { useCallback } from "react";
var createUseNotifications = (apiClient) => {
const useNotificationStore = createUseNotificationStore(apiClient);
return () => {
const {
notifications,
unreadCount,
loading,
error,
hasMore,
currentPage,
fetchNotifications,
fetchUnreadCount,
markAsRead,
markAllAsRead,
deleteNotification,
clearNotifications,
resetError,
getGroupedNotifications
} = useNotificationStore();
const handleNavigate = useCallback(
(entityType, entityId, onAfterNavigate) => {
if (entityType) {
switch (entityType.toUpperCase()) {
case "ACTIVITY" /* ACTIVITY */:
if (entityId) {
globalThis.location.href = `/conteudo/atividades/${entityId}`;
}
break;
case "RECOMMENDEDCLASS" /* RECOMMENDEDCLASS */:
globalThis.location.href = `/plano/consultar`;
break;
default:
break;
}
onAfterNavigate?.();
}
},
[]
);
const getActionLabel = useCallback(
(entityType) => {
if (!entityType) return "Ver mais";
switch (entityType.toUpperCase()) {
case "ACTIVITY" /* ACTIVITY */:
return "Ver atividade";
case "RECOMMENDEDCLASS" /* RECOMMENDEDCLASS */:
return "Ver meta";
default:
return void 0;
}
},
[]
);
const markAsReadAndNavigate = useCallback(
async (id, entityType, entityId, onAfterNavigate) => {
await markAsRead(id);
if (entityType && entityId) {
handleNavigate(entityType, entityId, onAfterNavigate);
}
},
[markAsRead, handleNavigate]
);
const refreshNotifications = useCallback(async () => {
resetError();
await fetchNotifications();
}, [resetError, fetchNotifications]);
const formatNotification = useCallback(
(notification) => ({
...notification,
time: formatTimeAgo(notification.createdAt),
entityType: notification.entityType || void 0,
entityId: notification.entityId || void 0
}),
[]
);
const getFormattedGroupedNotifications = useCallback(() => {
const groups = getGroupedNotifications();
return groups.map((group) => ({
...group,
notifications: group.notifications.map(formatNotification)
}));
}, [getGroupedNotifications, formatNotification]);
return {
// State
notifications,
unreadCount,
loading,
error,
hasMore,
currentPage,
// Actions
fetchNotifications,
fetchUnreadCount,
markAsRead,
markAllAsRead,
deleteNotification,
clearNotifications,
resetError,
markAsReadAndNavigate,
refreshNotifications,
// Navigation
handleNavigate,
// Helpers
getActionLabel,
getGroupedNotifications,
getFormattedGroupedNotifications
};
};
};
var createNotificationsHook = (apiClient) => {
return createUseNotifications(apiClient);
};
export {
createUseNotifications,
createNotificationsHook
};
//# sourceMappingURL=chunk-Z3UV7NNE.mjs.map