@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
103 lines • 4.65 kB
JavaScript
import { useEffect, useMemo } from "react";
import { useDispatch, useSelector } from "react-redux";
import { setProjectContext, setLimit, setNotificationTemplates, selectAppNotifications, selectUnreadCount, selectAppNotificationsLoading, selectAppNotificationsHasMore, selectAppNotificationsPage, selectCurrentProjectId, selectNotificationTemplates, } from "../../store/slices/appNotificationsSlice";
import { useAppNotificationsActions } from "./useAppNotificationsActions";
import useProject from "../projects/useProject";
import { useUser } from "../user";
/**
* Redux-powered hook that provides the exact same interface as useAppNotificationsData()
* This is a drop-in replacement for the Context-based hook
*/
function useAppNotifications(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.limit, limit = _c === void 0 ? 10 : _c, notificationTemplates = _b.notificationTemplates;
var dispatch = useDispatch();
// Get external context
var projectId = useProject().projectId;
var user = useUser().user;
// Get Redux state
var appNotifications = useSelector(function (state) {
return selectAppNotifications(state);
});
var unreadAppNotificationsCount = useSelector(function (state) {
return selectUnreadCount(state);
});
var loading = useSelector(function (state) {
return selectAppNotificationsLoading(state);
});
var hasMore = useSelector(function (state) {
return selectAppNotificationsHasMore(state);
});
var currentPage = useSelector(function (state) {
return selectAppNotificationsPage(state);
});
var currentProjectId = useSelector(function (state) {
return selectCurrentProjectId(state);
});
// Get actions
var _d = useAppNotificationsActions(), loadMore = _d.loadMore, markNotificationAsRead = _d.markNotificationAsRead, markAllNotificationsAsRead = _d.markAllNotificationsAsRead, resetAppNotifications = _d.resetAppNotifications, fetchMoreNotifications = _d.fetchMoreNotifications, updateUnreadCount = _d.updateUnreadCount;
// Update Redux state when props change
useEffect(function () {
if (projectId && projectId !== currentProjectId) {
dispatch(setProjectContext(projectId));
}
}, [dispatch, projectId, currentProjectId]);
useEffect(function () {
dispatch(setLimit(limit));
}, [dispatch, limit]);
// Prevent infinite re-renders by comparing current vs new templates
var currentTemplates = useSelector(function (state) {
return selectNotificationTemplates(state);
});
var templatesChanged = useMemo(function () {
// If no templates provided, skip comparison
if (!notificationTemplates)
return false;
// Deep comparison using JSON stringify
return (JSON.stringify(currentTemplates) !== JSON.stringify(notificationTemplates));
}, [currentTemplates, notificationTemplates]);
useEffect(function () {
if (notificationTemplates && templatesChanged) {
dispatch(setNotificationTemplates(notificationTemplates));
}
}, [dispatch, notificationTemplates, templatesChanged]);
// Fetch unread count on mount and when dependencies change
useEffect(function () {
if (projectId && user) {
updateUnreadCount();
}
}, [updateUnreadCount, projectId, user]);
// Reset and fetch initial notifications when dependencies change
useEffect(function () {
if (projectId && user) {
resetAppNotifications();
}
}, [resetAppNotifications, projectId, user]);
// Handle page changes (load more notifications)
useEffect(function () {
if (currentPage > 1 && projectId && user) {
fetchMoreNotifications(currentPage);
}
}, [currentPage, fetchMoreNotifications, projectId, user]);
// Return the same interface as the original hook
return useMemo(function () { return ({
appNotifications: appNotifications,
unreadAppNotificationsCount: unreadAppNotificationsCount,
loading: loading,
hasMore: hasMore,
loadMore: loadMore,
markNotificationAsRead: markNotificationAsRead,
markAllNotificationsAsRead: markAllNotificationsAsRead,
resetAppNotifications: resetAppNotifications,
}); }, [
appNotifications,
unreadAppNotificationsCount,
loading,
hasMore,
loadMore,
markNotificationAsRead,
markAllNotificationsAsRead,
resetAppNotifications,
]);
}
export default useAppNotifications;
//# sourceMappingURL=useAppNotifications.js.map