pleroma/app/soapbox/features/chats/components/chat-window.tsx

121 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-06-17 12:39:53 -07:00
import React, { useEffect, useRef } from 'react';
import { defineMessages, useIntl } from 'react-intl';
2022-06-17 12:39:53 -07:00
import { Link } from 'react-router-dom';
import {
closeChat,
toggleChat,
} from 'soapbox/actions/chats';
import Avatar from 'soapbox/components/avatar';
2022-11-15 12:46:23 -08:00
import HoverRefWrapper from 'soapbox/components/hover-ref-wrapper';
2022-11-15 06:11:30 -08:00
import IconButton from 'soapbox/components/icon-button';
import { HStack, Counter } from 'soapbox/components/ui';
2022-06-17 12:39:53 -07:00
import { useAppSelector, useAppDispatch } from 'soapbox/hooks';
import { makeGetChat } from 'soapbox/selectors';
import { getAcct } from 'soapbox/utils/accounts';
import { displayFqn as getDisplayFqn } from 'soapbox/utils/state';
import ChatBox from './chat-box';
2022-06-17 12:39:53 -07:00
import type { Account as AccountEntity } from 'soapbox/types/entities';
const messages = defineMessages({
close: { id: 'chat_window.close', defaultMessage: 'Close chat' },
});
2022-06-17 12:39:53 -07:00
type WindowState = 'open' | 'minimized';
const getChat = makeGetChat();
interface IChatWindow {
/** Position of the chat window on the screen, where 0 is rightmost. */
idx: number,
/** ID of the chat entity. */
chatId: string,
/** Whether the window is open or minimized. */
windowState: WindowState,
}
/** Floating desktop chat window. */
const ChatWindow: React.FC<IChatWindow> = ({ idx, chatId, windowState }) => {
const intl = useIntl();
2022-06-17 12:39:53 -07:00
const dispatch = useAppDispatch();
const displayFqn = useAppSelector(getDisplayFqn);
const chat = useAppSelector(state => {
const chat = state.chats.items.get(chatId);
return chat ? getChat(state, chat.toJS() as any) : undefined;
});
const inputElem = useRef<HTMLTextAreaElement | null>(null);
const handleChatClose = (chatId: string) => {
return () => {
dispatch(closeChat(chatId));
};
};
const handleChatToggle = (chatId: string) => {
return () => {
dispatch(toggleChat(chatId));
};
};
const handleInputRef = (el: HTMLTextAreaElement) => {
inputElem.current = el;
};
const focusInput = () => {
inputElem.current?.focus();
};
useEffect(() => {
2022-06-18 14:19:22 -07:00
if (windowState === 'open') {
focusInput();
}
}, [windowState]);
2022-06-17 12:39:53 -07:00
if (!chat) return null;
const account = chat.account as unknown as AccountEntity;
const right = (285 * (idx + 1)) + 20;
const unreadCount = chat.unread;
const unreadIcon = (
<div className='mr-2 flex-none'>
<Counter count={unreadCount} />
</div>
);
const avatar = (
<HoverRefWrapper accountId={account.id}>
<Link to={`/@${account.acct}`}>
<Avatar account={account} size={18} />
</Link>
</HoverRefWrapper>
);
return (
<div className={`pane pane--${windowState}`} style={{ right: `${right}px` }}>
<HStack space={2} className='pane__header'>
2022-06-17 12:39:53 -07:00
{unreadCount > 0 ? unreadIcon : avatar }
<button className='pane__title' onClick={handleChatToggle(chat.id)}>
@{getAcct(account, displayFqn)}
</button>
<div className='pane__close'>
<IconButton src={require('@tabler/icons/x.svg')} title={intl.formatMessage(messages.close)} onClick={handleChatClose(chat.id)} />
2022-06-17 12:39:53 -07:00
</div>
</HStack>
2022-06-17 12:39:53 -07:00
<div className='pane__content'>
<ChatBox
chatId={chat.id}
onSetInputRef={handleInputRef}
/>
</div>
</div>
);
};
export default ChatWindow;