@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
83 lines • 4.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const hooks_1 = require("../../store/hooks");
const appNotificationsSlice_1 = require("../../store/slices/appNotificationsSlice");
const useAppNotificationsActions_1 = require("./useAppNotificationsActions");
const useProject_1 = __importDefault(require("../projects/useProject"));
const user_1 = require("../user");
const addNotificationsMessages_1 = __importDefault(require("../../helpers/addNotificationsMessages"));
/**
* Redux-powered hook that provides the exact same interface as useAppNotificationsData()
* This is a drop-in replacement for the Context-based hook
*/
function useAppNotifications({ limit = 10, notificationTemplates, } = {}) {
const dispatch = (0, hooks_1.useReplykeDispatch)();
// Get external context
const { projectId } = (0, useProject_1.default)();
const { user } = (0, user_1.useUser)();
// Get Redux state
const appNotifications = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotifications);
const unreadAppNotificationsCount = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectUnreadCount);
const loading = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotificationsLoading);
const hasMore = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotificationsHasMore);
const currentPage = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotificationsPage);
const currentProjectId = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectCurrentProjectId);
// Get actions (templates are applied at display time, not fetch time)
const { loadMore, markNotificationAsRead, markAllNotificationsAsRead, resetAppNotifications, fetchMoreNotifications, updateUnreadCount, } = (0, useAppNotificationsActions_1.useAppNotificationsActions)();
// Update Redux state when props change
(0, react_1.useEffect)(() => {
if (projectId && projectId !== currentProjectId) {
dispatch((0, appNotificationsSlice_1.setProjectContext)(projectId));
}
}, [dispatch, projectId, currentProjectId]);
(0, react_1.useEffect)(() => {
dispatch((0, appNotificationsSlice_1.setLimit)(limit));
}, [dispatch, limit]);
// Fetch unread count on mount and when dependencies change
(0, react_1.useEffect)(() => {
if (projectId && user) {
updateUnreadCount();
}
}, [updateUnreadCount, projectId, user]);
// Reset and fetch initial notifications when dependencies change
(0, react_1.useEffect)(() => {
if (projectId && user) {
resetAppNotifications();
}
}, [resetAppNotifications, projectId, user]);
// Handle page changes (load more notifications)
(0, react_1.useEffect)(() => {
if (currentPage > 1 && projectId && user) {
fetchMoreNotifications({ pageToFetch: currentPage });
}
}, [currentPage, fetchMoreNotifications, projectId, user]);
// Apply templates at display time (not at fetch time)
// This ensures templates are always applied fresh from props
const templatedNotifications = (0, react_1.useMemo)(() => (0, addNotificationsMessages_1.default)(appNotifications, notificationTemplates), [appNotifications, notificationTemplates]);
// Return the same interface as the original hook
return (0, react_1.useMemo)(() => ({
appNotifications: templatedNotifications,
unreadAppNotificationsCount,
loading,
hasMore,
loadMore,
markNotificationAsRead,
markAllNotificationsAsRead,
resetAppNotifications,
}), [
templatedNotifications,
unreadAppNotificationsCount,
loading,
hasMore,
loadMore,
markNotificationAsRead,
markAllNotificationsAsRead,
resetAppNotifications,
]);
}
exports.default = useAppNotifications;
//# sourceMappingURL=useAppNotifications.js.map