diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/index.ts | 1 | ||||
| -rw-r--r-- | src/utils/layouts.ts | 1 | ||||
| -rw-r--r-- | src/utils/messages.ts | 106 | ||||
| -rw-r--r-- | src/utils/users.ts | 12 |
4 files changed, 117 insertions, 3 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts index 739e6fb8..4ff9afac 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,3 +4,4 @@ export * from './common'; export * from './users'; export * from './friends'; export * from './search'; +export * from './messages'; diff --git a/src/utils/layouts.ts b/src/utils/layouts.ts index e2f1f0b1..4d0d557d 100644 --- a/src/utils/layouts.ts +++ b/src/utils/layouts.ts @@ -31,6 +31,7 @@ export const StatusBarHeight = Platform.select({ }); export const AvatarHeaderHeight = (HeaderHeight + StatusBarHeight) * 1.3; +export const ChatHeaderHeight = (HeaderHeight + StatusBarHeight) * 1.1; /** * This is a function for normalizing the font size for different devices, based on iphone 8. diff --git a/src/utils/messages.ts b/src/utils/messages.ts new file mode 100644 index 00000000..b2162d34 --- /dev/null +++ b/src/utils/messages.ts @@ -0,0 +1,106 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import moment from 'moment'; +import {updateChatClientReady} from '../store/actions'; +import {AppDispatch} from '../store/configureStore'; +import {RootState} from '../store/rootReducer'; +import {ChannelGroupedType} from '../types'; + +/** + * Finds the difference in time in minutes + * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z" + * @returns diff in minutes + */ +const _diffInMinutes = (lastActive: string | undefined) => { + if (!lastActive) { + return undefined; + } + return moment().diff(moment(lastActive), 'minutes'); +}; + +/** + * Formats the last activity status. + * - "Active now" (≤ 5 minutes) + * - "Seen X minutes ago" (5 > x ≥ 59 minutes) + * - "Seen X hours ago" (x = [1, 2]) + * - "Offline" + * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z" + * @returns + */ +export const formatLastSeenText = (lastActive: string | undefined) => { + const diff = _diffInMinutes(lastActive); + if (!diff) { + return 'Offline'; + } + if (diff <= 5) { + return 'Active now'; + } + if (diff <= 59) { + return `Seen ${diff} minutes ago`; + } + if (diff <= 180) { + const hours = Math.floor(diff / 60); + return `Seen ${hours} hours ago`; + } + return 'Offline'; +}; + +/** + * Checks if a lastActive timestamp is considered Online or not. + * + * A user is online if last active is ≤ 15 minutes. + * + * @param lastActive given time e.g. "2021-04-08T19:07:09.361300983Z" + * @returns True if active + */ +export const isOnline = (lastActive: string | undefined) => { + if (!lastActive) { + return false; + } + const diff = _diffInMinutes(lastActive); + if (!diff) { + return false; + } + return diff <= 15; +}; + +/** + * Gets the other member in the channel. + * @param channel the current chat channel + * @param state the current redux state + * @returns other member or undefined + */ +export const getMember = ( + channel: ChannelGroupedType | undefined, + state: RootState, +) => { + if (!channel) { + return undefined; + } + const loggedInUserId = state.user.user.userId; + const otherMembers = channel + ? Object.values(channel.state.members).filter( + (member) => member.user?.id !== loggedInUserId, + ) + : []; + return otherMembers.length === 1 ? otherMembers[0] : undefined; +}; + +export const connectChatAccount = async ( + loggedInUserId: string, + chatClient, + dispatch: AppDispatch, +) => { + try { + const chatToken = await AsyncStorage.getItem('chatToken'); + await chatClient.connectUser( + { + id: loggedInUserId, + }, + chatToken, + ); + dispatch(updateChatClientReady(true)); + } catch (err) { + dispatch(updateChatClientReady(false)); + console.log('Error while connecting user to Stream: ', err); + } +}; diff --git a/src/utils/users.ts b/src/utils/users.ts index 22c1c1f0..ec09198d 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -12,18 +12,17 @@ import { logout, } from '../store/actions'; import {NO_SOCIAL_ACCOUNTS} from '../store/initialStates'; -import {userLoggedIn} from '../store/reducers'; import {loadUserMomentCategories} from './../store/actions/momentCategories'; import {loadUserX} from './../store/actions/userX'; import {AppDispatch} from './../store/configureStore'; import {RootState} from './../store/rootReducer'; import { ProfilePreviewType, - CategoryPreviewType, ProfileInfoType, ScreenType, UserType, } from './../types/types'; +import {connectChatAccount} from './messages'; const loadData = async (dispatch: AppDispatch, user: UserType) => { await Promise.all([ @@ -44,7 +43,11 @@ const loadData = async (dispatch: AppDispatch, user: UserType) => { * @param dispatch This is the dispatch object from the redux store * @param user The user if at all any */ -export const userLogin = async (dispatch: AppDispatch, user: UserType) => { +export const userLogin = async ( + dispatch: AppDispatch, + user: UserType, + chatClient?, +) => { try { let localUser = {...user}; if (!user.userId) { @@ -64,6 +67,9 @@ export const userLogin = async (dispatch: AppDispatch, user: UserType) => { AsyncStorage.setItem('username', user.username), ]); } + if (chatClient) { + connectChatAccount(localUser.userId, chatClient, dispatch); + } await loadData(dispatch, localUser); } catch (error) { console.log(error); |
