2022-09-19 13:12:18 -07:00
|
|
|
import classNames from 'clsx';
|
2022-09-22 11:03:12 -07:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-09-28 17:26:49 -07:00
|
|
|
import { Route, Switch } from 'react-router-dom';
|
2022-09-13 08:18:46 -07:00
|
|
|
|
2022-09-22 11:03:12 -07:00
|
|
|
import { Stack } from 'soapbox/components/ui';
|
2022-09-19 13:12:18 -07:00
|
|
|
import { useChatContext } from 'soapbox/contexts/chat-context';
|
2022-10-31 09:14:22 -07:00
|
|
|
import { useOwnAccount } from 'soapbox/hooks';
|
2022-09-28 13:38:05 -07:00
|
|
|
import { useChat } from 'soapbox/queries/chats';
|
2022-09-13 08:18:46 -07:00
|
|
|
|
2022-09-16 07:22:43 -07:00
|
|
|
import ChatPageMain from './components/chat-page-main';
|
2022-09-28 17:26:49 -07:00
|
|
|
import ChatPageNew from './components/chat-page-new';
|
2022-09-16 07:22:43 -07:00
|
|
|
import ChatPageSidebar from './components/chat-page-sidebar';
|
2022-09-28 17:39:22 -07:00
|
|
|
import Welcome from './components/welcome';
|
2022-09-13 08:18:46 -07:00
|
|
|
|
2022-09-28 13:38:05 -07:00
|
|
|
interface IChatPage {
|
|
|
|
chatId?: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChatPage: React.FC<IChatPage> = ({ chatId }) => {
|
2022-10-31 09:14:22 -07:00
|
|
|
const account = useOwnAccount();
|
|
|
|
const isOnboarded = account?.chats_onboarded;
|
|
|
|
|
2022-09-28 13:38:05 -07:00
|
|
|
const { chat, setChat } = useChatContext();
|
|
|
|
const { chat: chatQueryResult } = useChat(chatId);
|
2022-09-19 13:12:18 -07:00
|
|
|
|
2022-09-22 11:03:12 -07:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const [height, setHeight] = useState<string | number>('100%');
|
|
|
|
|
|
|
|
const calculateHeight = () => {
|
|
|
|
if (!containerRef.current) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { top } = containerRef.current.getBoundingClientRect();
|
|
|
|
const fullHeight = document.body.offsetHeight;
|
|
|
|
|
2022-09-22 15:38:00 -07:00
|
|
|
// On mobile, account for bottom navigation.
|
|
|
|
const offset = document.body.clientWidth < 976 ? -61 : 0;
|
|
|
|
|
|
|
|
setHeight(fullHeight - top + offset);
|
2022-09-22 11:03:12 -07:00
|
|
|
};
|
|
|
|
|
2022-09-28 13:38:05 -07:00
|
|
|
useEffect(() => {
|
|
|
|
const data = chatQueryResult?.data;
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
setChat(data);
|
|
|
|
}
|
|
|
|
}, [chatQueryResult?.isLoading]);
|
|
|
|
|
2022-09-22 11:03:12 -07:00
|
|
|
useEffect(() => {
|
|
|
|
calculateHeight();
|
|
|
|
}, [containerRef.current]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('resize', calculateHeight);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('resize', calculateHeight);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2022-09-13 08:18:46 -07:00
|
|
|
return (
|
2022-09-22 11:03:12 -07:00
|
|
|
<div
|
|
|
|
ref={containerRef}
|
|
|
|
style={{ height }}
|
|
|
|
className='h-screen bg-white dark:bg-primary-900 text-gray-900 dark:text-gray-100 shadow-lg dark:shadow-none overflow-hidden sm:rounded-t-xl'
|
|
|
|
>
|
2022-10-31 09:14:22 -07:00
|
|
|
{isOnboarded ? (
|
|
|
|
<div className='grid grid-cols-9 overflow-hidden h-full dark:divide-x-2 dark:divide-solid dark:divide-gray-800'>
|
|
|
|
<Stack
|
|
|
|
className={classNames('col-span-9 sm:col-span-3 bg-gradient-to-r from-white to-gray-100 dark:bg-gray-900 dark:bg-none overflow-hidden dark:inset', {
|
|
|
|
'hidden sm:block': chat,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<ChatPageSidebar />
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<Stack className={classNames('col-span-9 sm:col-span-6 h-full overflow-hidden', {
|
|
|
|
'hidden sm:block': !chat,
|
2022-09-19 13:12:18 -07:00
|
|
|
})}
|
2022-10-31 09:14:22 -07:00
|
|
|
>
|
|
|
|
<Switch>
|
|
|
|
<Route path='/chats/new'>
|
|
|
|
<ChatPageNew />
|
|
|
|
</Route>
|
|
|
|
<Route path='/chats/settings'>
|
|
|
|
<Welcome />
|
|
|
|
</Route>
|
|
|
|
<Route>
|
|
|
|
<ChatPageMain />
|
|
|
|
</Route>
|
|
|
|
</Switch>
|
|
|
|
</Stack>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<Welcome />
|
|
|
|
)}
|
2022-09-22 11:03:12 -07:00
|
|
|
</div>
|
2022-09-13 08:18:46 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-13 11:11:22 -07:00
|
|
|
export default ChatPage;
|