2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
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';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
import { isRtl } from '../rtl';
|
|
|
|
|
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';
|
2022-04-15 14:59:42 -07:00
|
|
|
import type { Status, Mention } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
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-02-15 13:26:27 -08: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' />
|
2023-01-09 15:11:39 -08:00
|
|
|
<Icon className='inline-block h-5 w-5' src={require('@tabler/icons/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-02-15 13:26:27 -08: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 */
|
2023-01-27 10:36:49 -08:00
|
|
|
const StatusContent: React.FC<IStatusContent> = ({
|
|
|
|
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 history = useHistory();
|
|
|
|
|
|
|
|
const [collapsed, setCollapsed] = useState(false);
|
|
|
|
const [onlyEmoji, setOnlyEmoji] = useState(false);
|
|
|
|
|
|
|
|
const node = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const onMentionClick = (mention: Mention, e: MouseEvent) => {
|
|
|
|
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
|
|
|
e.preventDefault();
|
2022-04-16 09:53:00 -07:00
|
|
|
e.stopPropagation();
|
2022-04-15 14:59:42 -07:00
|
|
|
history.push(`/@${mention.acct}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onHashtagClick = (hashtag: string, e: MouseEvent) => {
|
|
|
|
hashtag = hashtag.replace(/^#/, '').toLowerCase();
|
|
|
|
|
|
|
|
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
|
|
|
e.preventDefault();
|
2022-04-16 09:53:00 -07:00
|
|
|
e.stopPropagation();
|
2022-04-15 14:59:42 -07:00
|
|
|
history.push(`/tags/${hashtag}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-16 09:53:00 -07:00
|
|
|
/** For regular links, just stop propogation */
|
|
|
|
const onLinkClick = (e: MouseEvent) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
2022-04-15 14:59:42 -07:00
|
|
|
const updateStatusLinks = () => {
|
|
|
|
if (!node.current) return;
|
|
|
|
|
|
|
|
const links = node.current.querySelectorAll('a');
|
|
|
|
|
2022-04-15 15:51:12 -07:00
|
|
|
links.forEach(link => {
|
|
|
|
// Skip already processed
|
|
|
|
if (link.classList.contains('status-link')) return;
|
|
|
|
|
|
|
|
// Add attributes
|
2022-04-15 14:59:42 -07:00
|
|
|
link.classList.add('status-link');
|
|
|
|
link.setAttribute('rel', 'nofollow noopener');
|
|
|
|
link.setAttribute('target', '_blank');
|
|
|
|
|
|
|
|
const mention = status.mentions.find(mention => link.href === `${mention.url}`);
|
|
|
|
|
2022-04-15 15:51:12 -07:00
|
|
|
// Add event listeners on mentions and hashtags
|
2022-04-15 14:59:42 -07:00
|
|
|
if (mention) {
|
|
|
|
link.addEventListener('click', onMentionClick.bind(link, mention), false);
|
|
|
|
link.setAttribute('title', mention.acct);
|
2022-04-15 15:51:12 -07:00
|
|
|
} else if (link.textContent?.charAt(0) === '#' || (link.previousSibling?.textContent?.charAt(link.previousSibling.textContent.length - 1) === '#')) {
|
2022-04-15 14:59:42 -07:00
|
|
|
link.addEventListener('click', onHashtagClick.bind(link, link.text), false);
|
|
|
|
} else {
|
|
|
|
link.setAttribute('title', link.href);
|
2022-04-16 09:53:00 -07:00
|
|
|
link.addEventListener('click', onLinkClick.bind(link), false);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
2022-04-15 15:51:12 -07:00
|
|
|
});
|
2022-04-15 14:59:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
updateStatusLinks();
|
|
|
|
});
|
|
|
|
|
2022-06-25 10:00:00 -07:00
|
|
|
const parsedHtml = useMemo((): string => {
|
2023-05-28 12:45:22 -07:00
|
|
|
return translatable && status.translation ? status.translation.get('content')! : status.contentHtml;
|
2022-10-27 10:46:03 -07:00
|
|
|
}, [status.contentHtml, status.translation]);
|
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
|
|
|
|
2022-06-25 10:00:00 -07:00
|
|
|
const content = { __html: parsedHtml };
|
2022-11-19 16:13:27 -08:00
|
|
|
const direction = isRtl(status.search_index) ? 'rtl' : 'ltr';
|
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
|
|
|
dangerouslySetInnerHTML={content}
|
|
|
|
lang={status.language || undefined}
|
2023-01-30 11:38:29 -08:00
|
|
|
size={textSize}
|
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
|
|
|
}
|
|
|
|
|
2022-06-14 13:14:14 -07:00
|
|
|
const hasPoll = status.poll && typeof status.poll === 'string';
|
|
|
|
if (hasPoll) {
|
2022-04-15 14:59:42 -07:00
|
|
|
output.push(<Poll id={status.poll} key='poll' status={status.url} />);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
dangerouslySetInnerHTML={content}
|
|
|
|
lang={status.language || undefined}
|
2023-01-30 11:38:29 -08:00
|
|
|
size={textSize}
|
2022-04-15 14:59:42 -07:00
|
|
|
/>,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (status.poll && typeof status.poll === 'string') {
|
|
|
|
output.push(<Poll id={status.poll} key='poll' status={status.url} />);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <>{output}</>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-25 10:00:00 -07:00
|
|
|
export default React.memo(StatusContent);
|