webapp/src/components/Article/Comment.tsx

153 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-11-26 21:27:54 +00:00
import styles from './Comment.module.scss'
2022-12-16 07:17:50 +00:00
import type { JSX } from 'solid-js/jsx-runtime'
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'
2022-11-26 21:27:54 +00:00
import { Show, createMemo, createSignal } from 'solid-js'
2022-09-09 11:53:35 +00:00
import { clsx } from 'clsx'
import type { Author, Reaction as Point } from '../../graphql/types.gen'
import { t } from '../../utils/intl'
// import { createReaction, updateReaction, deleteReaction } from '../../stores/zine/reactions'
2022-10-07 11:02:34 +00:00
import MD from './MD'
2022-09-13 09:59:04 +00:00
import { deleteReaction } from '../../stores/zine/reactions'
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'
2022-09-09 11:53:35 +00:00
export default (props: {
level?: number
comment: Partial<Point>
canEdit?: boolean
compact?: boolean
2022-12-16 07:17:50 +00:00
children?: JSX.Element[]
2022-09-09 11:53:35 +00:00
}) => {
2022-11-26 21:27:54 +00:00
const [isReplyVisible, setIsReplyVisible] = createSignal(false)
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())
2022-09-09 11:53:35 +00:00
const remove = () => {
if (comment()?.id) {
console.log('[comment] removing', comment().id)
2022-09-13 09:59:04 +00:00
deleteReaction(comment().id)
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 (
2022-12-16 07:17:50 +00:00
<li class={clsx(styles.comment, { [styles[`commentLevel${props.level}`]]: Boolean(props.level) })}>
<Show when={!!body()}>
<div class={styles.commentContent}>
<Show
when={!props.compact}
fallback={
<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>
</div>
}
>
<div class={styles.commentDetails}>
<div class={styles.commentAuthor}>
<AuthorCard
author={comment()?.createdBy as Author}
hideDescription={true}
hideFollow={true}
isComments={true}
hasLink={true}
/>
2022-11-26 21:27:54 +00:00
</div>
2022-09-09 11:53:35 +00:00
2022-12-16 07:17:50 +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
}}
>
<button class={clsx(styles.commentRatingControl, styles.commentRatingControlUp)} />
<div class={styles.commentRatingValue}>{comment().stat?.rating || 0}</div>
<button class={clsx(styles.commentRatingControl, styles.commentRatingControlDown)} />
</div>
2022-12-16 05:35:53 +00:00
</div>
2022-12-16 07:17:50 +00:00
</Show>
<div
class={styles.commentBody}
contenteditable={props.canEdit}
id={'comment-' + (comment().id || '')}
>
<MD body={body()} />
</div>
2022-09-09 11:53:35 +00:00
2022-12-16 07:17:50 +00:00
<Show when={!props.compact}>
<div class={styles.commentControls}>
<button
class={clsx(styles.commentControl, styles.commentControlReply)}
onClick={() => setIsReplyVisible(!isReplyVisible())}
>
<Icon name="reply" class={styles.icon} />
{t('Reply')}
</button>
<Show when={props.canEdit}>
{/*FIXME implement edit comment modal*/}
{/*<button*/}
{/* class={clsx(styles.commentControl, styles.commentControlEdit)}*/}
{/* onClick={() => showModal('editComment')}*/}
{/*>*/}
{/* <Icon name="edit" class={styles.icon} />*/}
{/* {t('Edit')}*/}
{/*</button>*/}
2022-11-26 21:27:54 +00:00
<button
2022-12-16 07:17:50 +00:00
class={clsx(styles.commentControl, styles.commentControlDelete)}
onClick={() => remove()}
2022-11-26 21:27:54 +00:00
>
2022-12-16 07:17:50 +00:00
<Icon name="delete" class={styles.icon} />
{t('Delete')}
2022-09-09 11:53:35 +00:00
</button>
2022-12-16 07:17:50 +00:00
</Show>
2022-09-09 11:53:35 +00:00
2022-12-16 07:17:50 +00:00
<SharePopup
containerCssClass={stylesHeader.control}
trigger={
<button class={clsx(styles.commentControl, styles.commentControlShare)}>
<Icon name="share" class={styles.icon} />
{t('Share')}
2022-11-26 21:27:54 +00:00
</button>
2022-12-16 07:17:50 +00:00
}
/>
2022-11-26 21:27:54 +00:00
2022-12-16 07:17:50 +00:00
{/*<button*/}
{/* class={clsx(styles.commentControl, styles.commentControlComplain)}*/}
{/* onClick={() => showModal('reportComment')}*/}
{/*>*/}
{/* {t('Report')}*/}
{/*</button>*/}
</div>
2022-12-16 05:35:53 +00:00
2022-12-16 07:17:50 +00:00
<Show when={isReplyVisible()}>
<form class={styles.replyForm}>
<textarea name="reply" id="reply" rows="5" />
<div class={styles.replyFormControls}>
<button class="button button--light" onClick={() => setIsReplyVisible(false)}>
2022-12-18 14:25:41 +00:00
{t('cancel')}
2022-12-16 07:17:50 +00:00
</button>
<button class="button">{t('Send')}</button>
</div>
</form>
2022-11-26 21:27:54 +00:00
</Show>
2022-12-16 07:17:50 +00:00
</Show>
</div>
</Show>
<Show when={props.children}>
<ul>{props.children}</ul>
</Show>
</li>
2022-09-09 11:53:35 +00:00
)
}