Rocket.Chat.ReactNative/app/containers/MessageComposer/components/SendThreadToChannel.tsx

98 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-01-25 14:11:07 +00:00
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
import { StyleSheet, Text } from 'react-native';
import React, { useEffect, useRef } from 'react';
import { Subscription } from 'rxjs';
import { Q } from '@nozbe/watermelondb';
import { useRoomContext } from '../../../views/RoomView/context';
import { useAlsoSendThreadToChannel, useMessageComposerApi, useShowEmojiSearchbar } from '../context';
import { CustomIcon } from '../../CustomIcon';
import { useTheme } from '../../../theme';
import sharedStyles from '../../../views/Styles';
import I18n from '../../../i18n';
import { useAppSelector } from '../../../lib/hooks';
import database from '../../../lib/database';
import { compareServerVersion } from '../../../lib/methods/helpers';
export const SendThreadToChannel = (): React.ReactElement | null => {
const alsoSendThreadToChannel = useAlsoSendThreadToChannel();
const { setAlsoSendThreadToChannel } = useMessageComposerApi();
const showEmojiSearchbar = useShowEmojiSearchbar();
const { tmid } = useRoomContext();
const { colors } = useTheme();
const subscription = useRef<Subscription>();
const alsoSendThreadToChannelUserPref = useAppSelector(state => state.login.user.alsoSendThreadToChannel);
const serverVersion = useAppSelector(state => state.server.version);
useEffect(() => {
if (!tmid) {
return;
}
if (compareServerVersion(serverVersion, 'lowerThan', '5.0.0')) {
setAlsoSendThreadToChannel(false);
return;
}
if (alsoSendThreadToChannelUserPref === 'always') {
setAlsoSendThreadToChannel(true);
return;
}
if (alsoSendThreadToChannelUserPref === 'never') {
setAlsoSendThreadToChannel(false);
return;
}
/**
* "default" sends a to channel only in the first message of the thread.
* We check if the thread exists by observing/subscribing to the query with tmid.
* If it doesn't exist, it means that this is the first message of the thread. So it's true.
* Otherwise, it's false.
* */
if (alsoSendThreadToChannelUserPref === 'default') {
const db = database.active;
const observable = db.get('threads').query(Q.where('id', tmid)).observe();
2024-01-25 14:11:07 +00:00
subscription.current = observable.subscribe(result => {
setAlsoSendThreadToChannel(!result.length);
});
}
return () => {
subscription.current?.unsubscribe();
};
}, [tmid, alsoSendThreadToChannelUserPref, serverVersion, setAlsoSendThreadToChannel]);
if (!tmid || showEmojiSearchbar) {
return null;
}
return (
<TouchableWithoutFeedback
style={styles.container}
onPress={() => setAlsoSendThreadToChannel(!alsoSendThreadToChannel)}
testID='message-composer-send-to-channel'
>
<CustomIcon
testID={alsoSendThreadToChannel ? 'send-to-channel-checked' : 'send-to-channel-unchecked'}
name={alsoSendThreadToChannel ? 'checkbox-checked' : 'checkbox-unchecked'}
size={24}
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
color={alsoSendThreadToChannel ? colors.buttonBackgroundPrimaryDefault : colors.fontDefault}
2024-01-25 14:11:07 +00:00
/>
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
<Text style={[styles.text, { color: colors.fontDefault }]}>{I18n.t('Message_composer_Send_to_channel')}</Text>
2024-01-25 14:11:07 +00:00
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 12
},
text: {
fontSize: 14,
marginLeft: 8,
...sharedStyles.textRegular
}
});