webapp/src/components/Article/Comment.tsx

203 lines
6.7 KiB
TypeScript
Raw Normal View History

2022-11-26 21:27:54 +00:00
import styles from './Comment.module.scss'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-09-09 11:53:35 +00:00
import { AuthorCard } from '../Author/Card'
import { Show, createMemo, createSignal, For } from 'solid-js'
2022-09-09 11:53:35 +00:00
import { clsx } from 'clsx'
import type { Author, Reaction } from '../../graphql/types.gen'
2022-09-09 11:53:35 +00:00
import { t } from '../../utils/intl'
import { createReaction, deleteReaction } from '../../stores/zine/reactions'
2022-10-07 11:02:34 +00:00
import MD from './MD'
2022-11-26 21:27:54 +00:00
import { formatDate } from '../../utils'
import { SharePopup } from './SharePopup'
import stylesHeader from '../Nav/Header.module.scss'
2022-11-27 17:02:04 +00:00
import Userpic from '../Author/Userpic'
import { useSession } from '../../context/session'
import { ReactionKind } from '../../graphql/types.gen'
import CommentEditor from '../_shared/CommentEditor'
import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient'
2022-09-09 11:53:35 +00:00
type Props = {
comment: Reaction
2022-09-09 11:53:35 +00:00
compact?: boolean
reactions?: Reaction[]
isArticleAuthor?: boolean
}
const Comment = (props: Props) => {
2022-11-26 21:27:54 +00:00
const [isReplyVisible, setIsReplyVisible] = createSignal(false)
const [loading, setLoading] = createSignal<boolean>(false)
const [submitted, setSubmitted] = createSignal<boolean>(false)
const { session } = useSession()
const canEdit = createMemo(() => props.comment.createdBy?.slug === session()?.user?.slug)
2022-11-26 21:27:54 +00:00
2022-09-09 11:53:35 +00:00
const comment = createMemo(() => props.comment)
2022-11-26 01:46:34 +00:00
const body = createMemo(() => (comment().body || '').trim())
const remove = async () => {
2022-09-09 11:53:35 +00:00
if (comment()?.id) {
try {
await deleteReaction(comment().id)
} catch (error) {
console.error('[deleteReaction]', error)
}
}
}
const handleCreate = async (value) => {
try {
setLoading(true)
await createReaction(
{
kind: ReactionKind.Comment,
replyTo: props.comment.id,
body: value,
shout: props.comment.shout.id
},
{
name: session().user.name,
userpic: session().user.userpic,
slug: session().user.slug
}
)
setIsReplyVisible(false)
setSubmitted(true)
setLoading(false)
} catch (error) {
console.error('[handleCreate reaction]:', error)
2022-09-09 11:53:35 +00:00
}
}
2022-11-26 21:27:54 +00:00
const formattedDate = createMemo(() =>
formatDate(new Date(comment()?.createdAt), { hour: 'numeric', minute: 'numeric' })
)
2022-09-09 11:53:35 +00:00
return (
<li class={styles.comment}>
2022-09-09 11:53:35 +00:00
<Show when={!!body()}>
2022-11-26 21:27:54 +00:00
<div class={styles.commentContent}>
2022-09-09 11:53:35 +00:00
<Show
when={!props.compact}
fallback={
2022-11-27 17:02:04 +00:00
<div>
<Userpic user={comment().createdBy as Author} isBig={false} isAuthorsList={false} />
<small class={styles.commentArticle}>
<a href={`#comment-${comment()?.id}`}>{comment()?.shout.title || ''}</a>
</small>
2022-09-09 11:53:35 +00:00
</div>
}
>
2022-11-26 21:27:54 +00:00
<div class={styles.commentDetails}>
<div class={styles.commentAuthor}>
2022-09-09 11:53:35 +00:00
<AuthorCard
author={comment()?.createdBy as Author}
hideDescription={true}
hideFollow={true}
2022-11-26 21:27:54 +00:00
isComments={true}
hasLink={true}
2022-09-09 11:53:35 +00:00
/>
</div>
<Show when={props.isArticleAuthor}>
<div class={styles.articleAuthor}>{t('Author')}</div>
</Show>
2022-11-26 21:27:54 +00:00
<div class={styles.commentDate}>{formattedDate()}</div>
<div
class={styles.commentRating}
classList={{
[styles.commentRatingPositive]: comment().stat?.rating > 0,
[styles.commentRatingNegative]: comment().stat?.rating < 0
}}
>
2022-12-07 20:10:26 +00:00
<button class={clsx(styles.commentRatingControl, styles.commentRatingControlUp)} />
2022-11-26 21:27:54 +00:00
<div class={styles.commentRatingValue}>{comment().stat?.rating || 0}</div>
2022-12-07 20:10:26 +00:00
<button class={clsx(styles.commentRatingControl, styles.commentRatingControlDown)} />
2022-11-26 21:27:54 +00:00
</div>
2022-09-09 11:53:35 +00:00
</div>
</Show>
2022-11-26 21:27:54 +00:00
<div
class={styles.commentBody}
contenteditable={canEdit()}
2022-11-26 21:27:54 +00:00
id={'comment-' + (comment().id || '')}
>
2022-10-07 11:02:34 +00:00
<MD body={body()} />
2022-09-09 11:53:35 +00:00
</div>
<Show when={!props.compact}>
2022-11-26 21:27:54 +00:00
<div class={styles.commentControls}>
<button
disabled={loading()}
2022-11-26 21:27:54 +00:00
onClick={() => setIsReplyVisible(!isReplyVisible())}
class={clsx(styles.commentControl, styles.commentControlReply)}
2022-11-26 21:27:54 +00:00
>
<Icon name="reply" class={styles.icon} />
{loading() ? t('Loading') : t('Reply')}
2022-09-09 11:53:35 +00:00
</button>
<Show when={canEdit()}>
2022-09-13 09:59:04 +00:00
{/*FIXME implement edit comment modal*/}
2022-09-09 11:53:35 +00:00
{/*<button*/}
2022-11-26 21:27:54 +00:00
{/* class={clsx(styles.commentControl, styles.commentControlEdit)}*/}
2022-09-09 11:53:35 +00:00
{/* onClick={() => showModal('editComment')}*/}
{/*>*/}
2022-11-26 21:27:54 +00:00
{/* <Icon name="edit" class={styles.icon} />*/}
2022-09-09 11:53:35 +00:00
{/* {t('Edit')}*/}
{/*</button>*/}
2022-11-26 21:27:54 +00:00
<button
class={clsx(styles.commentControl, styles.commentControlDelete)}
onClick={() => remove()}
>
<Icon name="delete" class={styles.icon} />
2022-09-09 11:53:35 +00:00
{t('Delete')}
</button>
</Show>
2022-11-26 21:27:54 +00:00
<SharePopup
containerCssClass={stylesHeader.control}
trigger={
<button class={clsx(styles.commentControl, styles.commentControlShare)}>
2022-12-04 15:10:27 +00:00
<Icon name="share" class={styles.icon} />
2022-11-26 21:27:54 +00:00
{t('Share')}
</button>
}
/>
2022-09-09 11:53:35 +00:00
{/*<button*/}
2022-11-26 21:27:54 +00:00
{/* class={clsx(styles.commentControl, styles.commentControlComplain)}*/}
2022-09-09 11:53:35 +00:00
{/* onClick={() => showModal('reportComment')}*/}
{/*>*/}
{/* {t('Report')}*/}
{/*</button>*/}
</div>
2022-11-26 21:27:54 +00:00
<Show when={isReplyVisible()}>
<ShowOnlyOnClient>
<CommentEditor
initialValue={''}
clear={submitted()}
onSubmit={(value) => handleCreate(value)}
/>
</ShowOnlyOnClient>
2022-11-26 21:27:54 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</Show>
</div>
</Show>
<Show when={props.reactions}>
<ul>
<For each={props.reactions.filter((r) => r.replyTo === props.comment.id)}>
{(reaction) => (
<Comment
isArticleAuthor={props.isArticleAuthor}
reactions={props.reactions}
comment={reaction}
/>
)}
</For>
</ul>
</Show>
</li>
2022-09-09 11:53:35 +00:00
)
}
export default Comment