@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
342 lines • 16.9 kB
JavaScript
"use strict";
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 __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 useRequestConnection_1 = __importDefault(require("./useRequestConnection"));
var useAcceptConnection_1 = __importDefault(require("./useAcceptConnection"));
var useDeclineConnection_1 = __importDefault(require("./useDeclineConnection"));
var useRemoveConnection_1 = __importDefault(require("./useRemoveConnection"));
var useRemoveConnectionByUserId_1 = __importDefault(require("./useRemoveConnectionByUserId"));
var useFetchConnectionStatus_1 = __importDefault(require("./useFetchConnectionStatus"));
var useUser_1 = __importDefault(require("../../user/useUser"));
function useConnectionManager(_a) {
var _this = this;
var userId = _a.userId;
var user = (0, useUser_1.default)().user;
var _b = (0, react_1.useState)("none"), connectionStatus = _b[0], setConnectionStatus = _b[1];
var _c = (0, react_1.useState)({
connectionId: null,
}), connectionData = _c[0], setConnectionData = _c[1];
var _d = (0, react_1.useState)(true), isLoading = _d[0], setIsLoading = _d[1];
var requestConnection = (0, useRequestConnection_1.default)();
var acceptConnection = (0, useAcceptConnection_1.default)();
var declineConnection = (0, useDeclineConnection_1.default)();
var removeConnection = (0, useRemoveConnection_1.default)();
var removeConnectionByUserId = (0, useRemoveConnectionByUserId_1.default)();
var getConnectionStatus = (0, useFetchConnectionStatus_1.default)();
var loadConnectionStatus = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var statusResponse, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!userId || (user === null || user === void 0 ? void 0 : user.id) === userId) {
setConnectionStatus("none");
setConnectionData({ connectionId: null });
setIsLoading(false);
return [2 /*return*/];
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, 4, 5]);
setIsLoading(true);
return [4 /*yield*/, getConnectionStatus({ userId: userId })];
case 2:
statusResponse = _a.sent();
switch (statusResponse.status) {
case "none":
setConnectionStatus("none");
setConnectionData({ connectionId: null });
break;
case "pending":
setConnectionStatus(statusResponse.type === "sent" ? "pending-sent" : "pending-received");
setConnectionData({
connectionId: statusResponse.connectionId,
createdAt: statusResponse.createdAt,
type: statusResponse.type,
});
break;
case "connected":
setConnectionStatus("connected");
setConnectionData({
connectionId: statusResponse.connectionId,
connectedAt: statusResponse.connectedAt,
requestedAt: statusResponse.requestedAt,
});
break;
case "declined":
setConnectionStatus(statusResponse.type === "sent" ? "declined-sent" : "declined-received");
setConnectionData({
connectionId: statusResponse.connectionId,
respondedAt: statusResponse.respondedAt,
type: statusResponse.type,
});
break;
default:
setConnectionStatus("none");
setConnectionData({ connectionId: null });
}
return [3 /*break*/, 5];
case 3:
error_1 = _a.sent();
console.error("Failed to fetch connection status:", error_1);
setConnectionStatus("none");
setConnectionData({ connectionId: null });
return [3 /*break*/, 5];
case 4:
setIsLoading(false);
return [7 /*endfinally*/];
case 5: return [2 /*return*/];
}
});
}); }, [userId, user === null || user === void 0 ? void 0 : user.id, getConnectionStatus]);
(0, react_1.useEffect)(function () {
loadConnectionStatus();
}, [loadConnectionStatus]);
var sendConnectionRequest = (0, react_1.useCallback)(function (message) { return __awaiter(_this, void 0, void 0, function () {
var result, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if ((connectionStatus !== "none" && connectionStatus !== "declined-received") ||
isLoading ||
(user === null || user === void 0 ? void 0 : user.id) === userId)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, requestConnection({ userId: userId, message: message })];
case 2:
result = _a.sent();
setConnectionStatus("pending-sent");
setConnectionData({
connectionId: result.id,
type: "sent",
createdAt: result.createdAt,
});
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
console.error("Failed to send connection request:", error_2);
throw error_2;
case 4: return [2 /*return*/];
}
});
}); }, [connectionStatus, isLoading, userId, user === null || user === void 0 ? void 0 : user.id, requestConnection]);
var acceptConnectionRequest = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (connectionStatus !== "pending-received" ||
!connectionData.connectionId ||
isLoading)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 4, , 5]);
return [4 /*yield*/, acceptConnection({
connectionId: connectionData.connectionId,
})];
case 2:
_a.sent();
setConnectionStatus("connected");
// Reload to get the updated connection data with dates
return [4 /*yield*/, loadConnectionStatus()];
case 3:
// Reload to get the updated connection data with dates
_a.sent();
return [3 /*break*/, 5];
case 4:
error_3 = _a.sent();
console.error("Failed to accept connection request:", error_3);
throw error_3;
case 5: return [2 /*return*/];
}
});
}); }, [
connectionStatus,
connectionData.connectionId,
isLoading,
acceptConnection,
loadConnectionStatus,
]);
var declineConnectionRequest = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var result, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (connectionStatus !== "pending-received" ||
!connectionData.connectionId ||
isLoading)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, declineConnection({ connectionId: connectionData.connectionId })];
case 2:
result = _a.sent();
// When you decline someone, from your perspective it's "declined-received"
// (you received and declined their request)
setConnectionStatus("declined-received");
setConnectionData({
connectionId: connectionData.connectionId,
respondedAt: result.respondedAt,
type: "received",
});
return [3 /*break*/, 4];
case 3:
error_4 = _a.sent();
console.error("Failed to decline connection request:", error_4);
throw error_4;
case 4: return [2 /*return*/];
}
});
}); }, [
connectionStatus,
connectionData.connectionId,
isLoading,
declineConnection,
]);
var withdrawConnectionRequest = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var error_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (connectionStatus !== "pending-sent" ||
!connectionData.connectionId ||
isLoading)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, removeConnection({ connectionId: connectionData.connectionId })];
case 2:
_a.sent();
setConnectionStatus("none");
setConnectionData({ connectionId: null });
return [3 /*break*/, 4];
case 3:
error_5 = _a.sent();
console.error("Failed to withdraw connection request:", error_5);
throw error_5;
case 4: return [2 /*return*/];
}
});
}); }, [
connectionStatus,
connectionData.connectionId,
isLoading,
removeConnection,
]);
var disconnectUser = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var error_6;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (connectionStatus !== "connected" ||
!connectionData.connectionId ||
isLoading)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, removeConnection({ connectionId: connectionData.connectionId })];
case 2:
_a.sent();
setConnectionStatus("none");
setConnectionData({ connectionId: null });
return [3 /*break*/, 4];
case 3:
error_6 = _a.sent();
console.error("Failed to disconnect user:", error_6);
throw error_6;
case 4: return [2 /*return*/];
}
});
}); }, [
connectionStatus,
connectionData.connectionId,
isLoading,
removeConnection,
]);
var removeConnectionSmart = (0, react_1.useCallback)(function () { return __awaiter(_this, void 0, void 0, function () {
var error_7;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (connectionStatus === "none" ||
connectionStatus === "declined-sent" ||
isLoading ||
(user === null || user === void 0 ? void 0 : user.id) === userId)
return [2 /*return*/];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, removeConnectionByUserId({ userId: userId })];
case 2:
_a.sent();
setConnectionStatus("none");
setConnectionData({ connectionId: null });
return [3 /*break*/, 4];
case 3:
error_7 = _a.sent();
console.error("Failed to remove connection:", error_7);
throw error_7;
case 4: return [2 /*return*/];
}
});
}); }, [connectionStatus, isLoading, userId, user === null || user === void 0 ? void 0 : user.id, removeConnectionByUserId]);
return {
connectionStatus: connectionStatus,
connectionId: connectionData.connectionId,
connectionData: connectionData,
isLoading: isLoading,
sendConnectionRequest: sendConnectionRequest,
acceptConnectionRequest: acceptConnectionRequest,
declineConnectionRequest: declineConnectionRequest,
withdrawConnectionRequest: withdrawConnectionRequest,
disconnectUser: disconnectUser,
removeConnectionSmart: removeConnectionSmart,
refreshConnectionStatus: loadConnectionStatus,
};
}
exports.default = useConnectionManager;
//# sourceMappingURL=useConnectionManager.js.map