1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

highlight and scroll to new comments if authorized (#76)

* added new drone file

* commented-out unnecessary build stages

* commented-out unnecessary build stages

* added dynamic repo

* added dynamic repo

* added registry global env

* added registry global env

* added registry global env

* added template

* added template

* added template

* added template

* added branches to template

* added branches to template

* made build based on template

* made build based on template

* changed env file

* added .env.development file to repo

* fixed branch to develop

* added variables for develop and master

* added variables for develop and master

* added env variables to builder

* added env variables to builder

* added env variables to builder

* changed drone.yml

* added highlight for new comments

* fixed dependencies for useScrollToTop

* added smooth scrolling for comments

* fixed new comments highlight for same user
This commit is contained in:
muerwre 2021-10-06 12:48:26 +07:00 committed by GitHub
parent a7e8e19b06
commit e071409241
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 172 additions and 108 deletions

View file

@ -1,2 +1,2 @@
REACT_APP_API_HOST=https://pig.staging.vault48.org/ REACT_APP_API_HOST: http://localhost:3334/
REACT_APP_REMOTE_CURRENT=https://pig.staging.vault48.org/static/ REACT_APP_REMOTE_CURRENT: https://pig.staging.vault48.org/static/

View file

@ -5,40 +5,44 @@ import { CommentContent } from '~/components/comment/CommentContent';
import styles from './styles.module.scss'; import styles from './styles.module.scss';
import { CommendDeleted } from '../../node/CommendDeleted'; import { CommendDeleted } from '../../node/CommendDeleted';
import * as MODAL_ACTIONS from '~/redux/modal/actions'; import * as MODAL_ACTIONS from '~/redux/modal/actions';
import classNames from 'classnames';
import { NEW_COMMENT_CLASSNAME } from '~/constants/comment';
type IProps = HTMLAttributes<HTMLDivElement> & { type IProps = HTMLAttributes<HTMLDivElement> & {
is_empty?: boolean; is_empty?: boolean;
is_loading?: boolean; is_loading?: boolean;
comment_group: ICommentGroup; group: ICommentGroup;
is_same?: boolean; isSame?: boolean;
can_edit?: boolean; canEdit?: boolean;
onDelete: (id: IComment['id'], isLocked: boolean) => void; onDelete: (id: IComment['id'], isLocked: boolean) => void;
modalShowPhotoswipe: typeof MODAL_ACTIONS.modalShowPhotoswipe; modalShowPhotoswipe: typeof MODAL_ACTIONS.modalShowPhotoswipe;
}; };
const Comment: FC<IProps> = memo( const Comment: FC<IProps> = memo(
({ ({
comment_group, group,
is_empty, is_empty,
is_same, isSame,
is_loading, is_loading,
className, className,
can_edit, canEdit,
onDelete, onDelete,
modalShowPhotoswipe, modalShowPhotoswipe,
...props ...props
}) => { }) => {
return ( return (
<CommentWrapper <CommentWrapper
className={className} className={classNames(className, {
[NEW_COMMENT_CLASSNAME]: group.hasNew,
})}
isEmpty={is_empty} isEmpty={is_empty}
isLoading={is_loading} isLoading={is_loading}
user={comment_group.user} user={group.user}
isSame={is_same} isNew={group.hasNew && !isSame}
{...props} {...props}
> >
<div className={styles.wrap}> <div className={styles.wrap}>
{comment_group.comments.map(comment => { {group.comments.map(comment => {
if (comment.deleted_at) { if (comment.deleted_at) {
return <CommendDeleted id={comment.id} onDelete={onDelete} key={comment.id} />; return <CommendDeleted id={comment.id} onDelete={onDelete} key={comment.id} />;
} }
@ -47,7 +51,7 @@ const Comment: FC<IProps> = memo(
<CommentContent <CommentContent
comment={comment} comment={comment}
key={comment.id} key={comment.id}
can_edit={!!can_edit} can_edit={!!canEdit}
onDelete={onDelete} onDelete={onDelete}
modalShowPhotoswipe={modalShowPhotoswipe} modalShowPhotoswipe={modalShowPhotoswipe}
/> />

View file

@ -11,8 +11,8 @@ type IProps = DivProps & {
user: IUser; user: IUser;
isEmpty?: boolean; isEmpty?: boolean;
isLoading?: boolean; isLoading?: boolean;
isSame?: boolean;
isForm?: boolean; isForm?: boolean;
isNew?: boolean;
}; };
const CommentWrapper: FC<IProps> = ({ const CommentWrapper: FC<IProps> = ({
@ -20,16 +20,16 @@ const CommentWrapper: FC<IProps> = ({
className, className,
isEmpty, isEmpty,
isLoading, isLoading,
isSame,
isForm, isForm,
children, children,
isNew,
...props ...props
}) => ( }) => (
<div <div
className={classNames(styles.wrap, className, { className={classNames(styles.wrap, className, {
is_empty: isEmpty, [styles.is_empty]: isEmpty,
is_loading: isLoading, [styles.is_loading]: isLoading,
is_same: isSame, [styles.is_new]: isNew,
})} })}
{...props} {...props}
> >

View file

@ -1,5 +1,13 @@
@import "src/styles/variables"; @import "src/styles/variables";
@keyframes highlight {
0% { opacity: 0.75; }
25% { opacity: 0.5; }
50% { opacity: 0.75; }
75% { opacity: 0; }
100% { opacity: 0; }
}
.wrap { .wrap {
@include outer_shadow; @include outer_shadow;
@ -10,15 +18,27 @@
min-width: 0; min-width: 0;
border-radius: $radius; border-radius: $radius;
&:global(.is_empty) { &.is_empty {
opacity: 0.5; opacity: 0.5;
} }
&:global(.is_same) { &.is_same {
margin: 0 !important; margin: 0 !important;
border-radius: 0; border-radius: 0;
} }
&.is_new::after {
content: ' ';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
animation: highlight 1s 0.5s forwards;
background: transparentize($wisegreen, 0.7);
border-radius: $radius;
}
@include tablet { @include tablet {
flex-direction: column; flex-direction: column;
} }

View file

@ -23,6 +23,7 @@ interface IProps {
commentsCount: number; commentsCount: number;
isLoadingComments: boolean; isLoadingComments: boolean;
related: INodeRelated; related: INodeRelated;
lastSeenCurrent?: string;
} }
const NodeBottomBlock: FC<IProps> = ({ const NodeBottomBlock: FC<IProps> = ({
@ -34,6 +35,7 @@ const NodeBottomBlock: FC<IProps> = ({
commentsCount, commentsCount,
commentsOrder, commentsOrder,
related, related,
lastSeenCurrent,
}) => { }) => {
const { inline } = useNodeBlocks(node, isLoading); const { inline } = useNodeBlocks(node, isLoading);
const { is_user } = useUser(); const { is_user } = useUser();
@ -50,6 +52,7 @@ const NodeBottomBlock: FC<IProps> = ({
{inline && <div className={styles.inline}>{inline}</div>} {inline && <div className={styles.inline}>{inline}</div>}
<NodeCommentsBlock <NodeCommentsBlock
lastSeenCurrent={lastSeenCurrent}
isLoading={isLoading} isLoading={isLoading}
isLoadingComments={isLoadingComments} isLoadingComments={isLoadingComments}
comments={comments} comments={comments}

View file

@ -12,22 +12,22 @@ import { COMMENTS_DISPLAY } from '~/redux/node/constants';
import { plural } from '~/utils/dom'; import { plural } from '~/utils/dom';
import { modalShowPhotoswipe } from '~/redux/modal/actions'; import { modalShowPhotoswipe } from '~/redux/modal/actions';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { useGrouppedComments } from '~/utils/hooks/node/useGrouppedComments';
interface IProps { interface IProps {
comments: IComment[]; comments: IComment[];
count: INodeState['comment_count']; count: INodeState['comment_count'];
user: IUser; user: IUser;
order?: 'ASC' | 'DESC'; order?: 'ASC' | 'DESC';
lastSeenCurrent?: string;
} }
const NodeComments: FC<IProps> = memo(({ comments, user, count = 0, order = 'DESC' }) => { const NodeComments: FC<IProps> = memo(
({ comments, user, count = 0, order = 'DESC', lastSeenCurrent }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const left = useMemo(() => Math.max(0, count - comments.length), [comments, count]); const left = useMemo(() => Math.max(0, count - comments.length), [comments, count]);
const groupped: ICommentGroup[] = useMemo( const groupped: ICommentGroup[] = useGrouppedComments(comments, order, lastSeenCurrent);
() => (order === 'DESC' ? [...comments].reverse() : comments).reduce(groupCommentsByUser, []),
[comments, order]
);
const onDelete = useCallback( const onDelete = useCallback(
(id: IComment['id'], locked: boolean) => dispatch(nodeLockComment(id, locked)), (id: IComment['id'], locked: boolean) => dispatch(nodeLockComment(id, locked)),
@ -58,16 +58,18 @@ const NodeComments: FC<IProps> = memo(({ comments, user, count = 0, order = 'DES
{groupped.map(group => ( {groupped.map(group => (
<Comment <Comment
key={group.ids.join()} key={group.ids.join()}
comment_group={group} group={group}
can_edit={canEditComment(group, user)} canEdit={canEditComment(group, user)}
onDelete={onDelete} onDelete={onDelete}
modalShowPhotoswipe={onShowPhotoswipe} modalShowPhotoswipe={onShowPhotoswipe}
isSame={group.user.id === user.id}
/> />
))} ))}
{order === 'ASC' && more} {order === 'ASC' && more}
</div> </div>
); );
}); }
);
export { NodeComments }; export { NodeComments };

View file

@ -10,18 +10,32 @@ interface IProps {
node: INode; node: INode;
comments: IComment[]; comments: IComment[];
count: number; count: number;
lastSeenCurrent?: string;
isLoading: boolean; isLoading: boolean;
isLoadingComments: boolean; isLoadingComments: boolean;
} }
const NodeCommentsBlock: FC<IProps> = ({ isLoading, isLoadingComments, node, comments, count }) => { const NodeCommentsBlock: FC<IProps> = ({
isLoading,
isLoadingComments,
node,
comments,
count,
lastSeenCurrent,
}) => {
const user = useUser(); const user = useUser();
const { inline } = useNodeBlocks(node, isLoading); const { inline } = useNodeBlocks(node, isLoading);
return isLoading || isLoadingComments || (!comments.length && !inline) ? ( return isLoading || isLoadingComments || (!comments.length && !inline) ? (
<NodeNoComments is_loading={isLoadingComments || isLoading} /> <NodeNoComments is_loading={isLoadingComments || isLoading} />
) : ( ) : (
<NodeComments count={count} comments={comments} user={user} order="DESC" /> <NodeComments
count={count}
comments={comments}
user={user}
order="DESC"
lastSeenCurrent={lastSeenCurrent}
/>
); );
}; };

View file

@ -36,3 +36,5 @@ export const COMMENT_BLOCK_RENDERERS = {
[COMMENT_BLOCK_TYPES.MARK]: CommentTextBlock, [COMMENT_BLOCK_TYPES.MARK]: CommentTextBlock,
[COMMENT_BLOCK_TYPES.EMBED]: CommentEmbedBlock, [COMMENT_BLOCK_TYPES.EMBED]: CommentEmbedBlock,
}; };
export const NEW_COMMENT_CLASSNAME = 'newComment';

View file

@ -36,10 +36,11 @@ const NodeLayout: FC<IProps> = memo(
comment_count, comment_count,
is_loading_comments, is_loading_comments,
related, related,
lastSeenCurrent,
} = useShallowSelect(selectNode); } = useShallowSelect(selectNode);
useNodeCoverImage(current); useNodeCoverImage(current);
useScrollToTop([id]); useScrollToTop([id, is_loading_comments]);
useLoadNode(id, is_loading); useLoadNode(id, is_loading);
useOnNodeSeen(current); useOnNodeSeen(current);
@ -65,6 +66,7 @@ const NodeLayout: FC<IProps> = memo(
related={related} related={related}
isLoadingComments={is_loading_comments} isLoadingComments={is_loading_comments}
isLoading={is_loading} isLoading={is_loading}
lastSeenCurrent={lastSeenCurrent}
/> />
<Footer /> <Footer />

View file

@ -74,12 +74,6 @@ export const nodeSetRelated = (related: INodeState['related']) => ({
type: NODE_ACTIONS.SET_RELATED, type: NODE_ACTIONS.SET_RELATED,
}); });
export const nodeSetCommentData = (id: number, comment: Partial<IComment>) => ({
id,
comment,
type: NODE_ACTIONS.SET_COMMENT_DATA,
});
export const nodeUpdateTags = (id: INode['id'], tags: string[]) => ({ export const nodeUpdateTags = (id: INode['id'], tags: string[]) => ({
type: NODE_ACTIONS.UPDATE_TAGS, type: NODE_ACTIONS.UPDATE_TAGS,
id, id,

View file

@ -8,7 +8,7 @@ import {
ApiGetNodeRelatedRequest, ApiGetNodeRelatedRequest,
ApiGetNodeRelatedResult, ApiGetNodeRelatedResult,
ApiGetNodeRequest, ApiGetNodeRequest,
ApiGetNodeResult, ApiGetNodeResponse,
ApiLockCommentRequest, ApiLockCommentRequest,
ApiLockcommentResult, ApiLockcommentResult,
ApiLockNodeRequest, ApiLockNodeRequest,
@ -69,13 +69,13 @@ export const getNodeDiff = ({
.then(cleanResult); .then(cleanResult);
export const apiGetNode = ({ id }: ApiGetNodeRequest, config?: AxiosRequestConfig) => export const apiGetNode = ({ id }: ApiGetNodeRequest, config?: AxiosRequestConfig) =>
api.get<ApiGetNodeResult>(API.NODE.GET_NODE(id), config).then(cleanResult); api.get<ApiGetNodeResponse>(API.NODE.GET_NODE(id), config).then(cleanResult);
export const apiGetNodeWithCancel = ({ id }: ApiGetNodeRequest) => { export const apiGetNodeWithCancel = ({ id }: ApiGetNodeRequest) => {
const cancelToken = axios.CancelToken.source(); const cancelToken = axios.CancelToken.source();
return { return {
request: api request: api
.get<ApiGetNodeResult>(API.NODE.GET_NODE(id), { .get<ApiGetNodeResponse>(API.NODE.GET_NODE(id), {
cancelToken: cancelToken.token, cancelToken: cancelToken.token,
}) })
.then(cleanResult), .then(cleanResult),

View file

@ -44,7 +44,6 @@ export const NODE_ACTIONS = {
SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`, SET_LOADING_COMMENTS: `${prefix}SET_LOADING_COMMENTS`,
SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`, SET_SENDING_COMMENT: `${prefix}SET_SENDING_COMMENT`,
SET_CURRENT: `${prefix}SET_CURRENT`, SET_CURRENT: `${prefix}SET_CURRENT`,
SET_COMMENT_DATA: `${prefix}SET_COMMENT_DATA`,
SET_EDITOR: `${prefix}SET_EDITOR`, SET_EDITOR: `${prefix}SET_EDITOR`,
POST_COMMENT: `${prefix}POST_LOCAL_COMMENT`, POST_COMMENT: `${prefix}POST_LOCAL_COMMENT`,

View file

@ -7,7 +7,6 @@ import {
nodeSetLoadingComments, nodeSetLoadingComments,
nodeSetSendingComment, nodeSetSendingComment,
nodeSetComments, nodeSetComments,
nodeSetCommentData,
nodeSetTags, nodeSetTags,
nodeSetEditor, nodeSetEditor,
nodeSetCoverImage, nodeSetCoverImage,
@ -46,20 +45,6 @@ const setComments = (state: INodeState, { comments }: ReturnType<typeof nodeSetC
const setRelated = (state: INodeState, { related }: ReturnType<typeof nodeSetRelated>) => const setRelated = (state: INodeState, { related }: ReturnType<typeof nodeSetRelated>) =>
assocPath(['related'], related, state); assocPath(['related'], related, state);
const setCommentData = (
state: INodeState,
{ id, comment }: ReturnType<typeof nodeSetCommentData>
) => ({
...state,
comment_data: {
...state.comment_data,
[id]: {
...(state.comment_data[id] || {}),
...comment,
},
},
})
const setTags = (state: INodeState, { tags }: ReturnType<typeof nodeSetTags>) => const setTags = (state: INodeState, { tags }: ReturnType<typeof nodeSetTags>) =>
assocPath(['current', 'tags'], tags, state); assocPath(['current', 'tags'], tags, state);
@ -80,7 +65,6 @@ export const NODE_HANDLERS = {
[NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment, [NODE_ACTIONS.SET_SENDING_COMMENT]: setSendingComment,
[NODE_ACTIONS.SET_COMMENTS]: setComments, [NODE_ACTIONS.SET_COMMENTS]: setComments,
[NODE_ACTIONS.SET_RELATED]: setRelated, [NODE_ACTIONS.SET_RELATED]: setRelated,
[NODE_ACTIONS.SET_COMMENT_DATA]: setCommentData,
[NODE_ACTIONS.SET_TAGS]: setTags, [NODE_ACTIONS.SET_TAGS]: setTags,
[NODE_ACTIONS.SET_EDITOR]: setEditor, [NODE_ACTIONS.SET_EDITOR]: setEditor,
[NODE_ACTIONS.SET_COVER_IMAGE]: setCoverImage, [NODE_ACTIONS.SET_COVER_IMAGE]: setCoverImage,

View file

@ -9,7 +9,7 @@ export type INodeState = Readonly<{
current: INode; current: INode;
comments: IComment[]; comments: IComment[];
related: INodeRelated; related: INodeRelated;
comment_data: Record<number, IComment>; lastSeenCurrent?: string;
comment_count: number; comment_count: number;
current_cover_image?: IFile; current_cover_image?: IFile;
@ -29,11 +29,6 @@ const INITIAL_STATE: INodeState = {
files: [], files: [],
}, },
current: { ...EMPTY_NODE }, current: { ...EMPTY_NODE },
comment_data: {
0: {
...EMPTY_COMMENT,
},
},
comment_count: 0, comment_count: 0,
comments: [], comments: [],
related: { related: {

View file

@ -19,7 +19,6 @@ import {
nodeLockComment, nodeLockComment,
nodePostLocalComment, nodePostLocalComment,
nodeSet, nodeSet,
nodeSetCommentData,
nodeSetComments, nodeSetComments,
nodeSetCurrent, nodeSetCurrent,
nodeSetEditor, nodeSetEditor,
@ -43,9 +42,9 @@ import {
apiPostNodeLike, apiPostNodeLike,
apiPostNodeTags, apiPostNodeTags,
} from './api'; } from './api';
import { flowSetNodes, flowSetUpdated } from '../flow/actions'; import { flowSetNodes } from '../flow/actions';
import { modalSetShown, modalShowDialog } from '../modal/actions'; import { modalSetShown, modalShowDialog } from '../modal/actions';
import { selectFlow, selectFlowNodes } from '../flow/selectors'; import { selectFlowNodes } from '../flow/selectors';
import { URLS } from '~/constants/urls'; import { URLS } from '~/constants/urls';
import { selectNode } from './selectors'; import { selectNode } from './selectors';
import { INode, Unwrap } from '../types'; import { INode, Unwrap } from '../types';
@ -54,7 +53,6 @@ import { DIALOGS } from '~/redux/modal/constants';
import { has } from 'ramda'; import { has } from 'ramda';
import { selectLabListNodes } from '~/redux/lab/selectors'; import { selectLabListNodes } from '~/redux/lab/selectors';
import { labSetList } from '~/redux/lab/actions'; import { labSetList } from '~/redux/lab/actions';
import { INodeRelated } from '~/redux/node/types';
export function* updateNodeEverywhere(node) { export function* updateNodeEverywhere(node) {
const { const {
@ -119,7 +117,6 @@ function* onNodeGoto({ id, node_type }: ReturnType<typeof nodeGotoNode>) {
if (node_type) yield put(nodeSetCurrent({ ...EMPTY_NODE, type: node_type })); if (node_type) yield put(nodeSetCurrent({ ...EMPTY_NODE, type: node_type }));
yield put(nodeLoadNode(id)); yield put(nodeLoadNode(id));
yield put(nodeSetCommentData(0, { ...EMPTY_COMMENT }));
yield put(nodeSetRelated({ albums: {}, similar: [] })); yield put(nodeSetRelated({ albums: {}, similar: [] }));
} }
@ -188,9 +185,9 @@ function* onNodeLoad({ id }: ReturnType<typeof nodeLoadNode>) {
yield put(nodeSetLoading(true)); yield put(nodeSetLoading(true));
yield put(nodeSetLoadingComments(true)); yield put(nodeSetLoadingComments(true));
const { node }: Unwrap<typeof apiGetNode> = yield call(apiGetNode, { id }); const { node, last_seen }: Unwrap<typeof apiGetNode> = yield call(apiGetNode, { id });
yield put(nodeSetCurrent(node)); yield put(nodeSet({ current: node, lastSeenCurrent: last_seen }));
yield put(nodeSetLoading(false)); yield put(nodeSetLoading(false));
} catch (error) { } catch (error) {
yield put(push(URLS.ERRORS.NOT_FOUND)); yield put(push(URLS.ERRORS.NOT_FOUND));

View file

@ -31,7 +31,7 @@ export type PostCellViewResult = unknown; // TODO: update it with actual type
export type ApiGetNodeRequest = { export type ApiGetNodeRequest = {
id: string | number; id: string | number;
}; };
export type ApiGetNodeResult = { node: INode }; export type ApiGetNodeResponse = { node: INode; last_seen?: string };
export type ApiGetNodeRelatedRequest = { export type ApiGetNodeRelatedRequest = {
id: INode['id']; id: INode['id'];

View file

@ -165,6 +165,7 @@ export interface ICommentGroup {
user: IUser; user: IUser;
comments: IComment[]; comments: IComment[];
ids: IComment['id'][]; ids: IComment['id'][];
hasNew: boolean;
} }
export type IUploadProgressHandler = (progress: ProgressEvent) => void; export type IUploadProgressHandler = (progress: ProgressEvent) => void;

View file

@ -4,12 +4,28 @@ import { nth } from 'ramda';
import { remove } from 'ramda'; import { remove } from 'ramda';
import { ICommentGroup, IComment } from '~/redux/types'; import { ICommentGroup, IComment } from '~/redux/types';
import { path } from 'ramda'; import { path } from 'ramda';
import { isAfter, isValid, parseISO } from 'date-fns';
export const moveArrItem = curry((at, to, list) => insert(to, nth(at, list), remove(at, 1, list))); export const moveArrItem = curry((at, to, list) => insert(to, nth(at, list), remove(at, 1, list)));
export const objFromArray = (array: any[], key: string) => export const objFromArray = (array: any[], key: string) =>
array.reduce((obj, el) => (key && el[key] ? { ...obj, [el[key]]: el } : obj), {}); array.reduce((obj, el) => (key && el[key] ? { ...obj, [el[key]]: el } : obj), {});
export const groupCommentsByUser = ( const compareCommentDates = (commentDateValue?: string, lastSeenDateValue?: string) => {
if (!commentDateValue || !lastSeenDateValue) {
return false;
}
const commentDate = parseISO(commentDateValue);
const lastSeenDate = parseISO(lastSeenDateValue);
if (!isValid(commentDate) || !isValid(lastSeenDate)) {
return false;
}
return isAfter(commentDate, lastSeenDate);
};
export const groupCommentsByUser = (lastSeen?: string) => (
grouppedComments: ICommentGroup[], grouppedComments: ICommentGroup[],
comment: IComment comment: IComment
): ICommentGroup[] => { ): ICommentGroup[] => {
@ -28,6 +44,7 @@ export const groupCommentsByUser = (
user: comment.user, user: comment.user,
comments: [comment], comments: [comment],
ids: [comment.id], ids: [comment.id],
hasNew: compareCommentDates(comment.created_at, lastSeen),
}, },
] ]
: [ : [
@ -37,6 +54,7 @@ export const groupCommentsByUser = (
...last, ...last,
comments: [...last.comments, comment], comments: [...last.comments, comment],
ids: [...last.ids, comment.id], ids: [...last.ids, comment.id],
hasNew: last.hasNew || compareCommentDates(comment.created_at, lastSeen),
}, },
]), ]),
]; ];

View file

@ -0,0 +1,17 @@
import { IComment } from '~/redux/types';
import { useMemo } from 'react';
import { groupCommentsByUser } from '~/utils/fn';
export const useGrouppedComments = (
comments: IComment[],
order: 'ASC' | 'DESC',
lastSeen?: string
) =>
useMemo(
() =>
(order === 'DESC' ? [...comments].reverse() : comments).reduce(
groupCommentsByUser(lastSeen),
[]
),
[comments, order]
);

View file

@ -1,7 +1,19 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { NEW_COMMENT_CLASSNAME } from '~/constants/comment';
export const useScrollToTop = (deps?: any[]) => { export const useScrollToTop = (deps?: any[]) => {
useEffect(() => { useEffect(() => {
const targetElement = document.querySelector(`.${NEW_COMMENT_CLASSNAME}`);
if (!targetElement) {
window.scrollTo(0, 0); window.scrollTo(0, 0);
return;
}
const bounds = targetElement.getBoundingClientRect();
window.scrollTo({
top: bounds.top - 100,
behavior: 'smooth',
});
}, deps || []); }, deps || []);
}; };