diff --git a/packages/components/src/CommentItem/CommentItem.tsx b/packages/components/src/CommentItem/CommentItem.tsx index 40a6df3a4d..fb38bd6c24 100644 --- a/packages/components/src/CommentItem/CommentItem.tsx +++ b/packages/components/src/CommentItem/CommentItem.tsx @@ -61,11 +61,10 @@ export const CommentItem = (props: CommentItemProps) => { } return ( - + -const comments = [ - { - _created: '2022-06-15T09:41:09.571Z', - _creatorId: 'TestCreatorID', - _id: 'testID', - creatorName: 'TestName', - isUserVerified: false, - text: 'Test text one', - isEditable: true, - }, - { - _created: '2022-06-15T09:41:09.571Z', - _creatorId: 'TestCreatorID2', - _id: 'testID2', - creatorName: 'TestName2', - isUserVerified: false, - text: 'Test text two', - isEditable: true, - }, -] +const createComments = (numberOfComments = 2, commentOverloads = {}) => + [...Array(numberOfComments).keys()].slice(0).map(() => ({ + _created: faker.date.past().toString(), + creatorCountry: faker.address.countryCode().toLowerCase(), + _creatorId: faker.internet.userName(), + _id: faker.database.mongodbObjectId(), + creatorName: faker.internet.userName(), + isUserVerified: faker.datatype.boolean(), + text: faker.lorem.text(), + isEditable: faker.datatype.boolean(), + ...commentOverloads, + })) export const Default: ComponentStory = () => ( Promise.resolve()} + handleEditRequest={() => Promise.resolve()} + handleEdit={() => Promise.resolve()} + /> +) + +export const Expandable: ComponentStory = () => ( + Promise.resolve()} + handleEditRequest={() => Promise.resolve()} + handleEdit={() => Promise.resolve()} + /> +) + +const highlightedCommentList = createComments(20, { isEditable: false }) + +export const Highlighted: ComponentStory = () => ( + Promise.resolve()} handleEditRequest={() => Promise.resolve()} diff --git a/packages/components/src/CommentList/CommentList.tsx b/packages/components/src/CommentList/CommentList.tsx index d7dbdda523..d25a8f56d1 100644 --- a/packages/components/src/CommentList/CommentList.tsx +++ b/packages/components/src/CommentList/CommentList.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useEffect, useState } from 'react' import ReactGA from 'react-ga4' import { Box } from 'theme-ui' import { Button, CommentItem } from '../' @@ -10,16 +10,39 @@ export const CommentList: React.FC<{ handleEdit: (_id: string, comment: string) => Promise handleEditRequest: () => Promise handleDelete: (_id: string) => Promise + highlightedCommentId?: string articleTitle?: string }> = ({ articleTitle, comments, handleEditRequest, handleDelete, + highlightedCommentId, handleEdit, }) => { const [moreComments, setMoreComments] = useState(1) const shownComments = moreComments * MAX_COMMENTS + const scrollIntoRelevantComment = (commentId: string) => { + setTimeout(() => { + // the delay is needed, otherwise the scroll is not happening in Firefox + document + .getElementById(`comment:${commentId}`) + ?.scrollIntoView({ behavior: 'smooth', block: 'start' }) + }, 0) + } + + useEffect(() => { + if (!highlightedCommentId) return + + const i = comments.findIndex((comment) => + highlightedCommentId.includes(comment._id), + ) + if (i >= 0) { + setMoreComments(Math.floor(i / MAX_COMMENTS) + 1) + scrollIntoRelevantComment(highlightedCommentId) + } + }, [highlightedCommentId]) + return ( {comments && - comments - .slice(0, shownComments) - .map((comment: Comment) => ( + comments.slice(0, shownComments).map((comment: Comment) => ( + - ))} + + ))} {comments && comments.length > shownComments && (