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

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-16 13:35:06 -07:00
import React, { useRef } from 'react';
2022-06-17 12:39:53 -07:00
2022-08-16 05:39:58 -07:00
import { Avatar, HStack, Icon, Stack, Text } from 'soapbox/components/ui';
2022-08-10 05:38:49 -07:00
import VerificationBadge from 'soapbox/components/verification_badge';
2022-08-16 13:35:06 -07:00
import { useChatContext } from 'soapbox/contexts/chat-context';
2022-06-17 12:39:53 -07:00
import ChatBox from './chat-box';
2022-08-10 05:38:49 -07:00
import ChatPaneHeader from './chat-pane-header';
2022-08-17 06:58:46 -07:00
import ChatSettings from './chat-settings';
2022-06-17 12:39:53 -07:00
/** Floating desktop chat window. */
2022-08-16 13:35:06 -07:00
const ChatWindow = () => {
2022-08-17 12:48:04 -07:00
const { chat, setChat, isOpen, isEditing, needsAcceptance, setEditing, toggleChatPane } = useChatContext();
2022-08-16 13:35:06 -07:00
const inputRef = useRef<HTMLTextAreaElement>();
const closeChat = () => setChat(null);
2022-08-17 06:58:46 -07:00
2022-08-16 13:35:06 -07:00
const openAndFocusChat = () => {
toggleChatPane();
inputRef.current?.focus();
};
2022-08-17 06:58:46 -07:00
const openChatSettings = () => setEditing(true);
2022-08-17 12:48:04 -07:00
const secondaryAction = () => {
if (needsAcceptance) {
return undefined;
}
return isOpen ? openChatSettings : openAndFocusChat;
};
2022-06-17 12:39:53 -07:00
if (!chat) return null;
2022-08-17 06:58:46 -07:00
if (isEditing) {
return <ChatSettings />;
}
2022-06-17 12:39:53 -07:00
return (
2022-08-10 05:38:49 -07:00
<>
<ChatPaneHeader
title={
<HStack alignItems='center' space={2}>
2022-08-16 13:35:06 -07:00
{isOpen && (
<button onClick={closeChat}>
<Icon
src={require('@tabler/icons/arrow-left.svg')}
className='h-6 w-6 text-gray-600 dark:text-gray-400'
/>
</button>
)}
2022-08-10 05:38:49 -07:00
<HStack alignItems='center' space={3}>
2022-08-16 13:35:06 -07:00
{isOpen && (
<Avatar src={chat.account.avatar} size={40} />
)}
2022-08-10 05:38:49 -07:00
<Stack alignItems='start'>
<div className='flex items-center space-x-1 flex-grow'>
2022-08-17 06:58:46 -07:00
<Text size='sm' weight='bold' truncate>{chat.account.display_name}</Text>
2022-08-10 05:38:49 -07:00
{chat.account.verified && <VerificationBadge />}
</div>
2022-08-17 06:58:46 -07:00
<Text size='sm' weight='medium' theme='primary' truncate>@{chat.account.acct}</Text>
2022-08-10 05:38:49 -07:00
</Stack>
</HStack>
</HStack>
}
2022-08-17 12:48:04 -07:00
secondaryAction={secondaryAction()}
2022-08-17 06:58:46 -07:00
secondaryActionIcon={isOpen ? require('@tabler/icons/info-circle.svg') : require('@tabler/icons/edit.svg')}
2022-08-16 13:35:06 -07:00
isToggleable={!isOpen}
isOpen={isOpen}
onToggle={toggleChatPane}
2022-08-10 05:38:49 -07:00
/>
<Stack className='overflow-hidden flex-grow h-full' space={2}>
2022-08-16 13:35:06 -07:00
<ChatBox chat={chat} inputRef={inputRef as any} onSetInputRef={() => null} />
2022-08-10 05:38:49 -07:00
</Stack>
</>
2022-06-17 12:39:53 -07:00
);
};
export default ChatWindow;