2023-11-04 10:14:00 +01:00
|
|
|
import { Entity, MegalodonInterface } from 'megalodon'
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import emojify from '@/utils/emojify'
|
|
|
|
import Body from '../status/Body'
|
|
|
|
import Poll from '../status/Poll'
|
|
|
|
import Card from '../status/Card'
|
|
|
|
import Media from '../status/Media'
|
2024-08-30 16:53:32 +02:00
|
|
|
import { findAccount, findLink, ParsedAccount, accountMatch, findTag } from '@/utils/statusParser'
|
2023-11-04 10:14:00 +01:00
|
|
|
import { FaBarsProgress, FaHouse, FaPenToSquare, FaRetweet, FaStar } from 'react-icons/fa6'
|
2024-07-17 17:15:58 +02:00
|
|
|
import { FormattedMessage, useIntl } from 'react-intl'
|
|
|
|
import { MouseEventHandler, useState } from 'react'
|
|
|
|
import { Avatar, List, ListItem, Card as MaterialCard } from '@material-tailwind/react'
|
2024-01-14 15:30:21 +01:00
|
|
|
import { useRouter } from 'next/router'
|
2024-08-30 16:53:32 +02:00
|
|
|
import { invoke } from '@/utils/invoke'
|
|
|
|
import { Account } from '@/db'
|
2023-11-04 10:14:00 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
notification: Entity.Notification
|
|
|
|
client: MegalodonInterface
|
2024-08-30 16:53:32 +02:00
|
|
|
account: Account
|
2023-11-04 10:14:00 +01:00
|
|
|
onRefresh: (status: Entity.Status) => void
|
2024-03-09 07:00:27 +01:00
|
|
|
openMedia: (media: Array<Entity.Attachment>, index: number) => void
|
2023-11-04 10:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function Reaction(props: Props) {
|
|
|
|
const status = props.notification.status
|
2023-11-26 16:54:29 +01:00
|
|
|
const [spoilered, setSpoilered] = useState(status.spoiler_text.length > 0)
|
2023-11-04 10:14:00 +01:00
|
|
|
const { formatMessage } = useIntl()
|
2024-01-14 15:30:21 +01:00
|
|
|
const router = useRouter()
|
2023-11-04 10:14:00 +01:00
|
|
|
|
|
|
|
const refresh = async () => {
|
|
|
|
const res = await props.client.getStatus(status.id)
|
|
|
|
props.onRefresh(res.data)
|
|
|
|
}
|
|
|
|
|
2024-01-14 15:30:21 +01:00
|
|
|
const openStatus = () => {
|
|
|
|
router.push({ query: { id: router.query.id, timeline: router.query.timeline, status_id: status.id, detail: true } })
|
|
|
|
}
|
|
|
|
|
|
|
|
const openUser = (id: string) => {
|
|
|
|
router.push({ query: { id: router.query.id, timeline: router.query.timeline, user_id: id, detail: true } })
|
|
|
|
}
|
|
|
|
|
2024-08-30 16:53:32 +02:00
|
|
|
const statusClicked: MouseEventHandler<HTMLDivElement> = async e => {
|
|
|
|
const parsedAccount = findAccount(e.target as HTMLElement, 'status-body')
|
|
|
|
if (parsedAccount) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
|
|
|
|
const account = await searchAccount(parsedAccount, status, props.client, props.account.domain)
|
|
|
|
if (account) {
|
|
|
|
router.push({ query: { id: router.query.id, timeline: router.query.timeline, user_id: account.id, detail: true } })
|
|
|
|
} else {
|
|
|
|
console.warn('account not found', parsedAccount)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedTag = findTag(e.target as HTMLElement, 'status-body')
|
|
|
|
if (parsedTag) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
router.push({ query: { id: router.query.id, timeline: router.query.timeline, tag: parsedTag, detail: true } })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = findLink(e.target as HTMLElement, 'status-body')
|
|
|
|
if (url) {
|
|
|
|
invoke('open-browser', url)
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 17:15:58 +02:00
|
|
|
const onContextMenu: MouseEventHandler<HTMLDivElement> = e => {
|
|
|
|
e.preventDefault()
|
|
|
|
hideOthers()
|
|
|
|
const context = document.getElementById(`context-${status.id}`)
|
|
|
|
if (context) {
|
|
|
|
context.style.left = `${e.clientX}px`
|
|
|
|
context.style.top = `${e.clientY}px`
|
|
|
|
context.style.display = 'block'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onClick: MouseEventHandler<HTMLDivElement> = e => {
|
|
|
|
e.preventDefault()
|
|
|
|
hideOthers()
|
|
|
|
}
|
|
|
|
|
|
|
|
const hideOthers = () => {
|
|
|
|
const menu = document.getElementsByClassName('context-menu')
|
|
|
|
for (let i = 0; i < menu.length; i++) {
|
|
|
|
;(menu[i] as HTMLElement).style.display = 'none'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const copyLink = () => {
|
|
|
|
navigator.clipboard.writeText(status.url)
|
|
|
|
}
|
|
|
|
|
2023-11-04 10:14:00 +01:00
|
|
|
return (
|
2024-03-14 17:12:19 +01:00
|
|
|
<div className="border-b border-gray-200 dark:border-gray-800 mr-2 py-1">
|
2023-11-04 10:14:00 +01:00
|
|
|
<div className="flex items-center">
|
|
|
|
<div style={{ width: '56px' }}>{actionIcon(props.notification)}</div>
|
2024-03-14 17:12:19 +01:00
|
|
|
<div
|
|
|
|
className="cursor-pointer text-gray-950 dark:text-gray-300"
|
|
|
|
style={{ width: 'calc(100% - 56px)' }}
|
|
|
|
onClick={() => openUser(props.notification.account.id)}
|
|
|
|
>
|
2023-11-04 10:14:00 +01:00
|
|
|
<span
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: emojify(
|
|
|
|
formatMessage(
|
|
|
|
{
|
|
|
|
id: actionId(props.notification)
|
|
|
|
},
|
|
|
|
{ user: props.notification.account.username }
|
|
|
|
),
|
|
|
|
props.notification.account.emojis
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex">
|
2024-01-14 15:30:21 +01:00
|
|
|
<div className="p-2 cursor-pointer" style={{ width: '56px' }}>
|
|
|
|
<Avatar
|
|
|
|
src={status.account.avatar}
|
|
|
|
onClick={() => openUser(status.account.id)}
|
|
|
|
variant="rounded"
|
|
|
|
style={{ width: '40px', height: '40px' }}
|
2024-01-30 16:51:36 +01:00
|
|
|
alt={formatMessage({ id: 'timeline.status.avatar' }, { user: status.account.username })}
|
2024-01-14 15:30:21 +01:00
|
|
|
/>
|
2023-11-04 10:14:00 +01:00
|
|
|
</div>
|
2024-07-17 17:15:58 +02:00
|
|
|
<div
|
|
|
|
className="text-gray-600 dark:text-gray-500 break-all overflow-hidden"
|
|
|
|
style={{ width: 'calc(100% - 56px)' }}
|
|
|
|
onContextMenu={onContextMenu}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
2023-11-04 10:14:00 +01:00
|
|
|
<div className="flex justify-between">
|
2024-01-14 15:30:21 +01:00
|
|
|
<div className="flex cursor-pointer" onClick={() => openUser(status.account.id)}>
|
2023-11-04 10:14:00 +01:00
|
|
|
<span
|
2024-03-14 17:12:19 +01:00
|
|
|
className="text-ellipsis break-all overflow-hidden"
|
2023-11-04 10:14:00 +01:00
|
|
|
dangerouslySetInnerHTML={{ __html: emojify(status.account.display_name, status.account.emojis) }}
|
|
|
|
></span>
|
2024-03-14 17:12:19 +01:00
|
|
|
<span className="text-ellipsis break-all overflow-hidden">@{status.account.acct}</span>
|
2023-11-04 10:14:00 +01:00
|
|
|
</div>
|
2024-03-14 17:12:19 +01:00
|
|
|
<div className="text-right cursor-pointer" onClick={openStatus}>
|
2023-11-04 10:14:00 +01:00
|
|
|
<time dateTime={status.created_at}>{dayjs(status.created_at).format('YYYY-MM-DD HH:mm:ss')}</time>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-30 16:53:32 +02:00
|
|
|
<div className="status-body">
|
|
|
|
<Body
|
|
|
|
status={status}
|
|
|
|
className="text-gray-600 dark:text-gray-500"
|
|
|
|
spoilered={spoilered}
|
|
|
|
setSpoilered={setSpoilered}
|
|
|
|
onClick={statusClicked}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-11-26 16:54:29 +01:00
|
|
|
{!spoilered && (
|
|
|
|
<>
|
|
|
|
{status.poll && <Poll poll={status.poll} onRefresh={refresh} client={props.client} className="text-gray-600" />}
|
|
|
|
{status.card && <Card card={status.card} />}
|
2023-12-02 11:26:45 +01:00
|
|
|
<Media media={status.media_attachments} sensitive={status.sensitive} openMedia={props.openMedia} />
|
2023-11-26 16:54:29 +01:00
|
|
|
</>
|
|
|
|
)}
|
2024-07-17 17:15:58 +02:00
|
|
|
<div className="fixed hidden context-menu z-50" id={`context-${status.id}`}>
|
|
|
|
<MaterialCard className="rounded-md bg-white dark:bg-gray-800">
|
|
|
|
<List className="my-2 p-0">
|
|
|
|
<ListItem className="ground rounded-none py-1.5 px-3 text-sm" onClick={copyLink}>
|
|
|
|
<FormattedMessage id="timeline.status.context_menu.copy_link" />
|
|
|
|
</ListItem>
|
|
|
|
<ListItem className="ground rounded-none py-1.5 px-3 text-sm" onClick={openStatus}>
|
|
|
|
<FormattedMessage id="timeline.status.context_menu.open_detail" />
|
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
</MaterialCard>
|
|
|
|
</div>
|
2023-11-04 10:14:00 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionIcon = (notification: Entity.Notification) => {
|
|
|
|
switch (notification.type) {
|
|
|
|
case 'favourite': {
|
|
|
|
return <FaStar className="text-orange-500 w-4 mr-2 ml-auto" />
|
|
|
|
}
|
|
|
|
case 'reblog':
|
|
|
|
case 'quote': {
|
|
|
|
return <FaRetweet className="text-blue-600 w-4 mr-2 ml-auto" />
|
|
|
|
}
|
|
|
|
case 'poll_expired':
|
|
|
|
case 'poll_vote': {
|
|
|
|
return <FaBarsProgress className="text-blue-600 w-4 mr-2 ml-auto" />
|
|
|
|
}
|
|
|
|
case 'status': {
|
|
|
|
return <FaHouse className="text-blue-600 w-4 mr-2 ml-auto" />
|
|
|
|
}
|
|
|
|
case 'update': {
|
|
|
|
return <FaPenToSquare className="text-blue-600 w-4 mr-2 ml-auto" />
|
|
|
|
}
|
2024-02-15 16:55:02 +01:00
|
|
|
case 'reaction':
|
2023-11-04 10:14:00 +01:00
|
|
|
case 'emoji_reaction': {
|
2024-02-15 16:55:02 +01:00
|
|
|
if (notification.reaction) {
|
|
|
|
if (notification.reaction.url) {
|
|
|
|
return (
|
|
|
|
<div className="w-5 mr-2 ml-auto">
|
|
|
|
<img src={notification.reaction.url} style={{ height: '18px' }} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div className="w-5 mr-2 ml-auto">
|
|
|
|
<span dangerouslySetInnerHTML={{ __html: notification.reaction.name }} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2023-11-04 10:14:00 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionId = (notification: Entity.Notification) => {
|
|
|
|
switch (notification.type) {
|
|
|
|
case 'favourite':
|
|
|
|
return 'notification.favourite.body'
|
|
|
|
case 'reblog':
|
|
|
|
return 'notification.reblog.body'
|
|
|
|
case 'quote':
|
|
|
|
return 'notification.quote.body'
|
|
|
|
case 'poll_expired':
|
|
|
|
return 'notification.poll_expired.body'
|
|
|
|
case 'poll_vote':
|
|
|
|
return 'notification.poll.vote'
|
|
|
|
case 'status':
|
|
|
|
return 'notification.status.body'
|
|
|
|
case 'update':
|
|
|
|
return 'notification.update.body'
|
2024-02-15 16:55:02 +01:00
|
|
|
case 'reaction':
|
2024-01-29 16:10:44 +01:00
|
|
|
case 'emoji_reaction':
|
2023-11-04 10:14:00 +01:00
|
|
|
return 'notification.emoji_reaction.body'
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
2024-08-30 16:53:32 +02:00
|
|
|
|
|
|
|
async function searchAccount(account: ParsedAccount, status: Entity.Status, client: MegalodonInterface, domain: string) {
|
|
|
|
if (status.in_reply_to_account_id) {
|
|
|
|
const res = await client.getAccount(status.in_reply_to_account_id)
|
|
|
|
if (res.status === 200) {
|
|
|
|
const user = accountMatch([res.data], account, domain)
|
|
|
|
if (user) return user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.in_reply_to_id) {
|
|
|
|
const res = await client.getStatusContext(status.id)
|
|
|
|
if (res.status === 200) {
|
|
|
|
const accounts: Array<Entity.Account> = res.data.ancestors.map(s => s.account).concat(res.data.descendants.map(s => s.account))
|
|
|
|
const user = accountMatch(accounts, account, domain)
|
|
|
|
if (user) return user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const res = await client.searchAccount(account.url, { resolve: true })
|
|
|
|
if (res.data.length === 0) return null
|
|
|
|
const user = accountMatch(res.data, account, domain)
|
|
|
|
if (user) return user
|
|
|
|
return null
|
|
|
|
}
|