2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2023-12-21 08:18:15 -08:00
|
|
|
import parse, { Element, type HTMLReactParserOptions, domToReact, type DOMNode } from 'html-react-parser';
|
2023-01-12 08:57:39 -08:00
|
|
|
import React, { useState, useRef, useLayoutEffect, useMemo } from 'react';
|
2022-04-15 14:59:42 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2024-07-25 10:53:48 -07:00
|
|
|
import { Link } from 'react-router-dom';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
import Icon from 'soapbox/components/icon';
|
2022-11-15 12:46:23 -08:00
|
|
|
import { onlyEmoji as isOnlyEmoji } from 'soapbox/utils/rich-content';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2023-10-10 18:02:22 -07:00
|
|
|
import { getTextDirection } from '../utils/rtl';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2023-10-13 20:23:18 -07:00
|
|
|
import HashtagLink from './hashtag-link';
|
2024-07-25 10:53:48 -07:00
|
|
|
import HoverRefWrapper from './hover-ref-wrapper';
|
2022-11-19 16:13:27 -08:00
|
|
|
import Markup from './markup';
|
2022-06-15 12:59:17 -07:00
|
|
|
import Poll from './polls/poll';
|
|
|
|
|
2023-01-27 10:36:49 -08:00
|
|
|
import type { Sizes } from 'soapbox/components/ui/text/text';
|
2024-08-18 06:35:52 -07:00
|
|
|
import type { Status } from 'soapbox/normalizers';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
const MAX_HEIGHT = 642; // 20px * 32 (+ 2px padding at the top)
|
|
|
|
const BIG_EMOJI_LIMIT = 10;
|
|
|
|
|
2022-04-15 15:41:53 -07:00
|
|
|
interface IReadMoreButton {
|
2023-10-02 11:54:02 -07:00
|
|
|
onClick: React.MouseEventHandler;
|
2022-04-15 15:41:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Button to expand a truncated status (due to too much content) */
|
|
|
|
const ReadMoreButton: React.FC<IReadMoreButton> = ({ onClick }) => (
|
2023-02-01 14:13:42 -08:00
|
|
|
<button className='flex items-center border-0 bg-transparent p-0 pt-2 text-gray-900 hover:underline active:underline dark:text-gray-300' onClick={onClick}>
|
2022-11-19 14:08:58 -08:00
|
|
|
<FormattedMessage id='status.read_more' defaultMessage='Read more' />
|
2024-04-03 04:28:30 -07:00
|
|
|
<Icon className='inline-block h-5 w-5' src={require('@tabler/icons/outline/chevron-right.svg')} />
|
2022-11-19 14:08:58 -08:00
|
|
|
</button>
|
2022-04-15 15:41:53 -07:00
|
|
|
);
|
|
|
|
|
2022-04-15 14:59:42 -07:00
|
|
|
interface IStatusContent {
|
2023-10-02 11:54:02 -07:00
|
|
|
status: Status;
|
|
|
|
onClick?: () => void;
|
|
|
|
collapsable?: boolean;
|
|
|
|
translatable?: boolean;
|
|
|
|
textSize?: Sizes;
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
2022-04-15 15:41:53 -07:00
|
|
|
/** Renders the text content of a status */
|
2024-05-13 10:00:42 -07:00
|
|
|
const StatusContent: React.FC<IStatusContent> = React.memo(({
|
2023-01-27 10:36:49 -08:00
|
|
|
status,
|
|
|
|
onClick,
|
|
|
|
collapsable = false,
|
|
|
|
translatable,
|
2023-01-30 11:38:29 -08:00
|
|
|
textSize = 'md',
|
2023-01-27 10:36:49 -08:00
|
|
|
}) => {
|
2022-04-15 14:59:42 -07:00
|
|
|
const [collapsed, setCollapsed] = useState(false);
|
|
|
|
const [onlyEmoji, setOnlyEmoji] = useState(false);
|
|
|
|
|
|
|
|
const node = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const maybeSetCollapsed = (): void => {
|
|
|
|
if (!node.current) return;
|
|
|
|
|
2023-01-15 14:05:04 -08:00
|
|
|
if (collapsable && onClick && !collapsed) {
|
2022-04-15 14:59:42 -07:00
|
|
|
if (node.current.clientHeight > MAX_HEIGHT) {
|
|
|
|
setCollapsed(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const maybeSetOnlyEmoji = (): void => {
|
|
|
|
if (!node.current) return;
|
|
|
|
const only = isOnlyEmoji(node.current, BIG_EMOJI_LIMIT, true);
|
|
|
|
|
|
|
|
if (only !== onlyEmoji) {
|
|
|
|
setOnlyEmoji(only);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-12 08:57:39 -08:00
|
|
|
useLayoutEffect(() => {
|
2022-04-15 14:59:42 -07:00
|
|
|
maybeSetCollapsed();
|
|
|
|
maybeSetOnlyEmoji();
|
|
|
|
});
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const parsedHtml = useMemo(
|
2024-05-27 15:11:28 -07:00
|
|
|
(): string => translatable && status.translation
|
2024-08-11 01:48:58 -07:00
|
|
|
? status.translation.content!
|
2024-05-27 15:11:28 -07:00
|
|
|
: (status.contentMapHtml && status.currentLanguage)
|
2024-08-11 01:48:58 -07:00
|
|
|
? (status.contentMapHtml[status.currentLanguage] || status.contentHtml)
|
2024-05-27 15:11:28 -07:00
|
|
|
: status.contentHtml,
|
|
|
|
[status.contentHtml, status.translation, status.currentLanguage],
|
2024-05-12 16:18:04 -07:00
|
|
|
);
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
if (status.content.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-30 11:15:37 -07:00
|
|
|
const withSpoiler = status.spoiler_text.length > 0;
|
|
|
|
|
|
|
|
const baseClassName = 'text-gray-900 dark:text-gray-100 break-words text-ellipsis overflow-hidden relative focus:outline-none';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2023-10-13 20:07:57 -07:00
|
|
|
const options: HTMLReactParserOptions = {
|
|
|
|
replace(domNode) {
|
|
|
|
if (domNode instanceof Element && ['script', 'iframe'].includes(domNode.name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (domNode instanceof Element && domNode.name === 'a') {
|
|
|
|
const classes = domNode.attribs.class?.split(' ');
|
|
|
|
|
|
|
|
if (classes?.includes('mention')) {
|
|
|
|
const mention = status.mentions.find(({ url }) => domNode.attribs.href === url);
|
|
|
|
if (mention) {
|
2024-07-25 10:53:48 -07:00
|
|
|
return (
|
|
|
|
<HoverRefWrapper accountId={mention.id} inline>
|
|
|
|
<Link
|
|
|
|
to={`/@${mention.acct}`}
|
|
|
|
className='text-primary-600 hover:underline dark:text-accent-blue'
|
|
|
|
dir='ltr'
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
@{mention.username}
|
|
|
|
</Link>
|
|
|
|
</HoverRefWrapper>
|
|
|
|
);
|
2023-10-13 20:07:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (classes?.includes('hashtag')) {
|
2023-12-21 08:18:15 -08:00
|
|
|
const child = domToReact(domNode.children as DOMNode[]);
|
2023-10-13 20:23:18 -07:00
|
|
|
const hashtag = typeof child === 'string' ? child.replace(/^#/, '') : undefined;
|
2023-10-13 20:07:57 -07:00
|
|
|
if (hashtag) {
|
2023-10-13 20:23:18 -07:00
|
|
|
return <HashtagLink hashtag={hashtag} />;
|
2023-10-13 20:07:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
|
|
|
|
<a
|
|
|
|
{...domNode.attribs}
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
rel='nofollow noopener'
|
|
|
|
target='_blank'
|
|
|
|
title={domNode.attribs.href}
|
|
|
|
>
|
2023-12-21 08:18:15 -08:00
|
|
|
{domToReact(domNode.children as DOMNode[], options)}
|
2023-10-13 20:07:57 -07:00
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const content = parse(parsedHtml, options);
|
|
|
|
|
2023-10-10 18:02:22 -07:00
|
|
|
const direction = getTextDirection(status.search_index);
|
2023-02-06 10:01:03 -08:00
|
|
|
const className = clsx(baseClassName, {
|
2022-09-30 10:59:19 -07:00
|
|
|
'cursor-pointer': onClick,
|
2022-09-30 11:15:37 -07:00
|
|
|
'whitespace-normal': withSpoiler,
|
2022-09-30 10:59:19 -07:00
|
|
|
'max-h-[300px]': collapsed,
|
|
|
|
'leading-normal big-emoji': onlyEmoji,
|
2022-04-15 14:59:42 -07:00
|
|
|
});
|
|
|
|
|
2022-10-31 13:18:40 -07:00
|
|
|
if (onClick) {
|
2022-04-15 14:59:42 -07:00
|
|
|
const output = [
|
2022-11-19 16:13:27 -08:00
|
|
|
<Markup
|
2022-04-15 14:59:42 -07:00
|
|
|
ref={node}
|
|
|
|
tabIndex={0}
|
|
|
|
key='content'
|
|
|
|
className={className}
|
2022-11-19 16:13:27 -08:00
|
|
|
direction={direction}
|
2022-04-15 14:59:42 -07:00
|
|
|
lang={status.language || undefined}
|
2023-01-30 11:38:29 -08:00
|
|
|
size={textSize}
|
2023-10-13 20:07:57 -07:00
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</Markup>,
|
2022-04-15 14:59:42 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
if (collapsed) {
|
2022-04-15 15:41:53 -07:00
|
|
|
output.push(<ReadMoreButton onClick={onClick} key='read-more' />);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
let hasPoll = false;
|
|
|
|
|
|
|
|
if (status.poll && typeof status.poll === 'string') {
|
|
|
|
hasPoll = true;
|
2024-05-27 15:11:28 -07:00
|
|
|
output.push(<Poll id={status.poll} key='poll' status={status} />);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
2023-02-06 10:01:03 -08:00
|
|
|
return <div className={clsx({ 'bg-gray-100 dark:bg-primary-800 rounded-md p-4': hasPoll })}>{output}</div>;
|
2022-04-15 14:59:42 -07:00
|
|
|
} else {
|
|
|
|
const output = [
|
2022-11-19 16:13:27 -08:00
|
|
|
<Markup
|
2022-04-15 14:59:42 -07:00
|
|
|
ref={node}
|
|
|
|
tabIndex={0}
|
|
|
|
key='content'
|
2023-02-06 10:01:03 -08:00
|
|
|
className={clsx(baseClassName, {
|
2022-09-30 10:59:19 -07:00
|
|
|
'leading-normal big-emoji': onlyEmoji,
|
2022-04-15 14:59:42 -07:00
|
|
|
})}
|
2022-11-19 16:13:27 -08:00
|
|
|
direction={direction}
|
2022-04-15 14:59:42 -07:00
|
|
|
lang={status.language || undefined}
|
2023-01-30 11:38:29 -08:00
|
|
|
size={textSize}
|
2023-10-13 20:07:57 -07:00
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</Markup>,
|
2022-04-15 14:59:42 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
if (status.poll && typeof status.poll === 'string') {
|
2024-05-27 15:11:28 -07:00
|
|
|
output.push(<Poll id={status.poll} key='poll' status={status} />);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return <>{output}</>;
|
|
|
|
}
|
2024-05-13 10:00:42 -07:00
|
|
|
});
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
export { StatusContent as default };
|