2022-11-03 14:08:48 -07:00
|
|
|
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
|
2022-08-16 13:35:06 -07:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-11-03 13:54:18 -07:00
|
|
|
import { useHistory, useParams } from 'react-router-dom';
|
2022-08-16 13:35:06 -07:00
|
|
|
|
|
|
|
import { toggleMainWindow } from 'soapbox/actions/chats';
|
2022-08-17 12:48:04 -07:00
|
|
|
import { useOwnAccount, useSettings } from 'soapbox/hooks';
|
2022-11-02 12:28:16 -07:00
|
|
|
import { IChat, useChat } from 'soapbox/queries/chats';
|
2022-08-16 10:38:17 -07:00
|
|
|
|
2022-08-16 13:35:06 -07:00
|
|
|
type WindowState = 'open' | 'minimized';
|
|
|
|
|
2022-08-16 10:38:17 -07:00
|
|
|
const ChatContext = createContext<any>({
|
2022-08-16 13:35:06 -07:00
|
|
|
isOpen: false,
|
2022-08-17 12:48:04 -07:00
|
|
|
needsAcceptance: false,
|
2022-08-16 10:38:17 -07:00
|
|
|
});
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
enum ChatWidgetScreens {
|
|
|
|
INBOX = 'INBOX',
|
|
|
|
SEARCH = 'SEARCH',
|
|
|
|
CHAT = 'CHAT',
|
|
|
|
CHAT_SETTINGS = 'CHAT_SETTINGS'
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:38:17 -07:00
|
|
|
const ChatProvider: React.FC = ({ children }) => {
|
2022-11-03 12:29:53 -07:00
|
|
|
const history = useHistory();
|
2022-08-16 13:35:06 -07:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const settings = useSettings();
|
2022-08-17 12:48:04 -07:00
|
|
|
const account = useOwnAccount();
|
2022-08-16 13:35:06 -07:00
|
|
|
|
2022-11-03 12:29:53 -07:00
|
|
|
const path = history.location.pathname;
|
|
|
|
const isUsingMainChatPage = Boolean(path.match(/^\/chats/));
|
2022-11-03 13:54:18 -07:00
|
|
|
const { chatId } = useParams<{ chatId: string }>();
|
2022-11-03 12:29:53 -07:00
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
const [screen, setScreen] = useState<ChatWidgetScreens>(ChatWidgetScreens.INBOX);
|
2022-11-03 14:08:48 -07:00
|
|
|
const [currentChatId, setCurrentChatId] = useState<null | string>(chatId);
|
2022-11-02 12:28:16 -07:00
|
|
|
|
2022-11-03 14:08:48 -07:00
|
|
|
const { data: chat } = useChat(currentChatId as string);
|
2022-08-17 06:58:46 -07:00
|
|
|
|
2022-08-16 13:35:06 -07:00
|
|
|
const mainWindowState = settings.getIn(['chats', 'mainWindow']) as WindowState;
|
2022-08-17 12:48:04 -07:00
|
|
|
const needsAcceptance = !chat?.accepted && chat?.created_by_account !== account?.id;
|
2022-08-16 13:35:06 -07:00
|
|
|
const isOpen = mainWindowState === 'open';
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
const changeScreen = (screen: ChatWidgetScreens, currentChatId?: string | null) => {
|
|
|
|
setCurrentChatId(currentChatId || null);
|
|
|
|
setScreen(screen);
|
|
|
|
};
|
|
|
|
|
2022-08-16 13:35:06 -07:00
|
|
|
const toggleChatPane = () => dispatch(toggleMainWindow());
|
2022-08-16 10:38:17 -07:00
|
|
|
|
2022-08-17 12:48:04 -07:00
|
|
|
const value = useMemo(() => ({
|
|
|
|
chat,
|
|
|
|
needsAcceptance,
|
|
|
|
isOpen,
|
2022-11-03 12:29:53 -07:00
|
|
|
isUsingMainChatPage,
|
2022-08-17 12:48:04 -07:00
|
|
|
toggleChatPane,
|
2022-11-02 12:28:16 -07:00
|
|
|
screen,
|
|
|
|
changeScreen,
|
|
|
|
currentChatId,
|
2022-11-03 12:29:53 -07:00
|
|
|
}), [chat, currentChatId, needsAcceptance, isUsingMainChatPage, isOpen, screen, changeScreen]);
|
2022-08-17 12:48:04 -07:00
|
|
|
|
2022-11-03 14:08:48 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (chatId) {
|
|
|
|
setCurrentChatId(chatId);
|
|
|
|
} else {
|
|
|
|
setCurrentChatId(null);
|
|
|
|
}
|
|
|
|
}, [chatId]);
|
|
|
|
|
2022-08-16 10:38:17 -07:00
|
|
|
return (
|
2022-08-17 12:48:04 -07:00
|
|
|
<ChatContext.Provider value={value}>
|
2022-08-17 06:58:46 -07:00
|
|
|
{children}
|
|
|
|
</ChatContext.Provider>
|
2022-08-16 10:38:17 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IChatContext {
|
|
|
|
chat: IChat | null
|
2022-09-09 07:24:25 -07:00
|
|
|
isOpen: boolean
|
2022-11-03 12:29:53 -07:00
|
|
|
isUsingMainChatPage?: boolean
|
2022-08-17 12:48:04 -07:00
|
|
|
needsAcceptance: boolean
|
2022-08-16 13:35:06 -07:00
|
|
|
toggleChatPane(): void
|
2022-11-02 12:28:16 -07:00
|
|
|
screen: ChatWidgetScreens
|
|
|
|
currentChatId: string | null
|
|
|
|
changeScreen(screen: ChatWidgetScreens, currentChatId?: string | null): void
|
2022-08-16 10:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const useChatContext = (): IChatContext => useContext(ChatContext);
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
export { ChatContext, ChatProvider, useChatContext, ChatWidgetScreens };
|