// SPDX-License-Identifier: AGPL-3.0-or-later import Authentication from '@app/features/auth/state/Authentication'; import {Typing} from '@app/features/channel/components/ChannelTyping'; import styles from '@app/features/channel/components/TypingUsers.module.css'; import type {Channel} from '@app/features/channel/models/Channel'; import DeveloperOptions from '@app/features/devtools/state/DeveloperOptions'; import GuildMembers from '@app/features/member/state/GuildMembers'; import Relationships from '@app/features/relationship/state/Relationships'; import messageStyles from '@app/features/theme/styles/Message.module.css'; import TypingIndicator from '@app/features/typing/legacy/LegacyTypingIndicator'; import {getTypingTierText} from '@app/features/typing/utils/TypingTierText'; import {AvatarStack} from '@app/features/ui/avatars/AvatarStack'; import type {User} from '@app/features/user/models/User'; import Users from '@app/features/user/state/Users'; import * as NicknameUtils from '@app/features/user/utils/NicknameUtils'; import type {I18n} from '@lingui/core'; import {Trans, useLingui} from '@lingui/react/macro'; import {observer} from 'mobx-react-lite'; const getDisplayName = (user: User, guildId?: string | null) => NicknameUtils.getNickname(user, guildId ?? null); export const getTypingText = (i18n: I18n, typingUsers: ReadonlyArray, channel: Channel) => { const [a, b, c] = typingUsers.map((user) => { const member = GuildMembers.getMember(channel.guildId ?? '', user.id); return ( {getDisplayName(user, channel.guildId)} ); }); if (typingUsers.length === 1) { return {a} is typing...; } if (typingUsers.length === 2) { return ( {a} and {b} are typing... ); } if (typingUsers.length === 3) { return ( {a}, {b} and {c} are typing... ); } return getTypingTierText(i18n, typingUsers.length); }; const EMPTY_TYPING_USER_RECORDS: ReadonlyArray = Object.freeze([]); export const usePresentableTypingUsers = (channel: Channel): ReadonlyArray => { const typingUserIds = TypingIndicator.getTypingUsers(channel.id); if (typingUserIds.length === 0) return EMPTY_TYPING_USER_RECORDS; const currentUserId = Authentication.currentUserId; const showSelf = DeveloperOptions.showMyselfTyping; const result: Array = []; for (const userId of typingUserIds) { if (!showSelf && userId === currentUserId) continue; if (Relationships.isBlocked(userId)) continue; const user = Users.getUser(userId); if (user) result.push(user); } if (result.length === 0) return EMPTY_TYPING_USER_RECORDS; return result; }; const AVATAR_THRESHOLD = 5; export const TypingUsers = observer( ({channel, withText = true, showAvatars = true}: {channel: Channel; withText?: boolean; showAvatars?: boolean}) => { const {i18n} = useLingui(); const typingUsers = usePresentableTypingUsers(channel); if (typingUsers.length === 0) { return null; } return (
{withText && ( <> {showAvatars && ( )} {getTypingText(i18n, typingUsers, channel)} )}
); }, );