UNPKG

@replyke/core

Version:

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

129 lines 4.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); const useFetchManyComments_1 = __importDefault(require("./useFetchManyComments")); const handleError_1 = require("../../utils/handleError"); function useFetchManyCommentsWrapper(props) { const { entityId, userId, parentId, sourceId, limit = 10, defaultSortBy = "new", include, } = props; const fetchManyComments = (0, useFetchManyComments_1.default)(); const loading = (0, react_1.useRef)(true); const [loadingState, setLoadingState] = (0, react_1.useState)(true); const hasMore = (0, react_1.useRef)(true); const [hasMoreState, setHasMoreState] = (0, react_1.useState)(true); const [sortBy, setSortBy] = (0, react_1.useState)(defaultSortBy); const [page, setPage] = (0, react_1.useState)(1); const [comments, setComments] = (0, react_1.useState)([]); const resetComments = (0, react_1.useCallback)(async () => { if (!userId && !entityId && !parentId) { return; } try { loading.current = true; setLoadingState(true); hasMore.current = true; setHasMoreState(true); setPage(1); const response = await fetchManyComments({ entityId, userId, parentId, sourceId, page: 1, sortBy, limit, include, }); if (response) { const { data: newComments, pagination } = response; setComments(newComments); hasMore.current = pagination.hasMore; setHasMoreState(pagination.hasMore); } } catch (err) { (0, handleError_1.handleError)(err, "Failed to reset comments:"); } finally { loading.current = false; setLoadingState(false); } }, [ fetchManyComments, limit, sortBy, entityId, userId, parentId, sourceId, include, ]); const loadMore = () => { if (loading.current || !hasMore.current) return; setPage((prevPage) => { return prevPage + 1; }); }; (0, react_1.useEffect)(() => { resetComments(); }, [resetComments]); // useEffect to get a new batch of comments (0, react_1.useEffect)(() => { const loadMoreComments = async () => { loading.current = true; setLoadingState(true); try { const response = await fetchManyComments({ entityId, userId, parentId, sourceId, page, sortBy, limit, include, }); if (response) { const { data: newComments, pagination } = response; setComments((prevComments) => [...prevComments, ...newComments]); hasMore.current = pagination.hasMore; setHasMoreState(pagination.hasMore); } } catch (err) { (0, handleError_1.handleError)(err, "Loading more comments failed:"); } finally { loading.current = false; setLoadingState(false); } }; // We only load more if the page changed if (page > 1 && hasMore.current && !loading.current) { loadMoreComments(); } }, [ page, fetchManyComments, entityId, userId, parentId, sourceId, sortBy, limit, include, ]); return { comments, loading: loadingState, hasMore: hasMoreState, sortBy, setSortBy, loadMore, }; } exports.default = useFetchManyCommentsWrapper; //# sourceMappingURL=useFetchManyCommentsWrapper.js.map