2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2024-09-05 16:06:25 -07:00
|
|
|
import React, { useState, useRef, useLayoutEffect, useMemo, useEffect } from 'react';
|
2024-09-01 14:54:28 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2024-09-05 15:47:26 -07:00
|
|
|
import { collapseStatusSpoiler, expandStatusSpoiler } from 'pl-fe/actions/statuses';
|
2024-08-28 04:41:08 -07:00
|
|
|
import Icon from 'pl-fe/components/icon';
|
2024-08-29 15:09:46 -07:00
|
|
|
import { Button, Stack, Text } from 'pl-fe/components/ui';
|
2024-08-28 04:41:08 -07:00
|
|
|
import { useAppDispatch, useSettings } from 'pl-fe/hooks';
|
|
|
|
import { onlyEmoji as isOnlyEmoji } from 'pl-fe/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
|
|
|
|
2022-11-19 16:13:27 -08:00
|
|
|
import Markup from './markup';
|
2024-10-03 03:09:35 -07:00
|
|
|
import { ParsedContent } from './parsed-content';
|
2022-06-15 12:59:17 -07:00
|
|
|
import Poll from './polls/poll';
|
|
|
|
|
2024-10-18 15:53:07 -07:00
|
|
|
import type { Sizes } from 'pl-fe/components/ui/text';
|
2024-08-28 04:41:08 -07:00
|
|
|
import type { MinifiedStatus } from 'pl-fe/reducers/statuses';
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
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;
|
2024-08-31 10:50:45 -07:00
|
|
|
quote?: boolean;
|
2024-09-11 09:01:14 -07:00
|
|
|
poll?: boolean;
|
2022-04-15 15:41:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Button to expand a truncated status (due to too much content) */
|
2024-09-11 09:01:14 -07:00
|
|
|
const ReadMoreButton: React.FC<IReadMoreButton> = ({ onClick, quote, poll }) => (
|
2024-09-01 08:40:28 -07:00
|
|
|
<div className='relative -mt-4'>
|
2024-08-31 10:50:45 -07:00
|
|
|
<div
|
2024-09-12 11:42:38 -07:00
|
|
|
className={clsx('absolute -top-16 h-16 w-full bg-gradient-to-b from-transparent', {
|
|
|
|
'to-white black:to-black dark:to-primary-900': !poll,
|
2024-09-11 09:01:14 -07:00
|
|
|
'to-gray-100 dark:to-primary-800': poll,
|
2024-09-12 11:42:38 -07:00
|
|
|
'group-hover:to-gray-100 black:group-hover:to-gray-800 dark:group-hover:to-gray-800': quote,
|
2024-08-31 10:50:45 -07:00
|
|
|
})}
|
|
|
|
/>
|
2024-08-23 15:27:40 -07: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}>
|
|
|
|
<FormattedMessage id='status.read_more' defaultMessage='Read more' />
|
2024-09-21 15:09:45 -07:00
|
|
|
<Icon className='inline-block size-5' src={require('@tabler/icons/outline/chevron-right.svg')} />
|
2024-08-23 15:27:40 -07:00
|
|
|
</button>
|
|
|
|
</div>
|
2022-04-15 15:41:53 -07:00
|
|
|
);
|
|
|
|
|
2022-04-15 14:59:42 -07:00
|
|
|
interface IStatusContent {
|
2024-08-18 08:50:56 -07:00
|
|
|
status: MinifiedStatus;
|
2023-10-02 11:54:02 -07:00
|
|
|
onClick?: () => void;
|
|
|
|
collapsable?: boolean;
|
|
|
|
translatable?: boolean;
|
|
|
|
textSize?: Sizes;
|
2024-08-31 10:50:45 -07:00
|
|
|
quote?: boolean;
|
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',
|
2024-08-31 10:50:45 -07:00
|
|
|
quote = false,
|
2023-01-27 10:36:49 -08:00
|
|
|
}) => {
|
2024-08-23 15:27:40 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const { displaySpoilers } = useSettings();
|
|
|
|
|
2022-04-15 14:59:42 -07:00
|
|
|
const [collapsed, setCollapsed] = useState(false);
|
|
|
|
const [onlyEmoji, setOnlyEmoji] = useState(false);
|
2024-09-05 16:06:25 -07:00
|
|
|
const [lineClamp, setLineClamp] = useState(true);
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
const node = useRef<HTMLDivElement>(null);
|
2024-09-05 16:06:25 -07:00
|
|
|
const spoilerNode = useRef<HTMLSpanElement>(null);
|
2022-04-15 14:59:42 -07:00
|
|
|
|
|
|
|
const maybeSetCollapsed = (): void => {
|
|
|
|
if (!node.current) return;
|
|
|
|
|
2024-08-31 09:30:25 -07:00
|
|
|
if (collapsable && !collapsed) {
|
2024-09-22 05:50:56 -07:00
|
|
|
// 20px * x lines (+ 2px padding at the top)
|
|
|
|
if (node.current.clientHeight > (quote ? 202 : 282)) {
|
2022-04-15 14:59:42 -07:00
|
|
|
setCollapsed(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const maybeSetOnlyEmoji = (): void => {
|
|
|
|
if (!node.current) return;
|
|
|
|
const only = isOnlyEmoji(node.current, BIG_EMOJI_LIMIT, true);
|
|
|
|
|
|
|
|
if (only !== onlyEmoji) {
|
|
|
|
setOnlyEmoji(only);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-23 15:27:40 -07:00
|
|
|
const toggleExpanded: React.MouseEventHandler<HTMLButtonElement> = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2024-09-05 15:47:26 -07:00
|
|
|
if (expanded) dispatch(collapseStatusSpoiler(status.id));
|
|
|
|
else dispatch(expandStatusSpoiler(status.id));
|
2024-08-23 15:27:40 -07:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2024-09-05 16:06:25 -07:00
|
|
|
useEffect(() => {
|
|
|
|
setLineClamp(!spoilerNode.current || spoilerNode.current.clientHeight >= 96);
|
|
|
|
}, [spoilerNode.current]);
|
|
|
|
|
2024-09-02 03:35:47 -07:00
|
|
|
const withSpoiler = status.spoiler_text.length > 0;
|
2023-10-13 20:07:57 -07:00
|
|
|
|
2024-08-24 03:41:08 -07:00
|
|
|
const spoilerText = status.spoilerMapHtml && status.currentLanguage
|
|
|
|
? status.spoilerMapHtml[status.currentLanguage] || status.spoilerHtml
|
|
|
|
: status.spoilerHtml;
|
|
|
|
|
2023-10-10 18:02:22 -07:00
|
|
|
const direction = getTextDirection(status.search_index);
|
2024-10-02 08:25:39 -07:00
|
|
|
const className = clsx('relative text-ellipsis break-words text-gray-900 focus:outline-none dark:text-gray-100', {
|
2022-09-30 10:59:19 -07:00
|
|
|
'cursor-pointer': onClick,
|
2024-10-02 08:25:39 -07:00
|
|
|
'overflow-hidden': collapsed,
|
2024-09-22 05:50:56 -07:00
|
|
|
'max-h-[200px]': collapsed && !quote,
|
|
|
|
'max-h-[120px]': collapsed && quote,
|
2022-09-30 10:59:19 -07:00
|
|
|
'leading-normal big-emoji': onlyEmoji,
|
2022-04-15 14:59:42 -07:00
|
|
|
});
|
|
|
|
|
2024-08-23 15:27:40 -07:00
|
|
|
const expandable = !displaySpoilers;
|
|
|
|
const expanded = !withSpoiler || status.expanded || false;
|
|
|
|
|
2024-08-23 14:50:00 -07:00
|
|
|
const output = [];
|
|
|
|
|
|
|
|
if (spoilerText) {
|
|
|
|
output.push(
|
2024-08-25 11:17:52 -07:00
|
|
|
<Text key='spoiler' size='2xl' weight='medium'>
|
2024-09-01 14:55:38 -07:00
|
|
|
<span
|
2024-09-05 16:06:25 -07:00
|
|
|
className={clsx({ 'line-clamp-3': !expanded && lineClamp })}
|
2024-09-01 14:55:38 -07:00
|
|
|
dangerouslySetInnerHTML={{ __html: spoilerText }}
|
2024-09-05 16:06:25 -07:00
|
|
|
ref={spoilerNode}
|
2024-09-01 14:55:38 -07:00
|
|
|
/>
|
2024-10-03 03:09:35 -07:00
|
|
|
{status.content && expandable && (
|
2024-08-23 15:27:40 -07:00
|
|
|
<Button
|
|
|
|
className='ml-2 align-middle'
|
|
|
|
type='button'
|
|
|
|
theme='muted'
|
|
|
|
size='xs'
|
|
|
|
onClick={toggleExpanded}
|
2024-08-28 05:48:35 -07:00
|
|
|
icon={expanded ? require('@tabler/icons/outline/chevron-up.svg') : require('@tabler/icons/outline/chevron-down.svg')}
|
2024-08-23 15:27:40 -07:00
|
|
|
>
|
2024-09-01 14:54:28 -07:00
|
|
|
{expanded
|
|
|
|
? <FormattedMessage id='status.spoiler.collapse' defaultMessage='Collapse' />
|
|
|
|
: <FormattedMessage id='status.spoiler.expand' defaultMessage='Expand' />}
|
2024-08-23 15:27:40 -07:00
|
|
|
</Button>
|
|
|
|
)}
|
2024-08-23 14:50:00 -07:00
|
|
|
</Text>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-23 15:27:40 -07:00
|
|
|
if (expandable && !expanded) return <>{output}</>;
|
|
|
|
|
2022-10-31 13:18:40 -07:00
|
|
|
if (onClick) {
|
2024-10-03 03:09:35 -07:00
|
|
|
if (status.content) {
|
2024-09-14 08:57:50 -07:00
|
|
|
output.push(
|
|
|
|
<Markup
|
|
|
|
ref={node}
|
|
|
|
tabIndex={0}
|
|
|
|
key='content'
|
|
|
|
className={className}
|
|
|
|
direction={direction}
|
|
|
|
lang={status.language || undefined}
|
|
|
|
size={textSize}
|
|
|
|
>
|
2024-10-03 03:39:36 -07:00
|
|
|
<ParsedContent html={parsedHtml} mentions={status.mentions} hasQuote={!!status.quote_id} />
|
2024-09-14 08:57:50 -07:00
|
|
|
</Markup>,
|
|
|
|
);
|
|
|
|
}
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2024-09-11 09:01:14 -07:00
|
|
|
const hasPoll = !!status.poll_id;
|
|
|
|
|
2022-04-15 14:59:42 -07:00
|
|
|
if (collapsed) {
|
2024-09-11 09:01:14 -07:00
|
|
|
output.push(<ReadMoreButton onClick={onClick} key='read-more' quote={quote} poll={hasPoll} />);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
if (status.poll_id) {
|
|
|
|
output.push(<Poll id={status.poll_id} key='poll' status={status} />);
|
2022-04-15 14:59:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-29 15:09:46 -07:00
|
|
|
return <Stack space={4} className={clsx({ 'bg-gray-100 dark:bg-primary-800 rounded-md p-4': hasPoll })}>{output}</Stack>;
|
2022-04-15 14:59:42 -07:00
|
|
|
} else {
|
2024-10-03 03:09:35 -07:00
|
|
|
if (status.content) {
|
2024-09-14 08:57:50 -07:00
|
|
|
output.push(
|
|
|
|
<Markup
|
|
|
|
ref={node}
|
|
|
|
tabIndex={0}
|
|
|
|
key='content'
|
|
|
|
className={className}
|
|
|
|
direction={direction}
|
|
|
|
lang={status.language || undefined}
|
|
|
|
size={textSize}
|
|
|
|
>
|
2024-10-03 03:39:36 -07:00
|
|
|
<ParsedContent html={parsedHtml} mentions={status.mentions} hasQuote={!!status.quote_id} />
|
2024-09-14 08:57:50 -07:00
|
|
|
</Markup>,
|
|
|
|
);
|
|
|
|
}
|
2022-04-15 14:59:42 -07:00
|
|
|
|
2024-08-31 10:50:45 -07:00
|
|
|
if (collapsed) {
|
|
|
|
output.push(<ReadMoreButton onClick={() => {}} key='read-more' quote={quote} />);
|
|
|
|
}
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
if (status.poll_id) {
|
|
|
|
output.push(<Poll id={status.poll_id} 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 };
|