Rocket.Chat.ReactNative/app/views/StatusView/index.tsx

176 lines
4.9 KiB
TypeScript
Raw Normal View History

import { useNavigation } from '@react-navigation/native';
import React, { useEffect, useState } from 'react';
import { FlatList, StyleSheet } from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import { setUser } from '../../actions/login';
import * as HeaderButton from '../../containers/HeaderButton';
import * as List from '../../containers/List';
import { sendLoadingEvent } from '../../containers/Loading';
import SafeAreaView from '../../containers/SafeAreaView';
import StatusIcon from '../../containers/Status/Status';
import { FormTextInput } from '../../containers/TextInput';
import { IApplicationState, TUserStatus } from '../../definitions';
import I18n from '../../i18n';
import { showToast } from '../../lib/methods/helpers/showToast';
import { Services } from '../../lib/services';
import { getUserSelector } from '../../selectors/login';
import { showErrorAlert } from '../../lib/methods/helpers';
import log, { events, logEvent } from '../../lib/methods/helpers/log';
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
import { useTheme } from '../../theme';
interface IStatus {
id: TUserStatus;
name: string;
}
const STATUS: IStatus[] = [
{
id: 'online',
name: 'Online'
},
{
id: 'busy',
name: 'Busy'
},
{
id: 'away',
name: 'Away'
},
{
id: 'offline',
name: 'Invisible'
}
];
const styles = StyleSheet.create({
inputContainer: {
marginTop: 16,
marginBottom: 16
},
inputLeft: {
position: 'absolute',
left: 12
},
inputStyle: {
paddingLeft: 48,
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
borderRadius: 0,
borderTopWidth: 1,
borderBottomWidth: 1
}
});
const Status = ({
statusType,
status,
setStatus
}: {
statusType: IStatus;
status: TUserStatus;
setStatus: (status: TUserStatus) => void;
}) => {
const { id, name } = statusType;
return (
<List.Item
title={name}
onPress={() => {
const key = `STATUS_${statusType.id.toUpperCase()}` as keyof typeof events;
logEvent(events[key]);
if (status !== statusType.id) {
setStatus(statusType.id);
}
}}
testID={`status-view-${id}`}
left={() => <StatusIcon size={24} status={statusType.id} />}
/>
);
};
const StatusView = (): React.ReactElement => {
const user = useSelector((state: IApplicationState) => getUserSelector(state));
const isMasterDetail = useSelector((state: IApplicationState) => state.app.isMasterDetail);
const Accounts_AllowInvisibleStatusOption = useSelector(
(state: IApplicationState) => state.settings.Accounts_AllowInvisibleStatusOption
);
const [statusText, setStatusText] = useState(user.statusText || '');
const [status, setStatus] = useState(user.status);
const dispatch = useDispatch();
const { setOptions, goBack } = useNavigation();
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
const { colors } = useTheme();
useEffect(() => {
const submit = async () => {
logEvent(events.STATUS_DONE);
if (statusText !== user.statusText || status !== user.status) {
await setCustomStatus(status, statusText);
}
goBack();
};
const setHeader = () => {
setOptions({
title: I18n.t('Edit_Status'),
headerLeft: isMasterDetail ? undefined : () => <HeaderButton.CancelModal onPress={goBack} />,
headerRight: () => (
<HeaderButton.Container>
<HeaderButton.Item title={I18n.t('Save')} onPress={submit} testID='status-view-submit' />
</HeaderButton.Container>
)
});
};
setHeader();
}, [statusText, status]);
const setCustomStatus = async (status: TUserStatus, statusText: string) => {
sendLoadingEvent({ visible: true });
try {
2022-07-06 20:05:13 +00:00
await Services.setUserStatus(status, statusText);
dispatch(setUser({ statusText, status }));
2022-07-06 20:05:13 +00:00
logEvent(events.STATUS_CUSTOM);
showToast(I18n.t('Status_saved_successfully'));
} catch (e: any) {
const messageError =
2022-07-06 20:05:13 +00:00
e.error && e.error.includes('[error-too-many-requests]')
? I18n.t('error-too-many-requests', { seconds: e.reason.replace(/\D/g, '') })
: e.reason;
logEvent(events.STATUS_CUSTOM_F);
showErrorAlert(messageError);
log(e);
}
sendLoadingEvent({ visible: false });
};
const statusType = Accounts_AllowInvisibleStatusOption ? STATUS : STATUS.filter(s => s.id !== 'offline');
return (
<SafeAreaView testID='status-view'>
<FlatList
data={statusType}
keyExtractor={item => item.id}
renderItem={({ item }) => <Status statusType={item} status={status} setStatus={setStatus} />}
ListHeaderComponent={
<>
<FormTextInput
value={statusText}
containerStyle={styles.inputContainer}
onChangeText={text => setStatusText(text)}
left={<StatusIcon testID={`status-view-current-${status}`} style={styles.inputLeft} status={status} size={24} />}
inputStyle={styles.inputStyle}
placeholder={I18n.t('What_are_you_doing_right_now')}
testID='status-view-input'
/>
<List.Separator />
</>
}
ListFooterComponent={List.Separator}
ItemSeparatorComponent={List.Separator}
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
style={{ backgroundColor: colors.surfaceTint }}
/>
</SafeAreaView>
);
};
export default StatusView;