Refactor spoiler button logic into separate SpoilerButton
component (#34257)
This commit is contained in:
parent
5390edc2aa
commit
958953a687
4 changed files with 82 additions and 60 deletions
|
@ -12,6 +12,7 @@ import { debounce } from 'lodash';
|
|||
|
||||
import { AltTextBadge } from 'mastodon/components/alt_text_badge';
|
||||
import { Blurhash } from 'mastodon/components/blurhash';
|
||||
import { SpoilerButton } from 'mastodon/components/spoiler_button';
|
||||
import { formatTime } from 'mastodon/features/video';
|
||||
|
||||
import { autoPlayGif, displayMedia, useBlurhash } from '../initial_state';
|
||||
|
@ -299,7 +300,7 @@ class MediaGallery extends PureComponent {
|
|||
const { visible } = this.state;
|
||||
const width = this.state.width || defaultWidth;
|
||||
|
||||
let children, spoilerButton;
|
||||
let children;
|
||||
|
||||
const style = {};
|
||||
|
||||
|
@ -318,35 +319,11 @@ class MediaGallery extends PureComponent {
|
|||
children = media.map((attachment, i) => <Item key={attachment.get('id')} autoplay={autoplay} onClick={this.handleClick} attachment={attachment} index={i} lang={lang} size={size} displayWidth={width} visible={visible || uncached} />);
|
||||
}
|
||||
|
||||
if (uncached) {
|
||||
spoilerButton = (
|
||||
<button type='button' disabled className='spoiler-button__overlay'>
|
||||
<span className='spoiler-button__overlay__label'>
|
||||
<FormattedMessage id='status.uncached_media_warning' defaultMessage='Preview not available' />
|
||||
<span className='spoiler-button__overlay__action'><FormattedMessage id='status.media.open' defaultMessage='Click to open' /></span>
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
} else if (!visible) {
|
||||
spoilerButton = (
|
||||
<button type='button' onClick={this.handleOpen} className='spoiler-button__overlay'>
|
||||
<span className='spoiler-button__overlay__label'>
|
||||
{sensitive ? <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /> : <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />}
|
||||
<span className='spoiler-button__overlay__action'><FormattedMessage id='status.media.show' defaultMessage='Click to show' /></span>
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`media-gallery media-gallery--layout-${size}`} style={style} ref={this.handleRef}>
|
||||
{children}
|
||||
|
||||
{(!visible || uncached) && (
|
||||
<div className={classNames('spoiler-button', { 'spoiler-button--click-thru': uncached })}>
|
||||
{spoilerButton}
|
||||
</div>
|
||||
)}
|
||||
{(!visible || uncached) && <SpoilerButton uncached={uncached} sensitive={sensitive} onClick={this.handleOpen} />}
|
||||
|
||||
{(visible && !uncached) && (
|
||||
<div className='media-gallery__actions'>
|
||||
|
|
73
app/javascript/mastodon/components/spoiler_button.tsx
Normal file
73
app/javascript/mastodon/components/spoiler_button.tsx
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { FormattedMessage } from 'react-intl';
|
||||
|
||||
import classNames from 'classnames';
|
||||
|
||||
interface Props {
|
||||
hidden?: boolean;
|
||||
sensitive: boolean;
|
||||
uncached?: boolean;
|
||||
onClick: React.MouseEventHandler<HTMLButtonElement>;
|
||||
}
|
||||
|
||||
export const SpoilerButton: React.FC<Props> = ({
|
||||
hidden = false,
|
||||
sensitive,
|
||||
uncached = false,
|
||||
onClick,
|
||||
}) => {
|
||||
let warning;
|
||||
let action;
|
||||
|
||||
if (uncached) {
|
||||
warning = (
|
||||
<FormattedMessage
|
||||
id='status.uncached_media_warning'
|
||||
defaultMessage='Preview not available'
|
||||
/>
|
||||
);
|
||||
action = (
|
||||
<FormattedMessage id='status.media.open' defaultMessage='Click to open' />
|
||||
);
|
||||
} else if (sensitive) {
|
||||
warning = (
|
||||
<FormattedMessage
|
||||
id='status.sensitive_warning'
|
||||
defaultMessage='Sensitive content'
|
||||
/>
|
||||
);
|
||||
action = (
|
||||
<FormattedMessage id='status.media.show' defaultMessage='Click to show' />
|
||||
);
|
||||
} else {
|
||||
warning = (
|
||||
<FormattedMessage
|
||||
id='status.media_hidden'
|
||||
defaultMessage='Media hidden'
|
||||
/>
|
||||
);
|
||||
action = (
|
||||
<FormattedMessage id='status.media.show' defaultMessage='Click to show' />
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames('spoiler-button', {
|
||||
'spoiler-button--hidden': hidden,
|
||||
'spoiler-button--click-thru': uncached,
|
||||
})}
|
||||
>
|
||||
<button
|
||||
type='button'
|
||||
className='spoiler-button__overlay'
|
||||
onClick={onClick}
|
||||
disabled={uncached}
|
||||
>
|
||||
<span className='spoiler-button__overlay__label'>
|
||||
{warning}
|
||||
<span className='spoiler-button__overlay__action'>{action}</span>
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue