th-downstream
commit
c52397dea3
@ -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 [].filter.call(body.querySelectorAll('a[href]'), link => link.textContent[0] === '#' || (link.previousSibling?.textContent?.[link.previousSibling.textContent.length - 1] === '#')).map(node => node.textContent);
|
||||
}, [text]);
|
||||
|
||||
const invisibleHashtags = useMemo(() => (
|
||||
hashtags.filter(hashtag => !renderedHashtags.some(textContent => textContent.localeCompare(`#${hashtag.get('name')}`, undefined, { sensitivity: 'accent' }) === 0 || textContent.localeCompare(hashtag.get('name'), undefined, { sensitivity: 'accent' }) === 0))
|
||||
), [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,
|
||||
};
|
@ -0,0 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Monkey patching until https://github.com/httprb/http/pull/757 is merged
|
||||
unless HTTP::Request::METHODS.include?(:purge)
|
||||
module HTTP
|
||||
class Request
|
||||
METHODS = METHODS.dup.push(:purge).freeze
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
describe CacheBuster do
|
||||
subject { described_class.new(secret_header: secret_header, secret: secret, http_method: http_method) }
|
||||
|
||||
let(:secret_header) { nil }
|
||||
let(:secret) { nil }
|
||||
let(:http_method) { nil }
|
||||
|
||||
let(:purge_url) { 'https://example.com/test_purge' }
|
||||
|
||||
describe '#bust' do
|
||||
shared_examples 'makes_request' do
|
||||
it 'makes an HTTP purging request' do
|
||||
method = http_method&.to_sym || :get
|
||||
stub_request(method, purge_url).to_return(status: 200)
|
||||
|
||||
subject.bust(purge_url)
|
||||
|
||||
test_request = a_request(method, purge_url)
|
||||
|
||||
test_request = test_request.with(headers: { secret_header => secret }) if secret && secret_header
|
||||
|
||||
expect(test_request).to have_been_made.once
|
||||
end
|
||||
end
|
||||
|
||||
context 'when using default options' do
|
||||
include_examples 'makes_request'
|
||||
end
|
||||
|
||||
context 'when specifying a secret header' do
|
||||
let(:secret_header) { 'X-Purge-Secret' }
|
||||
let(:secret) { SecureRandom.hex(20) }
|
||||
|
||||
include_examples 'makes_request'
|
||||
end
|
||||
|
||||
context 'when specifying a PURGE method' do
|
||||
let(:http_method) { 'purge' }
|
||||
|
||||
context 'when not using headers' do
|
||||
include_examples 'makes_request'
|
||||
end
|
||||
|
||||
context 'when specifying a secret header' do
|
||||
let(:secret_header) { 'X-Purge-Secret' }
|
||||
let(:secret) { SecureRandom.hex(20) }
|
||||
|
||||
include_examples 'makes_request'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue