0
0
Fork 0

Add display of out-of-band hashtags in the web interface (#26492)

Co-authored-by: Eugen Rochko <eugen@zeonfederated.com>
This commit is contained in:
Claire 2023-08-14 23:42:30 +02:00 committed by GitHub
parent d9c21293aa
commit df6e719898
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,50 @@
import PropTypes from 'prop-types';
import { useMemo, useState, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import { Link } from 'react-router-dom';
import ImmutablePropTypes from 'react-immutable-proptypes';
const domParser = new DOMParser();
// About two lines on desktop
const VISIBLE_HASHTAGS = 7;
export const HashtagBar = ({ hashtags, text }) => {
const renderedHashtags = useMemo(() => {
const body = domParser.parseFromString(text, 'text/html').documentElement;
return [].map.call(body.querySelectorAll('[rel=tag]'), node => node.textContent.toLowerCase());
}, [text]);
const invisibleHashtags = useMemo(() => (
hashtags.filter(hashtag => !renderedHashtags.some(textContent => textContent === `#${hashtag.get('name')}` || textContent === hashtag.get('name')))
), [hashtags, renderedHashtags]);
const [expanded, setExpanded] = useState(false);
const handleClick = useCallback(() => setExpanded(true), []);
if (invisibleHashtags.isEmpty()) {
return null;
}
const revealedHashtags = expanded ? invisibleHashtags : invisibleHashtags.take(VISIBLE_HASHTAGS);
return (
<div className='hashtag-bar'>
{revealedHashtags.map(hashtag => (
<Link key={hashtag.get('name')} to={`/tags/${hashtag.get('name')}`}>
#{hashtag.get('name')}
</Link>
))}
{!expanded && invisibleHashtags.size > VISIBLE_HASHTAGS && <button className='link-button' onClick={handleClick}><FormattedMessage id='hashtags.and_other' defaultMessage='…and {count, plural, other {# more}}' values={{ count: invisibleHashtags.size - VISIBLE_HASHTAGS }} /></button>}
</div>
);
};
HashtagBar.propTypes = {
hashtags: ImmutablePropTypes.list,
text: PropTypes.string,
};

View file

@ -22,6 +22,7 @@ import { displayMedia } from '../initial_state';
import { Avatar } from './avatar';
import { AvatarOverlay } from './avatar_overlay';
import { DisplayName } from './display_name';
import { HashtagBar } from './hashtag_bar';
import { RelativeTimestamp } from './relative_timestamp';
import StatusActionBar from './status_action_bar';
import StatusContent from './status_content';
@ -580,6 +581,8 @@ class Status extends ImmutablePureComponent {
{media}
<HashtagBar hashtags={status.get('tags')} text={status.get('content')} />
<StatusActionBar scrollKey={scrollKey} status={status} account={account} onFilter={matchedFilters ? this.handleFilterClick : null} {...other} />
</div>
</div>