@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
168 lines • 7.19 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAppNotificationsActions = useAppNotificationsActions;
const react_1 = require("react");
const hooks_1 = require("../../store/hooks");
const appNotificationsSlice_1 = require("../../store/slices/appNotificationsSlice");
const appNotificationsApi_1 = require("../../store/api/appNotificationsApi");
const handleError_1 = require("../../utils/handleError");
const useProject_1 = __importDefault(require("../projects/useProject"));
const user_1 = require("../user");
/**
* Hook that provides Redux-powered actions for app notifications
* Integrates RTK Query with Redux slice actions
*
* Note: Templates are applied at display time in useAppNotifications, not here.
* This ensures templates are always fresh and avoids race conditions.
*/
function useAppNotificationsActions() {
const dispatch = (0, hooks_1.useReplykeDispatch)();
// Get current state for actions
const projectIdFromSlice = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectCurrentProjectId);
const page = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotificationsPage);
const limit = (0, hooks_1.useReplykeSelector)(appNotificationsSlice_1.selectAppNotificationsLimit);
// Get project and user context (fallback to current hooks)
const { projectId: projectIdFromHook } = (0, useProject_1.default)();
const { user } = (0, user_1.useUser)();
// Use project ID from slice if available, otherwise from hook
const projectId = projectIdFromSlice || projectIdFromHook;
// RTK Query hooks
const [triggerFetchNotifications] = (0, appNotificationsApi_1.useLazyFetchAppNotificationsQuery)();
const [markNotificationAsReadMutation] = (0, appNotificationsApi_1.useMarkNotificationAsReadMutation)();
const [markAllNotificationsAsReadMutation] = (0, appNotificationsApi_1.useMarkAllNotificationsAsReadMutation)();
const [triggerCountUnread] = (0, appNotificationsApi_1.useLazyCountUnreadNotificationsQuery)();
// Load more action
const loadMore = (0, react_1.useCallback)(() => {
dispatch((0, appNotificationsSlice_1.loadMore)());
}, [dispatch]);
// Mark notification as read action
const markNotificationAsRead = (0, react_1.useCallback)(async ({ notificationId }) => {
if (!projectId || !user) {
throw new Error("No project ID or authenticated user available");
}
try {
// Optimistic update
dispatch((0, appNotificationsSlice_1.markAsReadLocally)(notificationId));
// Make API call
await markNotificationAsReadMutation({
projectId,
notificationId,
}).unwrap();
}
catch (error) {
(0, handleError_1.handleError)(error, "Failed to mark notification as read:");
throw error;
}
}, [dispatch, projectId, user, markNotificationAsReadMutation]);
// Reset notifications action
const resetAppNotifications = (0, react_1.useCallback)(async () => {
if (!projectId || !user) {
throw new Error("No project ID or authenticated user available");
}
try {
dispatch((0, appNotificationsSlice_1.setLoading)(true));
dispatch((0, appNotificationsSlice_1.resetNotifications)());
// Fetch first page
const response = await triggerFetchNotifications({
projectId,
page: 1,
limit,
}).unwrap();
if (response) {
const { data: notifications, pagination } = response;
// Store raw notifications - templates applied at display time
dispatch((0, appNotificationsSlice_1.addNotifications)({
notifications,
hasMore: pagination.hasMore,
isFirstPage: true,
}));
}
}
catch (error) {
(0, handleError_1.handleError)(error, "Failed to refresh notifications:");
throw error;
}
}, [
dispatch,
projectId,
user,
triggerFetchNotifications,
limit,
]);
// Fetch more notifications (internal action triggered by page changes)
const fetchMoreNotifications = (0, react_1.useCallback)(async ({ pageToFetch }) => {
if (!projectId || !user)
return;
try {
dispatch((0, appNotificationsSlice_1.setLoading)(true));
const response = await triggerFetchNotifications({
projectId,
page: pageToFetch,
limit,
}).unwrap();
if (response) {
const { data: notifications, pagination } = response;
// Store raw notifications - templates applied at display time
dispatch((0, appNotificationsSlice_1.addNotifications)({
notifications,
hasMore: pagination.hasMore,
}));
}
}
catch (error) {
(0, handleError_1.handleError)(error, "Loading more app notifications failed:");
}
finally {
dispatch((0, appNotificationsSlice_1.setLoading)(false));
}
}, [
dispatch,
projectId,
user,
triggerFetchNotifications,
limit,
]);
// Update unread count
const updateUnreadCount = (0, react_1.useCallback)(async () => {
if (!projectId || !user)
return;
try {
const count = await triggerCountUnread({ projectId }).unwrap();
if (typeof count === "number") {
dispatch((0, appNotificationsSlice_1.setUnreadCount)(count));
}
}
catch (error) {
(0, handleError_1.handleError)(error, "Failed to fetch unread count:");
}
}, [dispatch, projectId, user, triggerCountUnread]);
// Mark all notifications as read action
const markAllNotificationsAsRead = (0, react_1.useCallback)(async () => {
if (!projectId || !user) {
throw new Error("No project ID or authenticated user available");
}
try {
// Optimistic update
dispatch((0, appNotificationsSlice_1.markAllAsReadLocally)());
// Make API call
await markAllNotificationsAsReadMutation({ projectId }).unwrap();
}
catch (error) {
(0, handleError_1.handleError)(error, "Failed to mark all notifications as read:");
throw error;
}
}, [dispatch, projectId, user, markAllNotificationsAsReadMutation]);
return {
loadMore,
markNotificationAsRead,
markAllNotificationsAsRead,
resetAppNotifications,
fetchMoreNotifications, // Internal action
updateUnreadCount, // Internal action
};
}
exports.default = useAppNotificationsActions;
//# sourceMappingURL=useAppNotificationsActions.js.map