Refactor privacy dropdown to have an interface closer to upstream's

lolsob-rspec
Claire 2021-02-12 11:36:41 +01:00
parent 5f403bb57a
commit cc5b429545
2 changed files with 110 additions and 74 deletions

View File

@ -9,6 +9,7 @@ import spring from 'react-motion/lib/spring';
import IconButton from 'flavours/glitch/components/icon_button'; import IconButton from 'flavours/glitch/components/icon_button';
import TextIconButton from './text_icon_button'; import TextIconButton from './text_icon_button';
import Dropdown from './dropdown'; import Dropdown from './dropdown';
import PrivacyDropdown from './privacy_dropdown';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
// Utils. // Utils.
@ -25,22 +26,10 @@ const messages = defineMessages({
defaultMessage: 'Attach...', defaultMessage: 'Attach...',
id: 'compose.attach', id: 'compose.attach',
}, },
change_privacy: {
defaultMessage: 'Adjust status privacy',
id: 'privacy.change',
},
content_type: { content_type: {
defaultMessage: 'Content type', defaultMessage: 'Content type',
id: 'content-type.change', id: 'content-type.change',
}, },
direct_long: {
defaultMessage: 'Visible for mentioned users only',
id: 'privacy.direct.long',
},
direct_short: {
defaultMessage: 'Direct',
id: 'privacy.direct.short',
},
doodle: { doodle: {
defaultMessage: 'Draw something', defaultMessage: 'Draw something',
id: 'compose.attach.doodle', id: 'compose.attach.doodle',
@ -65,22 +54,6 @@ const messages = defineMessages({
defaultMessage: 'Plain text', defaultMessage: 'Plain text',
id: 'compose.content-type.plain', id: 'compose.content-type.plain',
}, },
private_long: {
defaultMessage: 'Visible for followers only',
id: 'privacy.private.long',
},
private_short: {
defaultMessage: 'Followers-only',
id: 'privacy.private.short',
},
public_long: {
defaultMessage: 'Visible for all, shown in public timelines',
id: 'privacy.public.long',
},
public_short: {
defaultMessage: 'Public',
id: 'privacy.public.short',
},
spoiler: { spoiler: {
defaultMessage: 'Hide text behind warning', defaultMessage: 'Hide text behind warning',
id: 'compose_form.spoiler', id: 'compose_form.spoiler',
@ -93,14 +66,6 @@ const messages = defineMessages({
defaultMessage: 'Threaded mode', defaultMessage: 'Threaded mode',
id: 'advanced_options.threaded_mode.short', id: 'advanced_options.threaded_mode.short',
}, },
unlisted_long: {
defaultMessage: 'Visible for all, but not in public timelines',
id: 'privacy.unlisted.long',
},
unlisted_short: {
defaultMessage: 'Unlisted',
id: 'privacy.unlisted.short',
},
upload: { upload: {
defaultMessage: 'Upload a file', defaultMessage: 'Upload a file',
id: 'compose.attach.upload', id: 'compose.attach.upload',
@ -201,35 +166,6 @@ class ComposerOptions extends ImmutablePureComponent {
showContentTypeChoice, showContentTypeChoice,
} = this.props; } = this.props;
// We predefine our privacy items so that we can easily pick the
// dropdown icon later.
const privacyItems = {
direct: {
icon: 'envelope',
meta: <FormattedMessage {...messages.direct_long} />,
name: 'direct',
text: <FormattedMessage {...messages.direct_short} />,
},
private: {
icon: 'lock',
meta: <FormattedMessage {...messages.private_long} />,
name: 'private',
text: <FormattedMessage {...messages.private_short} />,
},
public: {
icon: 'globe',
meta: <FormattedMessage {...messages.public_long} />,
name: 'public',
text: <FormattedMessage {...messages.public_short} />,
},
unlisted: {
icon: 'unlock',
meta: <FormattedMessage {...messages.unlisted_long} />,
name: 'unlisted',
text: <FormattedMessage {...messages.unlisted_short} />,
},
};
const contentTypeItems = { const contentTypeItems = {
plain: { plain: {
icon: 'file-text', icon: 'file-text',
@ -297,19 +233,11 @@ class ComposerOptions extends ImmutablePureComponent {
/> />
)} )}
<hr /> <hr />
<Dropdown <PrivacyDropdown
disabled={disabled} disabled={disabled}
icon={(privacyItems[privacy] || {}).icon}
items={[
privacyItems.public,
privacyItems.unlisted,
privacyItems.private,
privacyItems.direct,
]}
onChange={onChangeVisibility} onChange={onChangeVisibility}
onModalClose={onModalClose} onModalClose={onModalClose}
onModalOpen={onModalOpen} onModalOpen={onModalOpen}
title={intl.formatMessage(messages.change_privacy)}
value={privacy} value={privacy}
/> />
{showContentTypeChoice && ( {showContentTypeChoice && (

View File

@ -0,0 +1,108 @@
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { FormattedMessage, defineMessages, injectIntl } from 'react-intl';
import Dropdown from './dropdown';
const messages = defineMessages({
change_privacy: {
defaultMessage: 'Adjust status privacy',
id: 'privacy.change',
},
direct_long: {
defaultMessage: 'Visible for mentioned users only',
id: 'privacy.direct.long',
},
direct_short: {
defaultMessage: 'Direct',
id: 'privacy.direct.short',
},
private_long: {
defaultMessage: 'Visible for followers only',
id: 'privacy.private.long',
},
private_short: {
defaultMessage: 'Followers-only',
id: 'privacy.private.short',
},
public_long: {
defaultMessage: 'Visible for all, shown in public timelines',
id: 'privacy.public.long',
},
public_short: {
defaultMessage: 'Public',
id: 'privacy.public.short',
},
unlisted_long: {
defaultMessage: 'Visible for all, but not in public timelines',
id: 'privacy.unlisted.long',
},
unlisted_short: {
defaultMessage: 'Unlisted',
id: 'privacy.unlisted.short',
},
});
export default @injectIntl
class PrivacyDropdown extends React.PureComponent {
static propTypes = {
isUserTouching: PropTypes.func,
onModalOpen: PropTypes.func,
onModalClose: PropTypes.func,
value: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
noDirect: PropTypes.bool,
container: PropTypes.func,
intl: PropTypes.object.isRequired,
};
render () {
const { value, onChange, onModalOpen, onModalClose, disabled, intl } = this.props;
// We predefine our privacy items so that we can easily pick the
// dropdown icon later.
const privacyItems = {
direct: {
icon: 'envelope',
meta: <FormattedMessage {...messages.direct_long} />,
name: 'direct',
text: <FormattedMessage {...messages.direct_short} />,
},
private: {
icon: 'lock',
meta: <FormattedMessage {...messages.private_long} />,
name: 'private',
text: <FormattedMessage {...messages.private_short} />,
},
public: {
icon: 'globe',
meta: <FormattedMessage {...messages.public_long} />,
name: 'public',
text: <FormattedMessage {...messages.public_short} />,
},
unlisted: {
icon: 'unlock',
meta: <FormattedMessage {...messages.unlisted_long} />,
name: 'unlisted',
text: <FormattedMessage {...messages.unlisted_short} />,
},
};
const items = [privacyItems.public, privacyItems.unlisted, privacyItems.private, privacyItems.direct];
return (
<Dropdown
disabled={disabled}
icon={(privacyItems[value] || {}).icon}
items={items}
onChange={onChange}
onModalClose={onModalClose}
onModalOpen={onModalOpen}
title={intl.formatMessage(messages.change_privacy)}
value={value}
/>
);
}
}