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

Commit

Permalink
Fix upload button & shortcut not working for narrow composer mode (#7894
Browse files Browse the repository at this point in the history
)
  • Loading branch information
t3chguy authored Feb 24, 2022
1 parent 4629d1e commit 5167b30
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/components/views/rooms/MessageComposerButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
emojiButton(props),
];
moreButtons = [
uploadButton(props, roomId),
uploadButton(), // props passed via UploadButtonContext
showStickersButton(props),
voiceRecordingButton(props, narrow),
pollButton(room, props.relation),
Expand All @@ -79,7 +79,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
} else {
mainButtons = [
emojiButton(props),
uploadButton(props, roomId),
uploadButton(), // props passed via UploadButtonContext
];
moreButtons = [
showStickersButton(props),
Expand All @@ -98,7 +98,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
mx_MessageComposer_closeButtonMenu: props.isMenuOpen,
});

return <>
return <UploadButtonContextProvider roomId={roomId} relation={props.relation}>
{ mainButtons }
<AccessibleTooltipButton
className={moreOptionsClasses}
Expand All @@ -116,7 +116,7 @@ const MessageComposerButtons: React.FC<IProps> = (props: IProps) => {
</OverflowMenuContext.Provider>
</ContextMenu>
) }
</>;
</UploadButtonContextProvider>;
};

function emojiButton(props: IProps): ReactElement {
Expand Down Expand Up @@ -175,23 +175,22 @@ const EmojiButton: React.FC<IEmojiButtonProps> = ({ addEmoji, menuPosition }) =>
</React.Fragment>;
};

function uploadButton(props: IProps, roomId: string): ReactElement {
return <UploadButton
key="controls_upload"
roomId={roomId}
relation={props.relation}
/>;
function uploadButton(): ReactElement {
return <UploadButton key="controls_upload" />;
}

type UploadButtonFn = () => void;
export const UploadButtonContext = createContext<UploadButtonFn | null>(null);

interface IUploadButtonProps {
roomId: string;
relation?: IEventRelation | null;
}

const UploadButton = ({ roomId, relation }: IUploadButtonProps) => {
// We put the file input outside the UploadButton component so that it doesn't get killed when the context menu closes.
const UploadButtonContextProvider: React.FC<IUploadButtonProps> = ({ roomId, relation, children }) => {
const cli = useContext(MatrixClientContext);
const roomContext = useContext(RoomContext);
const overflowMenuCloser = useContext(OverflowMenuContext);
const uploadInput = useRef<HTMLInputElement>();

const onUploadClick = () => {
Expand All @@ -200,7 +199,6 @@ const UploadButton = ({ roomId, relation }: IUploadButtonProps) => {
return;
}
uploadInput.current?.click();
overflowMenuCloser?.(); // close overflow menu
};

useDispatcher(dis, payload => {
Expand Down Expand Up @@ -229,20 +227,34 @@ const UploadButton = ({ roomId, relation }: IUploadButtonProps) => {
};

const uploadInputStyle = { display: 'none' };
return <>
<CollapsibleButton
className="mx_MessageComposer_button mx_MessageComposer_upload"
onClick={onUploadClick}
title={_t('Attachment')}
/>
return <UploadButtonContext.Provider value={onUploadClick}>
{ children }

<input
ref={uploadInput}
type="file"
style={uploadInputStyle}
multiple
onChange={onUploadFileInputChange}
/>
</>;
</UploadButtonContext.Provider>;
};

// Must be rendered within an UploadButtonContextProvider
const UploadButton = () => {
const overflowMenuCloser = useContext(OverflowMenuContext);
const uploadButtonFn = useContext(UploadButtonContext);

const onClick = () => {
uploadButtonFn?.();
overflowMenuCloser?.(); // close overflow menu
};

return <CollapsibleButton
className="mx_MessageComposer_button mx_MessageComposer_upload"
onClick={onClick}
title={_t('Attachment')}
/>;
};

function showStickersButton(props: IProps): ReactElement {
Expand Down

0 comments on commit 5167b30

Please sign in to comment.