Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Comments navigation issues. #2638

Merged
merged 16 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/components/ModalProvider.jsx
@@ -1,6 +1,5 @@
import React, { useState, createContext } from "react";
import React, { useState, createContext, useCallback, useRef } from "react";
import IdentityErrorBoundary from "src/components/IdentityErrorBoundary";
import isEmpty from "lodash/fp/isEmpty";

export const modalContext = createContext({ component: () => null, props: {} });

Expand All @@ -12,37 +11,46 @@ const initialState = {
};

const ModalProvider = ({ children }) => {
const [modalStack] = useState([]);
const [currentModal, setModal] = useState(initialState);
const handleOpenModal = function handleOpenModal(
const modalStack = useRef([]);
const modalToDisplay = useRef(initialState);
// Modals are refs, and refs doesn't trigger component re-render. This
// will force the state update, hence a modal update.
const [, setIsDisplaying] = useState(false);

const handleOpenModal = useCallback(function (
modal,
props = {},
{ overlay } = {}
) {
const newModal = { component: modal, props: { show: true, ...props } };
if (isEmpty(modalStack) || overlay) {
modalStack.push(newModal);
setModal(newModal);
} else if (!currentModal || !currentModal.props.show) {
setModal(newModal);
if (!modalStack.current || overlay) {
modalStack.current && modalStack.current.push(newModal);
modalToDisplay.current = newModal;
setIsDisplaying(true);
} else if (modalToDisplay.current && !modalToDisplay.current.props.show) {
modalToDisplay.current = newModal;
setIsDisplaying(true);
}
};
},
[]);

const handleCloseModal = function handleCloseModal() {
modalStack.pop();
const previousModal = modalStack.pop();
setModal(previousModal || initialState);
};
const handleCloseModal = useCallback(function () {
modalStack.current && modalStack.current.pop();
const previousModal = modalStack.current && modalStack.current.pop();
modalToDisplay.current = previousModal || initialState;
setIsDisplaying(false);
}, []);

const props = {
onClose: handleCloseModal,
...currentModal.props
...modalToDisplay.current.props
};

return (
<IdentityErrorBoundary>
<modalContext.Provider value={[handleOpenModal, handleCloseModal]}>
{children}
<currentModal.component {...props} />
<modalToDisplay.current.component {...props} />
</modalContext.Provider>
</IdentityErrorBoundary>
);
Expand Down
3 changes: 1 addition & 2 deletions src/components/Router/RouterProvider.jsx
Expand Up @@ -5,7 +5,6 @@ import React, {
useEffect,
useMemo
} from "react";
import isEqual from "lodash/isEqual";
import { withRouter } from "react-router-dom";

const routerCtx = createContext();
Expand All @@ -16,7 +15,7 @@ const RouterProvider = ({ location, children, ...rest }) => {
const [pastLocations, setPastLocations] = useState([]);

useEffect(() => {
if (!isEqual(pastLocations[0], location)) {
if (pastLocations[0]?.pathname !== location.pathname) {
victorgcramos marked this conversation as resolved.
Show resolved Hide resolved
setPastLocations([location].concat(pastLocations));
}
}, [location, pastLocations]);
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Comments/Comment/Comment.jsx
Expand Up @@ -88,7 +88,7 @@ const Comment = ({
</Link>
<DateTooltip timestamp={createdAt} placement="bottom">
{({ timeAgo }) => (
<Link className={styles.timeAgo} to={permalink}>
<Link className={styles.timeAgo} to={`${permalink}`}>
{timeAgo}
</Link>
)}
Expand Down
16 changes: 9 additions & 7 deletions src/containers/Comments/Comment/CommentWrapper.jsx
Expand Up @@ -11,13 +11,15 @@ import {
PROPOSAL_COMMENT_DOWNVOTE
} from "src/constants";

const ContextLink = React.memo(({ parentid, recordToken }) => (
<Link
className={styles.contextLink}
to={`/record/${recordToken}/comments/${parentid}`}>
see in context
</Link>
));
const ContextLink = React.memo(({ parentid, recordToken }) => {
return (
<Link
className={styles.contextLink}
to={`/record/${recordToken}/comments/${parentid}`}>
see in context
</Link>
);
});

const Replies = React.memo(({ children }) => (
<div className={styles.childrenContainer}>{children}</div>
Expand Down
17 changes: 3 additions & 14 deletions src/containers/Comments/Comments.jsx
Expand Up @@ -21,12 +21,7 @@ import { CommentContext } from "./hooks";
import CommentsListWrapper from "./CommentsList/CommentsListWrapper";
import CommentLoader from "./Comment/CommentLoader";
import Link from "src/components/Link";
import {
useQueryString,
useScrollTo,
useComments,
useLocalStorage
} from "src/hooks";
import { useComments, useLocalStorage } from "src/hooks";
import {
getSortOptionsForSelect,
createSelectOptionFromSortOption,
Expand All @@ -36,7 +31,6 @@ import {
} from "./helpers";
import { PROPOSAL_MAIN_THREAD_KEY } from "src/constants";
import { commentsReducer, initialState, actions } from "./commentsReducer";
import { getQueryStringValue } from "src/lib/queryString";
import { debounce } from "lodash";

const FlatModeButton = ({ isActive, onClick }) => (
Expand Down Expand Up @@ -367,14 +361,9 @@ const Comments = ({
const numOfComments = comments?.length;

const [state, dispatch] = useReducer(commentsReducer, initialState);
const hasComments = !!comments;
const hasScrollToQuery = !!getQueryStringValue("scrollToComments");
const shouldScrollToComments =
(hasScrollToQuery || isSingleThread) && hasComments;
useScrollTo("commentArea", shouldScrollToComments);

const [sortOption, setSortOption] = useQueryString(
"sort",
const [sortOption, setSortOption] = useLocalStorage(
`sortComments-${recordToken}`,
commentSortOptions.SORT_BY_TOP
);

Expand Down
11 changes: 10 additions & 1 deletion src/containers/Proposal/Detail/Detail.jsx
Expand Up @@ -10,6 +10,7 @@ import {
useIdentity,
useDocumentTitle,
useModalContext,
useScrollTo,
usePolicy
} from "src/hooks";
import Comments from "src/containers/Comments";
Expand Down Expand Up @@ -39,6 +40,7 @@ import WhatAreYourThoughts from "src/components/WhatAreYourThoughts";
import { PROPOSAL_UPDATE_HINT, PROPOSAL_STATE_UNVETTED } from "src/constants";
import { shortRecordToken } from "src/helpers";
import ModalLogin from "src/components/ModalLogin";
import { getQueryStringValue } from "src/lib/queryString";

const COMMENTS_LOGIN_MODAL_ID = "commentsLoginModal";

Expand All @@ -50,6 +52,8 @@ const SetPageTitle = ({ title }) => {
const ProposalDetail = ({ Main, match, history }) => {
const tokenFromUrl = shortRecordToken(get("params.token", match));
const threadParentCommentID = get("params.commentid", match);
const hasScrollToQuery = !!getQueryStringValue("scrollToComments");

const {
policyTicketVote: { summariespagesize: proposalPageSize }
} = usePolicy();
Expand All @@ -67,7 +71,8 @@ const ProposalDetail = ({ Main, match, history }) => {
commentsError,
commentsLoading,
onReloadProposalDetails,
billingStatusChangeUsername
billingStatusChangeUsername,
commentsFinishedLoading
} = useProposal(tokenFromUrl, proposalPageSize, threadParentCommentID);
const { userid } = currentUser || {};
const isSingleThread = !!threadParentID;
Expand All @@ -88,6 +93,10 @@ const ProposalDetail = ({ Main, match, history }) => {
const paywallMissing = paywallEnabled && !isPaid;
const [, identityError] = useIdentity();

const shouldScrollToComments =
(hasScrollToQuery || isSingleThread) && proposal && commentsFinishedLoading;
useScrollTo("commentArea", shouldScrollToComments);

const onRedirectToSignup = useCallback(
() => history.push("/user/signup"),
[history]
Expand Down
4 changes: 3 additions & 1 deletion src/containers/Proposal/Detail/hooks.js
Expand Up @@ -270,7 +270,8 @@ export function useProposal(token, proposalPageSize, threadParentID) {
hasAuthorUpdates,
singleThreadRootId,
error: commentsError,
loading: commentsLoading
loading: commentsLoading,
finishedCommentsFetch
} = useComments(proposalToken, proposalState, null, threadParentID);

return {
Expand All @@ -293,6 +294,7 @@ export function useProposal(token, proposalPageSize, threadParentID) {
commentsError,
currentUser,
commentsLoading,
commentsFinishedLoading: finishedCommentsFetch,
onReloadProposalDetails
};
}
8 changes: 6 additions & 2 deletions src/hooks/api/useComments.js
Expand Up @@ -85,14 +85,17 @@ export default function useComments(
// comments are not public on cms. User needs to be logged in
const isProposal = recordType === constants.RECORD_TYPE_PROPOSAL;
const needsToFetchComments = isProposal
? !!recordToken && !comments
: !!recordToken && !comments && userLoggedIn;
? !!recordToken &&
victorgcramos marked this conversation as resolved.
Show resolved Hide resolved
!allCommentsBySection &&
(proposalState === PROPOSAL_STATE_VETTED || currentUser?.isadmin)
: !!recordToken && !allCommentsBySection && userLoggedIn;

const needsToFetchCommentsLikes =
!!recordToken &&
!commentsLikes &&
enableCommentVote &&
userLoggedIn &&
!loadingLikes &&
proposalState === PROPOSAL_STATE_VETTED;

useEffect(
Expand Down Expand Up @@ -184,6 +187,7 @@ export default function useComments(
getCommentVotes,
commentSectionIds,
hasAuthorUpdates,
finishedCommentsFetch: !!allCommentsBySection,
victorgcramos marked this conversation as resolved.
Show resolved Hide resolved
latestAuthorUpdateId: hasAuthorUpdates && commentSectionIds[0],
singleThreadRootId
};
Expand Down