Files
cinny/src/app/features/room/RoomViewTyping.tsx
T
litruv b85a2b9290
Checks / checks (push) Successful in 2m6s
Trigger cinny-mobile / dispatch (push) Successful in 1s
Fix the reaction content type and the fixable lint sites. [skip build]
2026-11-02 04:21:58 +11:00

101 lines
3.4 KiB
TypeScript

import React from 'react';
import { Box, IconButton, Text, as } from 'folds';
import { Room } from 'matrix-js-sdk';
import classNames from 'classnames';
import { useSetAtom } from 'jotai';
import { Icon, Icons } from '../../components/icons';
import { roomIdToTypingMembersAtom } from '../../state/typingMembers';
import { TypingIndicator } from '../../components/typing-indicator';
import { getMemberDisplayName } from '../../utils/room';
import { getMxIdLocalPart } from '../../utils/matrix';
import * as css from './RoomViewTyping.css';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useRoomTypingMember } from '../../hooks/useRoomTypingMembers';
import { useTypingVerbs } from '../../hooks/useUserTypingVerb';
const DEFAULT_VERB = 'typing';
export type RoomViewTypingProps = {
room: Room;
};
export const RoomViewTyping = as<'div', RoomViewTypingProps>(
({ className, room, ...props }, ref) => {
const setTypingMembers = useSetAtom(roomIdToTypingMembersAtom);
const mx = useMatrixClient();
const typingMembers = useRoomTypingMember(room.roomId);
const typers = typingMembers
.filter((receipt) => receipt.userId !== mx.getUserId())
.map((receipt) => ({
userId: receipt.userId,
name: getMemberDisplayName(room, receipt.userId) ?? getMxIdLocalPart(receipt.userId),
}))
.reverse();
const verbs = useTypingVerbs(typers.map((typer) => typer.userId));
if (typers.length === 0) {
return null;
}
const entries = typers.slice(0, 3).map((typer) => ({
name: typer.name,
verb: verbs[typer.userId],
plural: false,
}));
if (typers.length > 3) {
entries.push({ name: `${typers.length - 3} others`, verb: undefined, plural: true });
}
// One verb for everyone keeps the joined form; mixed verbs need a clause per person
const firstVerb = verbs[typers[0].userId];
const sameVerb = typers.every((typer) => verbs[typer.userId] === firstVerb);
const clauses = entries.map((entry, index) => ({
key: entry.plural ? 'others' : typers[index].userId,
name: entry.name,
suffix: `${entry.plural ? 'are' : 'is'} ${entry.verb ?? DEFAULT_VERB}`,
}));
const sharedSuffix = sameVerb
? ` ${entries.length > 1 ? 'are' : 'is'} ${firstVerb ?? DEFAULT_VERB}`
: undefined;
const handleDropAll = () => {
// some homeserver does not timeout typing status
// we have given option so user can drop their typing status
typingMembers.forEach((receipt) =>
setTypingMembers({
type: 'DELETE',
roomId: room.roomId,
userId: receipt.userId,
}),
);
};
return (
<Box
className={classNames(css.RoomViewTyping, className)}
alignItems="Center"
gap="400"
{...props}
ref={ref}
>
<TypingIndicator />
<Text className={css.TypingText} size="T300" truncate>
{clauses.map((clause, index) => (
<React.Fragment key={clause.key}>
{index > 0 && (index === clauses.length - 1 ? ' and ' : ', ')}
<b>{clause.name}</b>
{!sharedSuffix && ` ${clause.suffix}`}
</React.Fragment>
))}
{sharedSuffix}
...
</Text>
<IconButton title="Drop Typing Status" size="300" radii="Pill" onClick={handleDropAll}>
<Icon size="50" src={Icons.Cross} />
</IconButton>
</Box>
);
},
);