webapp/src/components/Inbox/Message.tsx

60 lines
2.0 KiB
TypeScript
Raw Normal View History

import { createSignal, Show } from 'solid-js'
2022-11-21 05:06:53 +00:00
import MarkdownIt from 'markdown-it'
2022-11-16 12:25:37 +00:00
import { clsx } from 'clsx'
import styles from './Message.module.scss'
2022-11-21 05:06:53 +00:00
import DialogAvatar from './DialogAvatar'
import type { Message as MessageType, ChatMember } from '../../graphql/types.gen'
2022-12-17 03:27:00 +00:00
import formattedTime from '../../utils/formatDateTime'
import { Icon } from '../_shared/Icon'
import { MessageActionsPopup } from './MessageActionsPopup'
import QuotedMessage from './QuotedMessage'
2022-11-16 12:25:37 +00:00
type Props = {
content: MessageType
2022-12-17 03:27:00 +00:00
ownId: number
members: ChatMember[]
replyClick?: () => void
replyBody?: string
replyAuthor?: string
2022-11-16 12:25:37 +00:00
}
2022-11-21 05:06:53 +00:00
const md = new MarkdownIt({
2022-12-17 03:27:00 +00:00
linkify: true,
breaks: true
2022-11-21 05:06:53 +00:00
})
2023-02-17 09:21:02 +00:00
export const Message = (props: Props) => {
2022-12-17 03:27:00 +00:00
const isOwn = props.ownId === Number(props.content.author)
const user = props.members?.find((m) => m.id === Number(props.content.author))
const [isPopupVisible, setIsPopupVisible] = createSignal<boolean>(false)
2022-11-16 12:25:37 +00:00
return (
2022-12-17 03:27:00 +00:00
<div class={clsx(styles.Message, isOwn && styles.own)}>
<Show when={!isOwn}>
2022-11-21 05:06:53 +00:00
<div class={styles.author}>
2022-12-17 03:27:00 +00:00
<DialogAvatar size="small" name={user.name} url={user.userpic} />
<div class={styles.name}>{user.name}</div>
2022-11-21 05:06:53 +00:00
</div>
</Show>
2022-12-17 03:27:00 +00:00
<div class={clsx(styles.body, { [styles.popupVisible]: isPopupVisible() })}>
<div class={styles.text}>
<div class={styles.actions}>
<div onClick={props.replyClick}>
<Icon name="chat-reply" class={styles.reply} />
</div>
<MessageActionsPopup
onVisibilityChange={(isVisible) => setIsPopupVisible(isVisible)}
trigger={<Icon name="menu" />}
/>
</div>
<Show when={props.replyBody}>
<QuotedMessage body={props.replyBody} variant="inline" isOwn={isOwn} />
</Show>
<div innerHTML={md.render(props.content.body)} />
</div>
2022-11-21 05:06:53 +00:00
</div>
2022-12-17 03:27:00 +00:00
<div class={styles.time}>{formattedTime(props.content.createdAt * 1000)}</div>
2022-11-16 12:25:37 +00:00
</div>
)
}