[IMPROVE] - migrating the MessageBox container (in progress)

This commit is contained in:
AlexAlexandre 2021-07-22 23:59:46 -03:00
parent db11e664f6
commit 0182853fdb
15 changed files with 99 additions and 101 deletions

View File

@ -1,5 +1,4 @@
import React, { useContext, useState } from 'react'; import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import { TouchableOpacity } from 'react-native'; import { TouchableOpacity } from 'react-native';
import FastImage from '@rocket.chat/react-native-fast-image'; import FastImage from '@rocket.chat/react-native-fast-image';
@ -9,7 +8,16 @@ import { themes } from '../../../constants/colors';
import MessageboxContext from '../Context'; import MessageboxContext from '../Context';
import ActivityIndicator from '../../ActivityIndicator'; import ActivityIndicator from '../../ActivityIndicator';
const Item = ({ item, theme }) => { interface IMessageBoxCommandsPreviewItem {
item: {
type: string;
id: string;
value: string;
};
theme: string;
}
const Item = ({ item, theme }: IMessageBoxCommandsPreviewItem) => {
const context = useContext(MessageboxContext); const context = useContext(MessageboxContext);
const { onPressCommandPreview } = context; const { onPressCommandPreview } = context;
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@ -38,9 +46,4 @@ const Item = ({ item, theme }) => {
); );
}; };
Item.propTypes = {
item: PropTypes.object,
theme: PropTypes.string
};
export default Item; export default Item;

View File

