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

refactored comment form

This commit is contained in:
Fedor Katurov 2020-11-09 17:55:00 +07:00
parent af9b206a78
commit 9db25afb28
19 changed files with 235 additions and 153 deletions

View file

@ -0,0 +1,104 @@
import React, { FC, useMemo, memo, createElement, useCallback, useState } from 'react';
import { IComment, IFile } from '~/redux/types';
import path from 'ramda/es/path';
import { formatCommentText, getURL, getPrettyDate } from '~/utils/dom';
import { Group } from '~/components/containers/Group';
import styles from './styles.module.scss';
import { UPLOAD_TYPES } from '~/redux/uploads/constants';
import assocPath from 'ramda/es/assocPath';
import append from 'ramda/es/append';
import reduce from 'ramda/es/reduce';
import { AudioPlayer } from '~/components/media/AudioPlayer';
import classnames from 'classnames';
import { PRESETS } from '~/constants/urls';
import { COMMENT_BLOCK_RENDERERS } from '~/constants/comment';
import { nodeLockComment, nodeEditComment } from '~/redux/node/actions';
import { CommentMenu } from '../CommentMenu';
import * as MODAL_ACTIONS from '~/redux/modal/actions';
interface IProps {
comment: IComment;
can_edit: boolean;
onDelete: typeof nodeLockComment;
onEdit: typeof nodeEditComment;
modalShowPhotoswipe: typeof MODAL_ACTIONS.modalShowPhotoswipe;
}
const CommentContent: FC<IProps> = memo(
({ comment, can_edit, onDelete, onEdit, modalShowPhotoswipe }) => {
const groupped = useMemo<Record<keyof typeof UPLOAD_TYPES, IFile[]>>(
() =>
reduce(
(group, file) => assocPath([file.type], append(file, group[file.type]), group),
{},
comment.files
),
[comment]
);
const onLockClick = useCallback(() => {
onDelete(comment.id, !comment.deleted_at);
}, [comment, onDelete]);
const onEditClick = useCallback(() => {
onEdit(comment.id);
}, [comment, onEdit]);
const menu = useMemo(
() => can_edit && <CommentMenu onDelete={onLockClick} onEdit={onEditClick} />,
[can_edit, comment, onEditClick, onLockClick]
);
return (
<div className={styles.wrap}>
{comment.text && (
<Group className={classnames(styles.block, styles.block_text)}>
{menu}
<Group className={styles.renderers}>
{formatCommentText(path(['user', 'username'], comment), comment.text).map(
(block, key) =>
COMMENT_BLOCK_RENDERERS[block.type] &&
createElement(COMMENT_BLOCK_RENDERERS[block.type], { block, key })
)}
</Group>
<div className={styles.date}>{getPrettyDate(comment.created_at)}</div>
</Group>
)}
{groupped.image && groupped.image.length > 0 && (
<div className={classnames(styles.block, styles.block_image)}>
{menu}
<div className={styles.images}>
{groupped.image.map((file, index) => (
<div key={file.id} onClick={() => modalShowPhotoswipe(groupped.image, index)}>
<img src={getURL(file, PRESETS['600'])} alt={file.name} />
</div>
))}
</div>
<div className={styles.date}>{getPrettyDate(comment.created_at)}</div>
</div>
)}
{groupped.audio && groupped.audio.length > 0 && (
<>
{groupped.audio.map(file => (
<div className={classnames(styles.block, styles.block_audio)} key={file.id}>
{menu}
<AudioPlayer file={file} />
<div className={styles.date}>{getPrettyDate(comment.created_at)}</div>
</div>
))}
</>
)}
</div>
);
}
);
export { CommentContent };

View file

@ -0,0 +1,145 @@
@import 'flexbin/flexbin.scss';
.wrap {
position: relative;
}
.lock,
.edit {
position: absolute;
right: 0;
top: 0;
width: 32px;
height: 32px;
border-radius: $radius;
transform: translate(10px, 0);
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
pointer-events: none;
touch-action: none;
transition: opacity 0.25s, transform 0.25s;
cursor: pointer;
background: $red;
z-index: 2;
& > div {
width: 20px;
height: 20px;
border-radius: 100%;
display: flex;
align-items: center;
justify-content: center;
}
svg {
width: 16px;
height: 16px;
}
@include tablet {
right: 0;
border-radius: 0 0 0 $radius;
opacity: 1;
transform: translate(0, 0);
background: transparentize($red, $amount: 0.5);
}
}
.edit {
top: 28px;
background: blue;
}
.block {
@include outer_shadow();
min-height: $comment_height;
// box-shadow: inset rgba(255, 255, 255, 0.05) 1px 1px, inset rgba(0, 0, 0, 0.1) -1px -1px;
display: flex;
align-items: flex-start;
justify-content: flex-start;
position: relative;
padding-bottom: 10px;
box-sizing: border-box;
flex-direction: column;
// padding: $gap 0;
&:first-child {
border-top-right-radius: $radius;
}
&:last-child {
border-bottom-right-radius: $radius;
}
&:hover {
.lock,
.edit {
opacity: 1;
pointer-events: all;
touch-action: initial;
transform: translate(0, 0);
}
}
}
.block_audio {
align-items: stretch;
justify-content: center;
padding-bottom: 0 !important;
}
.block_image {
padding-bottom: 0 !important;
.date {
background: transparentize($color: $content_bg, $amount: 0.2);
border-radius: $radius 0 $radius 0;
color: transparentize(white, 0.2);
}
}
.block_text {
padding: $gap / 2 0;
}
.date {
position: absolute;
bottom: 0;
right: 0;
font: $font_12_regular;
color: transparentize($color: white, $amount: 0.8);
padding: 0 6px 2px;
border-radius: 0 0 $radius 0;
z-index: 2;
background: $comment_bg;
border-radius: 4px;
pointer-events: none;
touch-action: none;
}
.images {
@include flexbin(240px, 5px);
img {
border-radius: $radius;
}
}
.audios {
& > div {
height: $comment_height;
border-radius: $radius;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
}
.renderers {
width: 100%;
margin: 0 !important;
}