1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-26 05:16:41 +07:00

refactored node comments to component

This commit is contained in:
Fedor Katurov 2021-11-06 21:14:50 +07:00
parent b44266437d
commit ab15a10d01
13 changed files with 208 additions and 111 deletions

View file

@ -1,26 +1,24 @@
import React, { FC, useCallback } from 'react';
import { INode } from '~/redux/types';
import styles from './styles.module.scss';
import { Avatar } from '~/components/common/Avatar';
import { openUserProfile } from '~/utils/user';
import { useUserDescription } from '~/utils/hooks/user/useUserDescription';
import { INodeUser } from '~/redux/types';
interface Props {
node?: INode;
user?: INodeUser;
}
const NodeAuthorBlock: FC<Props> = ({ node }) => {
const onOpenProfile = useCallback(() => openUserProfile(node?.user?.username), [
node?.user?.username,
]);
const NodeAuthorBlock: FC<Props> = ({ user }) => {
const onOpenProfile = useCallback(() => openUserProfile(user?.username), [user?.username]);
const description = useUserDescription(node?.user);
const description = useUserDescription(user);
if (!node?.user) {
if (!user) {
return null;
}
const { fullname, username, photo } = node.user;
const { fullname, username, photo } = user;
return (
<div className={styles.block} onClick={onOpenProfile}>

View file

@ -6,16 +6,18 @@ import { NodeCommentsBlock } from '~/components/node/NodeCommentsBlock';
import { NodeCommentForm } from '~/components/node/NodeCommentForm';
import { NodeRelatedBlock } from '~/components/node/NodeRelatedBlock';
import { useNodeBlocks } from '~/utils/hooks/node/useNodeBlocks';
import { IComment, INode } from '~/redux/types';
import { useUser } from '~/utils/hooks/user/userUser';
import { IComment, IFile, INode } from '~/redux/types';
import { NodeTagsBlock } from '~/components/node/NodeTagsBlock';
import { INodeRelated } from '~/redux/node/types';
import StickyBox from 'react-sticky-box/dist/esnext';
import styles from './styles.module.scss';
import { NodeAuthorBlock } from '~/components/node/NodeAuthorBlock';
import { IUser } from '~/redux/auth/types';
interface IProps {
node: INode;
user: IUser;
isUser: boolean;
canEdit: boolean;
isLoading: boolean;
commentsOrder: 'ASC' | 'DESC';
@ -24,21 +26,28 @@ interface IProps {
isLoadingComments: boolean;
related: INodeRelated;
lastSeenCurrent?: string;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeBottomBlock: FC<IProps> = ({
node,
user,
canEdit,
isLoading,
isUser,
isLoadingComments,
comments,
commentsCount,
commentsOrder,
related,
lastSeenCurrent,
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
}) => {
const { inline } = useNodeBlocks(node, isLoading);
const { is_user } = useUser();
if (node.deleted_at) {
return <NodeDeletedBadge />;
@ -58,17 +67,21 @@ const NodeBottomBlock: FC<IProps> = ({
comments={comments}
count={commentsCount}
order={commentsOrder}
user={user}
node={node}
onShowImageModal={onShowImageModal}
onLoadMoreComments={onLoadMoreComments}
onDeleteComment={onDeleteComment}
/>
{is_user && !isLoading && <NodeCommentForm nodeId={node.id} />}
{isUser && !isLoading && <NodeCommentForm nodeId={node.id} />}
</Group>
<div className={styles.panel}>
<StickyBox className={styles.sticky} offsetTop={72}>
<div className={styles.left}>
<div className={styles.left_item}>
<NodeAuthorBlock node={node} />
<NodeAuthorBlock user={node?.user} />
</div>
<div className={styles.left_item}>
<NodeTagsBlock node={node} canEdit={canEdit} isLoading={isLoading} />

View file

@ -1,17 +1,13 @@
import React, { FC, memo, useCallback, useMemo } from 'react';
import React, { FC, memo, useMemo } from 'react';
import { Comment } from '../../comment/Comment';
import styles from './styles.module.scss';
import { IComment, ICommentGroup, IFile } from '~/redux/types';
import { groupCommentsByUser } from '~/utils/fn';
import { IUser } from '~/redux/auth/types';
import { canEditComment } from '~/utils/node';
import { nodeLoadMoreComments, nodeLockComment } from '~/redux/node/actions';
import { INodeState } from '~/redux/node/reducer';
import { COMMENTS_DISPLAY } from '~/redux/node/constants';
import { plural } from '~/utils/dom';
import { modalShowPhotoswipe } from '~/redux/modal/actions';
import { useDispatch } from 'react-redux';
import { useGrouppedComments } from '~/utils/hooks/node/useGrouppedComments';
interface IProps {
@ -20,25 +16,26 @@ interface IProps {
user: IUser;
order?: 'ASC' | 'DESC';
lastSeenCurrent?: string;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeComments: FC<IProps> = memo(
({ comments, user, count = 0, order = 'DESC', lastSeenCurrent }) => {
const dispatch = useDispatch();
({
comments,
user,
count = 0,
order = 'DESC',
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
lastSeenCurrent,
}) => {
const left = useMemo(() => Math.max(0, count - comments.length), [comments, count]);
const groupped: ICommentGroup[] = useGrouppedComments(comments, order, lastSeenCurrent);
const onDelete = useCallback(
(id: IComment['id'], locked: boolean) => dispatch(nodeLockComment(id, locked)),
[dispatch]
);
const onLoadMoreComments = useCallback(() => dispatch(nodeLoadMoreComments()), [dispatch]);
const onShowPhotoswipe = useCallback(
(images: IFile[], index: number) => dispatch(modalShowPhotoswipe(images, index)),
[dispatch]
);
const more = useMemo(
() =>
left > 0 && (
@ -60,8 +57,8 @@ const NodeComments: FC<IProps> = memo(
key={group.ids.join()}
group={group}
canEdit={canEditComment(group, user)}
onDelete={onDelete}
modalShowPhotoswipe={onShowPhotoswipe}
onDelete={onDeleteComment}
onShowImageModal={onShowImageModal}
isSame={group.user.id === user.id}
/>
))}

View file

@ -1,29 +1,36 @@
import React, { FC } from 'react';
import { NodeNoComments } from '~/components/node/NodeNoComments';
import { NodeComments } from '~/components/node/NodeComments';
import { IComment, INode } from '~/redux/types';
import { IComment, IFile, INode } from '~/redux/types';
import { useNodeBlocks } from '~/utils/hooks/node/useNodeBlocks';
import { useUser } from '~/utils/hooks/user/userUser';
import { IUser } from '~/redux/auth/types';
interface IProps {
order: 'ASC' | 'DESC';
node: INode;
user: IUser;
comments: IComment[];
count: number;
lastSeenCurrent?: string;
isLoading: boolean;
isLoadingComments: boolean;
onShowImageModal: (images: IFile[], index: number) => void;
onLoadMoreComments: () => void;
onDeleteComment: (id: IComment['id'], isLocked: boolean) => void;
}
const NodeCommentsBlock: FC<IProps> = ({
isLoading,
isLoadingComments,
node,
user,
comments,
count,
lastSeenCurrent,
isLoading,
isLoadingComments,
onLoadMoreComments,
onDeleteComment,
onShowImageModal,
}) => {
const user = useUser();
const { inline } = useNodeBlocks(node, isLoading);
return isLoading || isLoadingComments || (!comments.length && !inline) ? (
@ -35,6 +42,9 @@ const NodeCommentsBlock: FC<IProps> = ({
user={user}
order="DESC"
lastSeenCurrent={lastSeenCurrent}
onShowImageModal={onShowImageModal}
onLoadMoreComments={onLoadMoreComments}
onDeleteComment={onDeleteComment}
/>
);
};