* chore: configure WDYR for react profiling * perf: reduced post content re-rendering * perf: post content view and it child optimization * perf: add memoization in post editor * perf: add memoization in postCommnetsView * perf: improved endorsed comment view rendering * perf: improved re-rendering in reply component * fix: uncomment questionType commentsView * fix: removed console errors in postContent area * perf: reduced postType and postId dependancy * perf: improved re-rendering in discussionHome * perf: improved re-rendering of postsList and its child components * perf: improved re-rendering of legacyTopic and learner sidebar * fix: postFilterBar filter was not updating * fix: resolve duplicate comment posts issue * fix: memory leaking issue in comments view * fix: duplicate topic posts in inContext sidebar * perf: add lazy loading * chore: remove WDYR configuration * fix: alert banner padding * chore: update package-lock file * fix: bind tour API call with buttons
105 lines
3.2 KiB
JavaScript
105 lines
3.2 KiB
JavaScript
import {
|
|
useCallback, useContext, useEffect, useMemo,
|
|
} from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
|
|
|
|
import { EndorsementStatus } from '../../../data/constants';
|
|
import { useDispatchWithState } from '../../../data/hooks';
|
|
import { DiscussionContext } from '../../common/context';
|
|
import { selectThread } from '../../posts/data/selectors';
|
|
import { markThreadAsRead } from '../../posts/data/thunks';
|
|
import { filterPosts } from '../../utils';
|
|
import {
|
|
selectCommentSortOrder, selectThreadComments, selectThreadCurrentPage, selectThreadHasMorePages,
|
|
} from './selectors';
|
|
import { fetchThreadComments } from './thunks';
|
|
|
|
const trackLoadMoreEvent = (postId, params) => (
|
|
sendTrackEvent(
|
|
'edx.forum.responses.loadMore',
|
|
{
|
|
postId,
|
|
params,
|
|
},
|
|
)
|
|
);
|
|
|
|
export function usePost(postId) {
|
|
const dispatch = useDispatch();
|
|
const thread = useSelector(selectThread(postId));
|
|
|
|
useEffect(() => {
|
|
if (thread && !thread.read) {
|
|
dispatch(markThreadAsRead(postId));
|
|
}
|
|
}, [postId]);
|
|
|
|
return thread || {};
|
|
}
|
|
|
|
export function usePostComments(endorsed = null) {
|
|
const { enableInContextSidebar, postId } = useContext(DiscussionContext);
|
|
const [isLoading, dispatch] = useDispatchWithState();
|
|
const comments = useSelector(selectThreadComments(postId, endorsed));
|
|
const reverseOrder = useSelector(selectCommentSortOrder);
|
|
const hasMorePages = useSelector(selectThreadHasMorePages(postId, endorsed));
|
|
const currentPage = useSelector(selectThreadCurrentPage(postId, endorsed));
|
|
|
|
const endorsedCommentsIds = useMemo(() => (
|
|
[...filterPosts(comments, 'endorsed')].map(comment => comment.id)
|
|
), [comments]);
|
|
|
|
const unEndorsedCommentsIds = useMemo(() => (
|
|
[...filterPosts(comments, 'unendorsed')].map(comment => comment.id)
|
|
), [comments]);
|
|
|
|
const handleLoadMoreResponses = useCallback(async () => {
|
|
const params = {
|
|
endorsed,
|
|
page: currentPage + 1,
|
|
reverseOrder,
|
|
};
|
|
await dispatch(fetchThreadComments(postId, params));
|
|
trackLoadMoreEvent(postId, params);
|
|
}, [currentPage, endorsed, postId, reverseOrder]);
|
|
|
|
useEffect(() => {
|
|
const abortController = new AbortController();
|
|
|
|
dispatch(fetchThreadComments(postId, {
|
|
endorsed,
|
|
page: 1,
|
|
reverseOrder,
|
|
enableInContextSidebar,
|
|
signal: abortController.signal,
|
|
}));
|
|
|
|
return () => {
|
|
abortController.abort();
|
|
};
|
|
}, [postId, endorsed, reverseOrder, enableInContextSidebar]);
|
|
|
|
return {
|
|
endorsedCommentsIds,
|
|
unEndorsedCommentsIds,
|
|
hasMorePages,
|
|
isLoading,
|
|
handleLoadMoreResponses,
|
|
};
|
|
}
|
|
|
|
export function useCommentsCount(postId) {
|
|
const discussions = useSelector(selectThreadComments(postId, EndorsementStatus.DISCUSSION));
|
|
const endorsedQuestions = useSelector(selectThreadComments(postId, EndorsementStatus.ENDORSED));
|
|
const unendorsedQuestions = useSelector(selectThreadComments(postId, EndorsementStatus.UNENDORSED));
|
|
|
|
const commentsLength = useMemo(() => (
|
|
[...discussions, ...endorsedQuestions, ...unendorsedQuestions].length
|
|
), [discussions, endorsedQuestions, unendorsedQuestions]);
|
|
|
|
return commentsLength;
|
|
}
|