@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
239 lines • 12.4 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var useFetchAppNotifications_1 = __importDefault(require("./useFetchAppNotifications"));
var useCountUnreadNotifications_1 = __importDefault(require("./useCountUnreadNotifications"));
var useMarkNotificationAsRead_1 = __importDefault(require("./useMarkNotificationAsRead"));
var handleError_1 = require("../../utils/handleError");
var addNotificationsMessages_1 = __importDefault(require("../../helpers/addNotificationsMessages"));
function useAppNotificationsData(_a) {
var _this = this;
var _b = _a.limit, limit = _b === void 0 ? 10 : _b, notificationTemplates = _a.notificationTemplates;
var _c = (0, react_1.useState)([]), appNotifications = _c[0], setAppNotifications = _c[1];
var _d = (0, react_1.useState)(0), unreadAppNotificationsCount = _d[0], setUnreadAppNotificationsCount = _d[1];
var _e = (0, react_1.useState)(1), page = _e[0], setPage = _e[1];
// Consolidate loading state management
var _f = (0, react_1.useState)(true), loading = _f[0], setLoading = _f[1];
var _g = (0, react_1.useState)(true), hasMore = _g[0], setHasMore = _g[1];
// Track initialization to prevent multiple initial loads
var initializedRef = (0, react_1.useRef)(false);
var requestInFlightRef = (0, react_1.useRef)(false);
var fetchAppNotifications = (0, useFetchAppNotifications_1.default)();
var countUnreadNotifications = (0, useCountUnreadNotifications_1.default)();
var markNotificationAsRead = (0, useMarkNotificationAsRead_1.default)();
var resetAppNotifications = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var newAppNotifications, completeNotifications, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// Prevent concurrent reset calls
if (requestInFlightRef.current)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, 4, 5]);
requestInFlightRef.current = true;
setLoading(true);
setHasMore(true);
setPage(1);
setAppNotifications([]);
return [4 /*yield*/, fetchAppNotifications({
page: 1,
limit: limit,
})];
case 2:
newAppNotifications = _a.sent();
if (newAppNotifications) {
completeNotifications = (0, addNotificationsMessages_1.default)(newAppNotifications, notificationTemplates);
setAppNotifications(completeNotifications);
setHasMore(newAppNotifications.length >= limit);
}
initializedRef.current = true;
return [3 /*break*/, 5];
case 3:
err_1 = _a.sent();
(0, handleError_1.handleError)(err_1, "Failed to refresh notifications:");
return [3 /*break*/, 5];
case 4:
setLoading(false);
requestInFlightRef.current = false;
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
}); }, [fetchAppNotifications, limit, notificationTemplates]);
var loadMore = function () {
if (loading || !hasMore || requestInFlightRef.current)
return;
setPage(function (prevPage) { return prevPage + 1; });
};
var handleMarkNotificationAsRead = function (notificationId) { return __awaiter(_this, void 0, void 0, function () {
var err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, markNotificationAsRead(notificationId)];
case 1:
_a.sent();
setUnreadAppNotificationsCount(function (prevCount) { return Math.max(prevCount - 1, 0); });
setAppNotifications(function (prevNotifs) {
return prevNotifs.map(function (notif) {
return notif.id === notificationId ? __assign(__assign({}, notif), { isRead: true }) : notif;
});
});
return [3 /*break*/, 3];
case 2:
err_2 = _a.sent();
(0, handleError_1.handleError)(err_2, "Failed to mark notification as read:");
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
// Initialize notifications on mount only
(0, react_1.useEffect)(function () {
if (!initializedRef.current) {
resetAppNotifications();
}
}, []);
// Check how many unread notifications we have
(0, react_1.useEffect)(function () {
(function () { return __awaiter(_this, void 0, void 0, function () {
var newCount;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, countUnreadNotifications()];
case 1:
newCount = _a.sent();
if (typeof newCount === "number")
setUnreadAppNotificationsCount(newCount);
return [2 /*return*/];
}
});
}); })();
}, [countUnreadNotifications]);
// useEffect to get a new batch of app notifications
(0, react_1.useEffect)(function () {
var loadMoreNotifications = function () { return __awaiter(_this, void 0, void 0, function () {
var newAppNotifications, completeNotifications_1, err_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// Prevent concurrent requests and ensure we're initialized
if (requestInFlightRef.current || !initializedRef.current)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, 4, 5]);
requestInFlightRef.current = true;
setLoading(true);
return [4 /*yield*/, fetchAppNotifications({
page: page,
limit: limit,
})];
case 2:
newAppNotifications = _a.sent();
if (newAppNotifications) {
completeNotifications_1 = (0, addNotificationsMessages_1.default)(newAppNotifications, notificationTemplates);
setAppNotifications(function (prevEntities) {
return __spreadArray(__spreadArray([], prevEntities, true), completeNotifications_1, true);
});
setHasMore(newAppNotifications.length >= limit);
}
return [3 /*break*/, 5];
case 3:
err_3 = _a.sent();
(0, handleError_1.handleError)(err_3, "Loading more app notifications failed:");
return [3 /*break*/, 5];
case 4:
setLoading(false);
requestInFlightRef.current = false;
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
}); };
// Only load more pages after initial load (page > 1) and when we have more data
if (page > 1 && hasMore && !loading && initializedRef.current) {
loadMoreNotifications();
}
}, [page, hasMore, loading, fetchAppNotifications, limit, notificationTemplates]);
// Cleanup on unmount
(0, react_1.useEffect)(function () {
return function () {
requestInFlightRef.current = false;
initializedRef.current = false;
};
}, []);
return {
appNotifications: appNotifications,
unreadAppNotificationsCount: unreadAppNotificationsCount,
loading: loading,
hasMore: hasMore,
loadMore: loadMore,
markNotificationAsRead: handleMarkNotificationAsRead,
resetAppNotifications: resetAppNotifications,
};
}
exports.default = useAppNotificationsData;
//# sourceMappingURL=useAppNotificationsData.js.map