diff options
author | Ivan Chen <ivan@tagg.id> | 2021-04-08 20:41:54 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-08 20:41:54 -0400 |
commit | fb5cca5bd8aff7232c2ab5e01df0e79dddbef504 (patch) | |
tree | 8a74ae256bc826d2338061fdd605db67c1ec350b /src/utils/messages.ts | |
parent | 4cf3bc720ebcc0b16d158caf60fbdf091621c327 (diff) | |
parent | 98a31b59df5b51ea9488220d47bd7d60b3a268b9 (diff) |
Merge pull request #355 from IvanIFChen/tma769-styling-channel-list-screen
[TMA-769] Styling chat list screen
Diffstat (limited to 'src/utils/messages.ts')
-rw-r--r-- | src/utils/messages.ts | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/utils/messages.ts b/src/utils/messages.ts new file mode 100644 index 00000000..d63f2b7a --- /dev/null +++ b/src/utils/messages.ts @@ -0,0 +1,83 @@ +import moment from 'moment'; +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; +}; |