webapp/src/components/Article/Comment.tsx

148 lines
5.4 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'
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-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-11-26 21:27:54 +00:00
<div class={clsx(styles.comment, { [styles[`commentLevel${props.level}`]]: Boolean(props.level) })}>
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}
contenteditable={props.canEdit}
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
class={clsx(styles.commentControl, styles.commentControlReply)}
onClick={() => setIsReplyVisible(!isReplyVisible())}
>
<Icon name="reply" class={styles.icon} />
2022-09-09 11:53:35 +00:00
{t('Reply')}
</button>
<Show when={props.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()}>
<form class={styles.replyForm}>
2022-12-07 20:10:26 +00:00
<textarea name="reply" id="reply" rows="5" />
2022-11-26 21:27:54 +00:00
<div class={styles.replyFormControls}>
<button class="button button--light" onClick={() => setIsReplyVisible(false)}>
2022-11-27 17:02:04 +00:00
{t('Cancel')}
2022-11-26 21:27:54 +00:00
</button>
2022-11-27 17:02:04 +00:00
<button class="button">{t('Send')}</button>
2022-11-26 21:27:54 +00:00
</div>
</form>
</Show>
2022-09-09 11:53:35 +00:00
</Show>
</div>
</Show>
</div>
)
}