Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add unexposed account setting for hiding poll creation (#7972)
Browse files Browse the repository at this point in the history
* Add unexposed account setting for hiding poll creation

This is to match the sticker picker setting we already have, but not exposed in the user settings until we decide if we actually want it to be there.

This is primarily intended to be used in environments that disable it at the config level, though given there's multiple people in the wild who have asked for this, it seems reasonable to expose it as a hidden setting instead. Note that as of writing it's not clear if any of those requests made it as far as an issue tracker.

Typically for the usecase presented we'd add a new UIFeature flag, however this *feels* wrong for the case at hand.

* Move visibility check way higher

* Fix i18n post-merge
  • Loading branch information
turt2live authored Mar 4, 2022
1 parent aa48cfd commit b981a96
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/components/views/rooms/MessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface IState {
isMenuOpen: boolean;
isStickerPickerOpen: boolean;
showStickersButton: boolean;
showPollsButton: boolean;
}

@replaceableComponent("views.rooms.MessageComposer")
Expand Down Expand Up @@ -117,11 +118,13 @@ export default class MessageComposer extends React.Component<IProps, IState> {
isMenuOpen: false,
isStickerPickerOpen: false,
showStickersButton: SettingsStore.getValue("MessageComposerInput.showStickersButton"),
showPollsButton: SettingsStore.getValue("MessageComposerInput.showPollsButton"),
};

this.instanceId = instanceCount++;

SettingsStore.monitorSetting("MessageComposerInput.showStickersButton", null);
SettingsStore.monitorSetting("MessageComposerInput.showPollsButton", null);
}

private get voiceRecording(): Optional<VoiceRecording> {
Expand Down Expand Up @@ -189,6 +192,13 @@ export default class MessageComposer extends React.Component<IProps, IState> {
}
break;
}
case "MessageComposerInput.showPollsButton": {
const showPollsButton = SettingsStore.getValue("MessageComposerInput.showPollsButton");
if (this.state.showPollsButton !== showPollsButton) {
this.setState({ showPollsButton });
}
break;
}
}
}
}
Expand Down Expand Up @@ -459,6 +469,7 @@ export default class MessageComposer extends React.Component<IProps, IState> {
}}
setStickerPickerOpen={this.setStickerPickerOpen}
showLocationButton={!window.electron}
showPollsButton={this.state.showPollsButton}
showStickersButton={this.state.showStickersButton}
toggleButtonMenu={this.toggleButtonMenu}
/> }
Expand Down
9 changes: 5 additions & 4 deletions src/components/views/rooms/MessageComposerButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface IProps {
relation?: IEventRelation;
setStickerPickerOpen: (isStickerPickerOpen: boolean) => void;
showLocationButton: boolean;
showPollsButton: boolean;
showStickersButton: boolean;
toggleButtonMenu: () => void;
}
Expand All @@ -73,7 +74,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
uploadButton(), // props passed via UploadButtonContext
showStickersButton(props),
voiceRecordingButton(props, narrow),
pollButton(room, props.relation),
props.showPollsButton && pollButton(room, props.relation),
showLocationButton(props, room, roomId, matrixClient),
];
} else {
Expand All @@ -84,7 +85,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
moreButtons = [
showStickersButton(props),
voiceRecordingButton(props, narrow),
pollButton(room, props.relation),
props.showPollsButton && pollButton(room, props.relation),
showLocationButton(props, room, roomId, matrixClient),
];
}
Expand Down Expand Up @@ -295,7 +296,7 @@ interface IPollButtonProps {
}

class PollButton extends React.PureComponent<IPollButtonProps> {
static contextType = OverflowMenuContext;
public static contextType = OverflowMenuContext;
public context!: React.ContextType<typeof OverflowMenuContext>;

private onCreateClick = () => {
Expand Down Expand Up @@ -336,7 +337,7 @@ class PollButton extends React.PureComponent<IPollButtonProps> {
}
};

render() {
public render() {
// do not allow sending polls within threads at this time
if (this.props.relation?.rel_type === RelationType.Thread) return null;

Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@
"Use custom size": "Use custom size",
"Enable Emoji suggestions while typing": "Enable Emoji suggestions while typing",
"Show stickers button": "Show stickers button",
"Show polls button": "Show polls button",
"Insert a trailing colon after user mentions at the start of a message": "Insert a trailing colon after user mentions at the start of a message",
"Use a more compact 'Modern' layout": "Use a more compact 'Modern' layout",
"Show a placeholder for removed messages": "Show a placeholder for removed messages",
Expand Down
5 changes: 5 additions & 0 deletions src/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ export const SETTINGS: {[setting: string]: ISetting} = {
default: true,
controller: new UIFeatureController(UIFeature.Widgets, false),
},
"MessageComposerInput.showPollsButton": {
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
displayName: _td('Show polls button'),
default: true,
},
"MessageComposerInput.insertTrailingColon": {
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
displayName: _td('Insert a trailing colon after user mentions at the start of a message'),
Expand Down
54 changes: 54 additions & 0 deletions test/components/views/rooms/MessageComposerButtons-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe("MessageComposerButtons", () => {
<MessageComposerButtons
isMenuOpen={false}
showLocationButton={true}
showPollsButton={true}
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
Expand All @@ -57,6 +58,7 @@ describe("MessageComposerButtons", () => {
<MessageComposerButtons
isMenuOpen={true}
showLocationButton={true}
showPollsButton={true}
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
Expand All @@ -81,6 +83,7 @@ describe("MessageComposerButtons", () => {
<MessageComposerButtons
isMenuOpen={false}
showLocationButton={true}
showPollsButton={true}
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
Expand All @@ -98,6 +101,7 @@ describe("MessageComposerButtons", () => {
<MessageComposerButtons
isMenuOpen={true}
showLocationButton={true}
showPollsButton={true}
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
Expand All @@ -115,6 +119,56 @@ describe("MessageComposerButtons", () => {
],
]);
});

describe('polls button', () => {
it('should render when asked to', () => {
const buttons = wrapAndRender(
<MessageComposerButtons
isMenuOpen={true}
showLocationButton={true}
showPollsButton={true}
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
true,
);

expect(buttonLabels(buttons)).toEqual([
"Emoji",
"More options",
[
"Attachment",
"Sticker",
"Poll",
"Location",
],
]);
});

it('should not render when asked not to', () => {
const buttons = wrapAndRender(
<MessageComposerButtons
isMenuOpen={true}
showLocationButton={true}
showPollsButton={false} // !! the change from the alternate test
showStickersButton={true}
toggleButtonMenu={() => {}}
/>,
true,
);

expect(buttonLabels(buttons)).toEqual([
"Emoji",
"More options",
[
"Attachment",
"Sticker",
// "Poll", // should be hidden
"Location",
],
]);
});
});
});

function wrapAndRender(component: React.ReactElement, narrow: boolean): ReactWrapper {
Expand Down

0 comments on commit b981a96

Please sign in to comment.