feat: use a selector for htmlDirection and pass it down as props instead of using state

this is more inline with class component conventions
pull/2796/head
William Grant 2 years ago
parent ac4a00d415
commit 61149a5ca3

@ -57,6 +57,7 @@ import { MessageDetail } from './message/message-item/MessageDetail';
import styled from 'styled-components'; import styled from 'styled-components';
import { SessionSpinner } from '../basic/SessionSpinner'; import { SessionSpinner } from '../basic/SessionSpinner';
import { HTMLDirection } from '../../util/i18n';
// tslint:disable: jsx-curly-spacing // tslint:disable: jsx-curly-spacing
interface State { interface State {
@ -76,6 +77,7 @@ interface Props {
showMessageDetails: boolean; showMessageDetails: boolean;
isRightPanelShowing: boolean; isRightPanelShowing: boolean;
hasOngoingCallWithFocusedConvo: boolean; hasOngoingCallWithFocusedConvo: boolean;
htmlDirection: HTMLDirection;
// lightbox options // lightbox options
lightBoxOptions?: LightBoxOptions; lightBoxOptions?: LightBoxOptions;
@ -290,6 +292,7 @@ export class SessionConversation extends React.Component<Props, State> {
sendMessage={this.sendMessageFn} sendMessage={this.sendMessageFn}
stagedAttachments={this.props.stagedAttachments} stagedAttachments={this.props.stagedAttachments}
onChoseAttachments={this.onChoseAttachments} onChoseAttachments={this.onChoseAttachments}
htmlDirection={this.props.htmlDirection}
/> />
</div> </div>
<div <div

@ -57,7 +57,7 @@ import {
getSelectedConversationKey, getSelectedConversationKey,
} from '../../../state/selectors/selectedConversation'; } from '../../../state/selectors/selectedConversation';
import { SettingsKey } from '../../../data/settings-key'; import { SettingsKey } from '../../../data/settings-key';
import { getHTMLDirection, HTMLDirection } from '../../../util/i18n'; import { HTMLDirection } from '../../../util/i18n';
export interface ReplyingToMessageProps { export interface ReplyingToMessageProps {
convoId: string; convoId: string;
@ -105,6 +105,7 @@ interface Props {
quotedMessageProps?: ReplyingToMessageProps; quotedMessageProps?: ReplyingToMessageProps;
stagedAttachments: Array<StagedAttachmentType>; stagedAttachments: Array<StagedAttachmentType>;
onChoseAttachments: (newAttachments: Array<File>) => void; onChoseAttachments: (newAttachments: Array<File>) => void;
htmlDirection: HTMLDirection;
} }
interface State { interface State {
@ -114,7 +115,6 @@ interface State {
ignoredLink?: string; // set the ignored url when users closed the link preview ignoredLink?: string; // set the ignored url when users closed the link preview
stagedLinkPreview?: StagedLinkPreviewData; stagedLinkPreview?: StagedLinkPreviewData;
showCaptionEditor?: AttachmentType; showCaptionEditor?: AttachmentType;
htmlDirection?: HTMLDirection;
} }
const sendMessageStyle = (dir?: HTMLDirection) => { const sendMessageStyle = (dir?: HTMLDirection) => {
@ -149,7 +149,6 @@ const getDefaultState = (newConvoId?: string) => {
ignoredLink: undefined, ignoredLink: undefined,
stagedLinkPreview: undefined, stagedLinkPreview: undefined,
showCaptionEditor: undefined, showCaptionEditor: undefined,
htmlDirection: getHTMLDirection(),
}; };
}; };
@ -418,7 +417,7 @@ class CompositionBoxInner extends React.Component<Props, State> {
return ( return (
<Flex <Flex
dir={this.state.htmlDirection} dir={this.props.htmlDirection}
container={true} container={true}
flexDirection={'row'} flexDirection={'row'}
alignItems={'center'} alignItems={'center'}
@ -439,7 +438,7 @@ class CompositionBoxInner extends React.Component<Props, State> {
<StyledSendMessageInput <StyledSendMessageInput
role="main" role="main"
dir={this.state.htmlDirection} dir={this.props.htmlDirection}
onClick={this.focusCompositionBox} // used to focus on the textarea when clicking in its container onClick={this.focusCompositionBox} // used to focus on the textarea when clicking in its container
ref={el => { ref={el => {
this.container = el; this.container = el;
@ -455,7 +454,7 @@ class CompositionBoxInner extends React.Component<Props, State> {
<SendMessageButton onClick={this.onSendMessage} /> <SendMessageButton onClick={this.onSendMessage} />
{typingEnabled && showEmojiPanel && ( {typingEnabled && showEmojiPanel && (
<StyledEmojiPanelContainer role="button" dir={this.state.htmlDirection}> <StyledEmojiPanelContainer role="button" dir={this.props.htmlDirection}>
<SessionEmojiPanel <SessionEmojiPanel
ref={this.emojiPanel} ref={this.emojiPanel}
show={showEmojiPanel} show={showEmojiPanel}
@ -470,7 +469,8 @@ class CompositionBoxInner extends React.Component<Props, State> {
private renderTextArea() { private renderTextArea() {
const { i18n } = window; const { i18n } = window;
const { draft, htmlDirection } = this.state; const { draft } = this.state;
const { htmlDirection } = this.props;
if (!this.props.selectedConversation) { if (!this.props.selectedConversation) {
return null; return null;

@ -4,6 +4,7 @@ import { LocalizerType } from '../../types/Util';
import { StateType } from '../reducer'; import { StateType } from '../reducer';
import { UserStateType } from '../ducks/user'; import { UserStateType } from '../ducks/user';
import { HTMLDirection, isRtlBody } from '../../util/i18n';
export const getUser = (state: StateType): UserStateType => state.user; export const getUser = (state: StateType): UserStateType => state.user;
@ -13,3 +14,8 @@ export const getOurNumber = createSelector(
); );
export const getIntl = createSelector(getUser, (): LocalizerType => window.i18n); export const getIntl = createSelector(getUser, (): LocalizerType => window.i18n);
export const getHTMLDirection = createSelector(
getUser,
(): HTMLDirection => (isRtlBody() ? 'rtl' : 'ltr')
);

@ -17,7 +17,7 @@ import {
} from '../selectors/selectedConversation'; } from '../selectors/selectedConversation';
import { getStagedAttachmentsForCurrentConversation } from '../selectors/stagedAttachments'; import { getStagedAttachmentsForCurrentConversation } from '../selectors/stagedAttachments';
import { getTheme } from '../selectors/theme'; import { getTheme } from '../selectors/theme';
import { getOurNumber } from '../selectors/user'; import { getHTMLDirection, getOurNumber } from '../selectors/user';
const mapStateToProps = (state: StateType) => { const mapStateToProps = (state: StateType) => {
return { return {
@ -33,6 +33,7 @@ const mapStateToProps = (state: StateType) => {
stagedAttachments: getStagedAttachmentsForCurrentConversation(state), stagedAttachments: getStagedAttachmentsForCurrentConversation(state),
hasOngoingCallWithFocusedConvo: getHasOngoingCallWithFocusedConvo(state), hasOngoingCallWithFocusedConvo: getHasOngoingCallWithFocusedConvo(state),
isSelectedConvoInitialLoadingInProgress: getIsSelectedConvoInitialLoadingInProgress(state), isSelectedConvoInitialLoadingInProgress: getIsSelectedConvoInitialLoadingInProgress(state),
htmlDirection: getHTMLDirection(state),
}; };
}; };

@ -73,7 +73,3 @@ export function isRtlBody(): boolean {
return body?.classList.contains('rtl') || false; return body?.classList.contains('rtl') || false;
} }
export function getHTMLDirection(): HTMLDirection {
return isRtlBody() ? 'rtl' : 'ltr';
}

Loading…
Cancel
Save