2021-09-13 20:41:05 +00:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
|
|
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
|
|
|
import { themes } from '../../constants/colors';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { MarkdownPreview } from '../markdown';
|
2021-09-13 20:41:05 +00:00
|
|
|
import RoomTypeIcon from '../RoomTypeIcon';
|
|
|
|
import { withTheme } from '../../theme';
|
2022-03-25 20:05:49 +00:00
|
|
|
import { TUserStatus } from '../../definitions';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const HIT_SLOP = {
|
|
|
|
top: 5,
|
|
|
|
right: 5,
|
|
|
|
bottom: 5,
|
|
|
|
left: 5
|
|
|
|
};
|
|
|
|
const TITLE_SIZE = 16;
|
|
|
|
const SUBTITLE_SIZE = 12;
|
|
|
|
|
|
|
|
const getSubTitleSize = (scale: number) => SUBTITLE_SIZE * scale;
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
titleContainer: {
|
|
|
|
alignItems: 'center',
|
|
|
|
flexDirection: 'row'
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
flexShrink: 1,
|
|
|
|
...sharedStyles.textSemibold
|
|
|
|
},
|
|
|
|
subtitle: {
|
|
|
|
flexShrink: 1,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
typingUsers: {
|
|
|
|
...sharedStyles.textSemibold
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
type TRoomHeaderSubTitle = {
|
|
|
|
usersTyping: [];
|
|
|
|
theme: string;
|
|
|
|
subtitle: string;
|
|
|
|
renderFunc: any;
|
|
|
|
scale: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
type TRoomHeaderHeaderTitle = {
|
|
|
|
title: string;
|
|
|
|
tmid: string;
|
|
|
|
prid: string;
|
|
|
|
scale: number;
|
|
|
|
theme: string;
|
|
|
|
testID: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IRoomHeader {
|
|
|
|
title: string;
|
|
|
|
subtitle: string;
|
|
|
|
type: string;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
prid: string;
|
|
|
|
tmid: string;
|
|
|
|
teamMain: boolean;
|
2022-03-25 20:05:49 +00:00
|
|
|
status: TUserStatus;
|
2022-01-17 16:10:39 +00:00
|
|
|
theme?: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
usersTyping: [];
|
|
|
|
isGroupChat: boolean;
|
|
|
|
parentTitle: string;
|
2022-01-17 16:10:39 +00:00
|
|
|
onPress: () => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
testID: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SubTitle = React.memo(({ usersTyping, subtitle, renderFunc, theme, scale }: TRoomHeaderSubTitle) => {
|
|
|
|
const fontSize = getSubTitleSize(scale);
|
|
|
|
// typing
|
|
|
|
if (usersTyping.length) {
|
|
|
|
let usersText;
|
|
|
|
if (usersTyping.length === 2) {
|
|
|
|
usersText = usersTyping.join(` ${I18n.t('and')} `);
|
|
|
|
} else {
|
|
|
|
usersText = usersTyping.join(', ');
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Text style={[styles.subtitle, { fontSize, color: themes[theme].auxiliaryText }]} numberOfLines={1}>
|
|
|
|
<Text style={styles.typingUsers}>{usersText} </Text>
|
|
|
|
{usersTyping.length > 1 ? I18n.t('are_typing') : I18n.t('is_typing')}...
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// renderFunc
|
|
|
|
if (renderFunc) {
|
|
|
|
return renderFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtitle
|
|
|
|
if (subtitle) {
|
2022-02-17 15:27:01 +00:00
|
|
|
return <MarkdownPreview msg={subtitle} style={[styles.subtitle, { fontSize, color: themes[theme].auxiliaryText }]} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const HeaderTitle = React.memo(({ title, tmid, prid, scale, theme, testID }: TRoomHeaderHeaderTitle) => {
|
|
|
|
const titleStyle = { fontSize: TITLE_SIZE * scale, color: themes[theme].headerTitleColor };
|
|
|
|
if (!tmid && !prid) {
|
|
|
|
return (
|
|
|
|
<Text style={[styles.title, titleStyle]} numberOfLines={1} testID={testID}>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:27:01 +00:00
|
|
|
return <MarkdownPreview msg={title} style={[styles.title, titleStyle]} testID={testID} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const Header = React.memo(
|
|
|
|
({
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
parentTitle,
|
|
|
|
type,
|
|
|
|
status,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
prid,
|
|
|
|
tmid,
|
|
|
|
onPress,
|
|
|
|
theme,
|
|
|
|
isGroupChat,
|
|
|
|
teamMain,
|
|
|
|
testID,
|
|
|
|
usersTyping = []
|
|
|
|
}: IRoomHeader) => {
|
|
|
|
const portrait = height > width;
|
|
|
|
let scale = 1;
|
|
|
|
|
|
|
|
if (!portrait && !tmid) {
|
|
|
|
if (usersTyping.length > 0 || subtitle) {
|
|
|
|
scale = 0.8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let renderFunc;
|
|
|
|
if (tmid) {
|
|
|
|
renderFunc = () => (
|
|
|
|
<View style={styles.titleContainer}>
|
|
|
|
<RoomTypeIcon type={prid ? 'discussion' : type} isGroupChat={isGroupChat} status={status} teamMain={teamMain} />
|
2022-01-17 16:10:39 +00:00
|
|
|
<Text style={[styles.subtitle, { color: themes[theme!].auxiliaryText }]} numberOfLines={1}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{parentTitle}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleOnPress = useCallback(() => onPress(), []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
testID='room-header'
|
|
|
|
accessibilityLabel={title}
|
|
|
|
onPress={handleOnPress}
|
|
|
|
style={styles.container}
|
|
|
|
// @ts-ignore
|
|
|
|
disabled={tmid}
|
|
|
|
hitSlop={HIT_SLOP}>
|
|
|
|
<View style={styles.titleContainer}>
|
|
|
|
{tmid ? null : (
|
|
|
|
<RoomTypeIcon type={prid ? 'discussion' : type} isGroupChat={isGroupChat} status={status} teamMain={teamMain} />
|
|
|
|
)}
|
2022-01-17 16:10:39 +00:00
|
|
|
<HeaderTitle title={title} tmid={tmid} prid={prid} scale={scale} theme={theme!} testID={testID} />
|
2021-09-13 20:41:05 +00:00
|
|
|
</View>
|
2022-01-17 16:10:39 +00:00
|
|
|
<SubTitle
|
|
|
|
usersTyping={tmid ? [] : usersTyping}
|
|
|
|
subtitle={subtitle}
|
|
|
|
theme={theme!}
|
|
|
|
renderFunc={renderFunc}
|
|
|
|
scale={scale}
|
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export default withTheme(Header);
|