UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

234 lines 10.8 kB
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 }; } }; import { useEffect, useMemo, useCallback } from "react"; import { useDispatch, useSelector } from "react-redux"; import { setProjectContext, selectCurrentList, selectSubLists, selectListsLoading, selectListHistory, selectSubListsMap, selectCurrentProjectId, } from "../../store/slices/listsSlice"; import { useListsActions } from "./useListsActions"; import useProject from "../projects/useProject"; import { useUser } from "../user"; /** * Redux-powered hook that provides the exact same interface as useListsData() * This is a drop-in replacement for the Context-based hook */ function useLists(_) { var _this = this; if (_ === void 0) { _ = {}; } var dispatch = useDispatch(); // Get external context var projectId = useProject().projectId; var user = useUser().user; // Get Redux state var currentList = useSelector(function (state) { return selectCurrentList(state); }); var subLists = useSelector(function (state) { return selectSubLists(state); }); var loading = useSelector(function (state) { return selectListsLoading(state); }); var listHistory = useSelector(function (state) { return selectListHistory(state); }); var subListsMap = useSelector(function (state) { return selectSubListsMap(state); }); var currentProjectId = useSelector(function (state) { return selectCurrentProjectId(state); }); // Get actions var _a = useListsActions(), openList = _a.openList, goBack = _a.goBack, goToRoot = _a.goToRoot, fetchRootList = _a.fetchRootList, fetchSubLists = _a.fetchSubLists, createListAction = _a.createList, updateListAction = _a.updateList, deleteListAction = _a.deleteList, addToListAction = _a.addToList, removeFromListAction = _a.removeFromList; // Update Redux state when project changes useEffect(function () { if (projectId && projectId !== currentProjectId) { dispatch(setProjectContext(projectId)); } }, [dispatch, projectId, currentProjectId]); // Fetch root list when user and project are available useEffect(function () { if (!user || !projectId) return; fetchRootList(projectId); }, [fetchRootList, user, projectId]); // Fetch sub-lists when current list changes useEffect(function () { if (!user || !projectId || !currentList) return; // Check if sub-lists for this list are already fetched if (subListsMap[currentList.id] !== undefined) { return; // No need to fetch, we already have the mapping (even if empty) } fetchSubLists(projectId, currentList.id); }, [fetchSubLists, user, projectId, currentList, subListsMap]); // Entity membership checker var isEntityInList = useCallback(function (selectedEntityId) { if (!currentList) return false; return currentList.entityIds.some(function (entityId) { return entityId === selectedEntityId; }); }, [currentList]); // Wrapped CRUD operations that match the original interface var handleCreateList = useCallback(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) { var listName = _b.listName; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!listName) { console.error("No listName provided."); return [2 /*return*/]; } if (!currentList) { console.error("No current list."); return [2 /*return*/]; } if (!projectId) { console.error("No projectId available."); return [2 /*return*/]; } return [4 /*yield*/, createListAction(projectId, currentList.id, listName)]; case 1: _c.sent(); return [2 /*return*/]; } }); }); }, [createListAction, currentList, projectId]); var handleUpdateList = useCallback(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) { var listId = _b.listId, update = _b.update; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!projectId) { console.error("No projectId available."); return [2 /*return*/]; } return [4 /*yield*/, updateListAction(projectId, listId, update)]; case 1: _c.sent(); return [2 /*return*/]; } }); }); }, [updateListAction, projectId]); var handleDeleteList = useCallback(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) { var list = _b.list; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!projectId) { console.error("No projectId available."); return [2 /*return*/]; } return [4 /*yield*/, deleteListAction(projectId, list)]; case 1: _c.sent(); return [2 /*return*/]; } }); }); }, [deleteListAction, projectId]); var handleAddToList = useCallback(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) { var entityId = _b.entityId; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!entityId) { console.error("No entityId provided."); return [2 /*return*/]; } if (!currentList) { console.error("No current list."); return [2 /*return*/]; } if (!projectId) { console.error("No projectId available."); return [2 /*return*/]; } return [4 /*yield*/, addToListAction(projectId, currentList.id, entityId)]; case 1: _c.sent(); return [2 /*return*/]; } }); }); }, [addToListAction, currentList, projectId]); var handleRemoveFromList = useCallback(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) { var entityId = _b.entityId; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!currentList) { console.error("No current list."); return [2 /*return*/]; } if (!projectId) { console.error("No projectId available."); return [2 /*return*/]; } return [4 /*yield*/, removeFromListAction(projectId, currentList.id, entityId)]; case 1: _c.sent(); return [2 /*return*/]; } }); }); }, [removeFromListAction, currentList, projectId]); // Return the same interface as the original hook return useMemo(function () { return ({ currentList: currentList, subLists: subLists, loading: loading, openList: openList, goBack: goBack, goToRoot: goToRoot, isEntityInList: isEntityInList, createList: handleCreateList, updateList: handleUpdateList, deleteList: handleDeleteList, addToList: handleAddToList, removeFromList: handleRemoveFromList, }); }, [ currentList, subLists, loading, openList, goBack, goToRoot, isEntityInList, handleCreateList, handleUpdateList, handleDeleteList, handleAddToList, handleRemoveFromList, ]); } export default useLists; //# sourceMappingURL=useLists.js.map