@ -8,7 +8,13 @@ import styles from '../styles';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
import { withTheme } from '../../../theme'; import { withTheme } from '../../../theme';
const CommandsPreview = React.memo(({ theme, commandPreview, showCommandPreview }) => { interface IMessageBoxCommandsPreview {
commandPreview: [];
showCommandPreview: boolean;
theme: string;
}
const CommandsPreview = React.memo(({ theme, commandPreview, showCommandPreview }: IMessageBoxCommandsPreview) => {
if (!showCommandPreview) { if (!showCommandPreview) {
return null; return null;
} }
@ -18,7 +24,7 @@ const CommandsPreview = React.memo(({ theme, commandPreview, showCommandPreview
style={[styles.mentionList, { backgroundColor: themes[theme].messageboxBackground }]} style={[styles.mentionList, { backgroundColor: themes[theme].messageboxBackground }]}
data={commandPreview} data={commandPreview}
renderItem={({ item }) => <Item item={item} theme={theme} />} renderItem={({ item }) => <Item item={item} theme={theme} />}
keyExtractor={item => item.id} keyExtractor={(item: any) => item.id}
keyboardShouldPersistTaps='always' keyboardShouldPersistTaps='always'
horizontal horizontal
showsHorizontalScrollIndicator={false} showsHorizontalScrollIndicator={false}
@ -37,10 +43,4 @@ const CommandsPreview = React.memo(({ theme, commandPreview, showCommandPreview
return true; return true;
}); });
CommandsPreview.propTypes = {
commandPreview: PropTypes.array,
showCommandPreview: PropTypes.bool,
theme: PropTypes.string
};
export default withTheme(CommandsPreview); export default withTheme(CommandsPreview);

View File

@ -1,4 +0,0 @@
import React from 'react';
const MessageboxContext = React.createContext();
export default MessageboxContext;

View File

@ -0,0 +1,5 @@
import React from 'react';
// @ts-ignore
const MessageboxContext = React.createContext<any>();
export default MessageboxContext;

View File

@ -1,12 +1,19 @@
import React from 'react'; import React from 'react';
import { TouchableOpacity, Text } from 'react-native'; import { TouchableOpacity, Text } from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles'; import styles from '../styles';
import I18n from '../../../i18n'; import I18n from '../../../i18n';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
const FixedMentionItem = ({ item, onPress, theme }) => ( interface IMessageBoxFixedMentionItem {
item: {
username: string;
};
onPress({}): void;
theme: string;
}
const FixedMentionItem = ({ item, onPress, theme }: IMessageBoxFixedMentionItem) => (
<TouchableOpacity <TouchableOpacity
style={[ style={[
styles.mentionItem, styles.mentionItem,
@ -24,10 +31,4 @@ const FixedMentionItem = ({ item, onPress, theme }) => (
</TouchableOpacity> </TouchableOpacity>
); );
FixedMentionItem.propTypes = {
item: PropTypes.object,
onPress: PropTypes.func,
theme: PropTypes.string
};
export default FixedMentionItem; export default FixedMentionItem;

View File

@ -6,8 +6,13 @@ import shortnameToUnicode from '../../../utils/shortnameToUnicode';
import styles from '../styles'; import styles from '../styles';
import MessageboxContext from '../Context'; import MessageboxContext from '../Context';
import CustomEmoji from '../../EmojiPicker/CustomEmoji'; import CustomEmoji from '../../EmojiPicker/CustomEmoji';
import {TEmoji} from "../../EmojiPicker";
const MentionEmoji = ({ item }) => { interface IMessageBoxMentionEmoji {
item: TEmoji;
}
const MentionEmoji = ({ item }: IMessageBoxMentionEmoji) => {
const context = useContext(MessageboxContext); const context = useContext(MessageboxContext);
const { baseUrl } = context; const { baseUrl } = context;

View File

@ -1,25 +1,32 @@
import React, { useContext } from 'react'; import React, { useContext } from 'react';
import { TouchableOpacity, Text } from 'react-native'; import { TouchableOpacity, Text } from 'react-native';
import PropTypes from 'prop-types';
import styles from '../styles'; import styles from '../styles';
import Avatar from '../../Avatar'; import Avatar from '../../Avatar';
import MessageboxContext from '../Context'; import MessageboxContext from '../Context';
import FixedMentionItem from './FixedMentionItem'; import FixedMentionItem from './FixedMentionItem';
import MentionEmoji from './MentionEmoji'; import MentionEmoji from './MentionEmoji';
import { import { MENTIONS_TRACKING_TYPE_EMOJIS, MENTIONS_TRACKING_TYPE_COMMANDS } from '../constants';
MENTIONS_TRACKING_TYPE_EMOJIS,
MENTIONS_TRACKING_TYPE_COMMANDS
} from '../constants';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
import {TEmoji} from "../../EmojiPicker";
const MentionItem = ({ interface IMessageBoxMentionItem {
item, trackingType, theme item: {
}) => { name: string;
command: string;
username: string;
t: string;
id: string;
} & TEmoji;
trackingType: string;
theme: string;
}
const MentionItem = ({ item, trackingType, theme }: IMessageBoxMentionItem) => {
const context = useContext(MessageboxContext); const context = useContext(MessageboxContext);
const { onPressMention } = context; const { onPressMention } = context;
const defineTestID = (type) => { const defineTestID = (type: string) => {
switch (type) { switch (type) {
case MENTIONS_TRACKING_TYPE_EMOJIS: case MENTIONS_TRACKING_TYPE_EMOJIS:
return `mention-item-${ item.name || item }`; return `mention-item-${ item.name || item }`;
@ -83,10 +90,4 @@ const MentionItem = ({
); );
}; };
MentionItem.propTypes = {
item: PropTypes.object,
trackingType: PropTypes.string,
theme: PropTypes.string
};
export default MentionItem; export default MentionItem;

View File

@ -7,7 +7,12 @@ import styles from '../styles';
import MentionItem from './MentionItem'; import MentionItem from './MentionItem';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
const Mentions = React.memo(({ mentions, trackingType, theme }) => { interface IMessageBoxMentions {
mentions: [];
trackingType: string;
theme: string;
}
const Mentions = React.memo(({ mentions, trackingType, theme }: IMessageBoxMentions) => {
if (!trackingType) { if (!trackingType) {
return null; return null;
} }
@ -18,7 +23,7 @@ const Mentions = React.memo(({ mentions, trackingType, theme }) => {
data={mentions} data={mentions}
extraData={mentions} extraData={mentions}
renderItem={({ item }) => <MentionItem item={item} trackingType={trackingType} theme={theme} />} renderItem={({ item }) => <MentionItem item={item} trackingType={trackingType} theme={theme} />}
keyExtractor={item => item.rid || item.name || item.command || item} keyExtractor={(item: any) => item.rid || item.name || item.command || item}
keyboardShouldPersistTaps='always' keyboardShouldPersistTaps='always'
/> />
</View> </View>
@ -36,10 +41,4 @@ const Mentions = React.memo(({ mentions, trackingType, theme }) => {
return true; return true;
}); });
Mentions.propTypes = {
mentions: PropTypes.array,
trackingType: PropTypes.string,
theme: PropTypes.string
};
export default Mentions; export default Mentions;

View File

@ -1,9 +1,12 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import BaseButton from './BaseButton'; import BaseButton from './BaseButton';
const ActionsButton = React.memo(({ theme, onPress }) => ( interface IActionsButton {
theme: string;
onPress(): void;
}
const ActionsButton = React.memo(({ theme, onPress }: IActionsButton) => (
<BaseButton <BaseButton
onPress={onPress} onPress={onPress}
testID='messagebox-actions' testID='messagebox-actions'
@ -13,9 +16,4 @@ const ActionsButton = React.memo(({ theme, onPress }) => (
/> />
)); ));
ActionsButton.propTypes = {
theme: PropTypes.string,
onPress: PropTypes.func.isRequired
};
export default ActionsButton; export default ActionsButton;

View File

@ -1,33 +1,31 @@
import React from 'react'; import React from 'react';
import { BorderlessButton } from 'react-native-gesture-handler'; import { BorderlessButton } from 'react-native-gesture-handler';
import PropTypes from 'prop-types';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
import { CustomIcon } from '../../../lib/Icons'; import { CustomIcon } from '../../../lib/Icons';
import styles from '../styles'; import styles from '../styles';
import I18n from '../../../i18n'; import I18n from '../../../i18n';
const BaseButton = React.memo(({ interface IBaseButton {
onPress, testID, accessibilityLabel, icon, theme, color theme: string;
}) => ( onPress(): void;
testID: string;
accessibilityLabel: string;
icon: string;
color: string;
}
const BaseButton = React.memo(({ onPress, testID, accessibilityLabel, icon, theme, color }: Partial<IBaseButton>) => (
<BorderlessButton <BorderlessButton
onPress={onPress} onPress={onPress}
style={styles.actionButton} style={styles.actionButton}
testID={testID} testID={testID}
// @ts-ignore
accessibilityLabel={I18n.t(accessibilityLabel)} accessibilityLabel={I18n.t(accessibilityLabel)}
accessibilityTraits='button' accessibilityTraits='button'
> >
<CustomIcon name={icon} size={24} color={color ?? themes[theme].auxiliaryTintColor} /> <CustomIcon name={icon} size={24} color={color ?? themes[theme!].auxiliaryTintColor} />
</BorderlessButton> </BorderlessButton>
)); ));
BaseButton.propTypes = {
theme: PropTypes.string,
onPress: PropTypes.func.isRequired,
testID: PropTypes.string.isRequired,
accessibilityLabel: PropTypes.string.isRequired,
icon: PropTypes.string.isRequired,
color: PropTypes.string
};
export default BaseButton; export default BaseButton;

View File

@ -1,9 +1,12 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import BaseButton from './BaseButton'; import BaseButton from './BaseButton';
const CancelEditingButton = React.memo(({ theme, onPress }) => ( interface ICancelEditingButton {
theme: string;
onPress():void;
}
const CancelEditingButton = React.memo(({ theme, onPress }: ICancelEditingButton) => (
<BaseButton <BaseButton
onPress={onPress} onPress={onPress}
testID='messagebox-cancel-editing' testID='messagebox-cancel-editing'
@ -13,9 +16,4 @@ const CancelEditingButton = React.memo(({ theme, onPress }) => (
/> />
)); ));
CancelEditingButton.propTypes = {
theme: PropTypes.string,
onPress: PropTypes.func.isRequired
};
export default CancelEditingButton; export default CancelEditingButton;

View File

@ -1,10 +1,13 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import BaseButton from './BaseButton'; import BaseButton from './BaseButton';
import { themes } from '../../../constants/colors'; import { themes } from '../../../constants/colors';
const SendButton = React.memo(({ theme, onPress }) => ( interface ISendButton {
theme: string;
onPress(): void;
}
const SendButton = React.memo(({ theme, onPress }: ISendButton) => (
<BaseButton <BaseButton
onPress={onPress} onPress={onPress}
testID='messagebox-send-message' testID='messagebox-send-message'
@ -15,9 +18,4 @@ const SendButton = React.memo(({ theme, onPress }) => (
/> />
)); ));
SendButton.propTypes = {
theme: PropTypes.string,
onPress: PropTypes.func.isRequired
};
export default SendButton; export default SendButton;

View File

@ -1,11 +1,14 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import BaseButton from './BaseButton'; import BaseButton from './BaseButton';
const ToggleEmojiButton = React.memo(({ interface IToggleEmojiButton {
theme, show, open, close theme: string;
}) => { show: boolean;
open(): void;
close(): void;
}
const ToggleEmojiButton = React.memo(({ theme, show, open, close }: IToggleEmojiButton) => {
if (show) { if (show) {
return ( return (
<BaseButton <BaseButton
@ -28,11 +31,4 @@ const ToggleEmojiButton = React.memo(({
); );
}); });
ToggleEmojiButton.propTypes = {
theme: PropTypes.string,
show: PropTypes.bool,
open: PropTypes.func.isRequired,
close: PropTypes.func.isRequired
};
export default ToggleEmojiButton; export default ToggleEmojiButton;