Skip to content

Commit

Permalink
Send Typing Start
Browse files Browse the repository at this point in the history
  • Loading branch information
theADAMJR committed Aug 13, 2021
1 parent 0f3f44a commit 033cb45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions frontend/src/components/channel/message-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = (props) => {
const dispatch = useDispatch();
const [content, setContent] = useState(props.content ?? '');
const channel = useSelector((s: Store.AppStore) => s.ui.activeChannel)!;
const typing = useSelector(getTypersInChannel(channel.id));
const typers = useSelector(getTypersInChannel(channel.id));

const onKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
handleEscape(event);
Expand Down Expand Up @@ -46,8 +46,17 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = (props) => {

const user = (userId: string) => getUser(userId)(store.getState());

const typingUsers = () => typing.map(t =>
<span className="text-xs py-2">{user(t.userId)!.username} is typing</span>);
const typingMessage = () => {
const maxTypers = 3;
const typingUsers = typers.map(t => user(t.userId)!.username).join(', ');

if (!typers.length) return;

return (typers.length > maxTypers)
? 'Many users are typing'
: `${typingUsers} is typing`
}


return (
<div className={`${props.editingMessageId ? 'mt-2' : 'px-4'}`}>
Expand All @@ -61,7 +70,7 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = (props) => {
autoFocus />
{(props.editingMessageId)
? <span className="text-xs py-2">escape to cancel • enter to save</span>
: <div className="w-full h-6">{typingUsers}</div>}
: <div className="w-full h-6">{typingMessage()}</div>}
</div>
);
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const getTypersInChannel = (channelId: string) => createSelector<any, any
let lastTypedAt: Date;

export const startTyping = (channelId: string) => (dispatch, getState) => {
const minsAgo = moment(lastTypedAt).diff(new Date(), 'minutes');
if (lastTypedAt && minsAgo < 5) return;
const secsAgo = moment(lastTypedAt).diff(new Date(), 'seconds');
if (lastTypedAt && secsAgo < 5) return;

lastTypedAt = new Date();

Expand Down

0 comments on commit 033cb45

Please sign in to comment.