mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
added user context
This commit is contained in:
parent
f631f79654
commit
e881512f63
13 changed files with 116 additions and 160 deletions
64
src/views/node/NodeComments/index.tsx
Normal file
64
src/views/node/NodeComments/index.tsx
Normal file
|
@ -0,0 +1,64 @@
|
|||
import React, { FC, memo, useMemo } from 'react';
|
||||
|
||||
import styles from './styles.module.scss';
|
||||
import { ICommentGroup } from '~/redux/types';
|
||||
import { canEditComment } from '~/utils/node';
|
||||
import { COMMENTS_DISPLAY } from '~/redux/node/constants';
|
||||
import { plural } from '~/utils/dom';
|
||||
import { useGrouppedComments } from '~/utils/hooks/node/useGrouppedComments';
|
||||
import { useCommentContext } from '~/utils/providers/CommentProvider';
|
||||
import { Comment } from '~/components/comment/Comment';
|
||||
import { useUserContext } from '~/utils/providers/UserProvider';
|
||||
|
||||
interface IProps {
|
||||
order: 'ASC' | 'DESC';
|
||||
}
|
||||
|
||||
const NodeComments: FC<IProps> = memo(({ order }) => {
|
||||
const user = useUserContext();
|
||||
const {
|
||||
comments,
|
||||
count,
|
||||
lastSeenCurrent,
|
||||
onLoadMoreComments,
|
||||
onDeleteComment,
|
||||
onShowImageModal,
|
||||
} = useCommentContext();
|
||||
|
||||
const left = useMemo(() => Math.max(0, count - comments.length), [comments, count]);
|
||||
|
||||
const groupped: ICommentGroup[] = useGrouppedComments(comments, order, lastSeenCurrent);
|
||||
|
||||
const more = useMemo(
|
||||
() =>
|
||||
left > 0 && (
|
||||
<div className={styles.more} onClick={onLoadMoreComments}>
|
||||
Показать ещё{' '}
|
||||
{plural(Math.min(left, COMMENTS_DISPLAY), 'комментарий', 'комментария', 'комментариев')}
|
||||
{left > COMMENTS_DISPLAY ? ` из ${left} оставшихся` : ''}
|
||||
</div>
|
||||
),
|
||||
[left, onLoadMoreComments]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={styles.wrap}>
|
||||
{order === 'DESC' && more}
|
||||
|
||||
{groupped.map(group => (
|
||||
<Comment
|
||||
key={group.ids.join()}
|
||||
group={group}
|
||||
canEdit={canEditComment(group, user)}
|
||||
onDelete={onDeleteComment}
|
||||
onShowImageModal={onShowImageModal}
|
||||
isSame={group.user.id === user.id}
|
||||
/>
|
||||
))}
|
||||
|
||||
{order === 'ASC' && more}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
export { NodeComments };
|
Loading…
Add table
Add a link
Reference in a new issue