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'
|
2023-01-20 04:40:55 +00:00
|
|
|
import { Show, createMemo, createSignal, For } from 'solid-js'
|
2022-09-09 11:53:35 +00:00
|
|
|
import { clsx } from 'clsx'
|
2023-01-20 04:40:55 +00:00
|
|
|
import type { Author, Reaction } from '../../graphql/types.gen'
|
2022-09-09 11:53:35 +00:00
|
|
|
import { t } from '../../utils/intl'
|
2023-01-20 04:40:55 +00:00
|
|
|
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'
|
2023-01-20 04:40:55 +00:00
|
|
|
import { useSession } from '../../context/session'
|
|
|
|
import { ReactionKind } from '../../graphql/types.gen'
|
|
|
|
import GrowingTextarea from '../_shared/GrowingTextarea'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2023-01-20 04:40:55 +00:00
|
|
|
type Props = {
|
|
|
|
comment: Reaction
|
2022-09-09 11:53:35 +00:00
|
|
|
compact?: boolean
|
2023-01-20 04:40:55 +00:00
|
|
|
reactions?: Reaction[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const Comment = (props: Props) => {
|
2022-11-26 21:27:54 +00:00
|
|
|
const [isReplyVisible, setIsReplyVisible] = createSignal(false)
|
2023-01-20 04:40:55 +00:00
|
|
|
const [loading, setLoading] = createSignal(false)
|
|
|
|
const [errorMessage, setErrorMessage] = createSignal<string | null>(null)
|
|
|
|
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())
|
2023-01-20 04:40:55 +00:00
|
|
|
const remove = async () => {
|
2022-09-09 11:53:35 +00:00
|
|
|
if (comment()?.id) {
|
2023-01-20 04:40:55 +00:00
|
|
|
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)
|
|
|
|
setLoading(false)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('[handleCreate reaction]:', error)
|
|
|
|
setErrorMessage(t('Something went wrong, please try again'))
|
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 (
|
2023-01-20 04:40:55 +00:00
|
|
|
<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>
|
|
|
|
|
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}
|
2023-01-20 04:40:55 +00:00
|
|
|
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
|
2023-01-20 04:40:55 +00:00
|
|
|
disabled={loading()}
|
2022-11-26 21:27:54 +00:00
|
|
|
onClick={() => setIsReplyVisible(!isReplyVisible())}
|
2023-01-20 04:40:55 +00:00
|
|
|
class={clsx(styles.commentControl, styles.commentControlReply)}
|
2022-11-26 21:27:54 +00:00
|
|
|
>
|
|
|
|
<Icon name="reply" class={styles.icon} />
|
2023-01-20 04:40:55 +00:00
|
|
|
{loading() ? t('Loading') : t('Reply')}
|
2022-09-09 11:53:35 +00:00
|
|
|
</button>
|
|
|
|
|
2023-01-20 04:40:55 +00:00
|
|
|
<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()}>
|
2023-01-20 04:40:55 +00:00
|
|
|
<GrowingTextarea
|
|
|
|
placeholder={t('Write comment')}
|
|
|
|
submitButtonText={t('Send')}
|
|
|
|
cancelButtonText={t('cancel')}
|
|
|
|
submit={(value) => handleCreate(value)}
|
|
|
|
loading={loading()}
|
|
|
|
errorMessage={errorMessage()}
|
|
|
|
/>
|
2022-11-26 21:27:54 +00:00
|
|
|
</Show>
|
2022-09-09 11:53:35 +00:00
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-01-20 04:40:55 +00:00
|
|
|
<Show when={props.reactions}>
|
|
|
|
<ul>
|
|
|
|
<For each={props.reactions.filter((r) => r.replyTo === props.comment.id)}>
|
|
|
|
{(reaction) => <Comment reactions={props.reactions} comment={reaction} />}
|
|
|
|
</For>
|
|
|
|
</ul>
|
|
|
|
</Show>
|
|
|
|
</li>
|
2022-09-09 11:53:35 +00:00
|
|
|
)
|
|
|
|
}
|
2023-01-20 04:40:55 +00:00
|
|
|
|
|
|
|
export default Comment
|