2022-08-17 12:48:04 -07:00
|
|
|
import React, { createContext, useContext, useMemo, useState } from 'react';
|
2022-08-16 13:35:06 -07:00
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
|
|
|
import { toggleMainWindow } from 'soapbox/actions/chats';
|
2022-08-17 12:48:04 -07:00
|
|
|
import { useOwnAccount, useSettings } from 'soapbox/hooks';
|
2022-08-16 10:38:17 -07:00
|
|
|
|
|
|
|
import type { IChat } from 'soapbox/queries/chats';
|
|
|
|
|
2022-08-16 13:35:06 -07:00
|
|
|
type WindowState = 'open' | 'minimized';
|
|
|
|
|
2022-08-16 10:38:17 -07:00
|
|
|
const ChatContext = createContext<any>({
|
|
|
|
chat: null,
|
2022-08-16 13:35:06 -07:00
|
|
|
isOpen: false,
|
2022-08-17 06:58:46 -07:00
|
|
|
isEditing: false,
|
2022-08-17 12:48:04 -07:00
|
|
|
needsAcceptance: false,
|
2022-08-16 10:38:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const ChatProvider: React.FC = ({ children }) => {
|
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-08-17 06:58:46 -07:00
|
|
|
const [chat, setChat] = useState<IChat | null>(null);
|
|
|
|
const [isEditing, setEditing] = useState<boolean>(false);
|
2022-09-09 07:24:25 -07:00
|
|
|
const [isSearching, setSearching] = useState<boolean>(false);
|
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';
|
|
|
|
|
|
|
|
const toggleChatPane = () => dispatch(toggleMainWindow());
|
2022-08-16 10:38:17 -07:00
|
|
|
|
2022-08-17 12:48:04 -07:00
|
|
|
const value = useMemo(() => ({
|
|
|
|
chat,
|
|
|
|
setChat,
|
|
|
|
needsAcceptance,
|
|
|
|
isOpen,
|
|
|
|
isEditing,
|
2022-09-09 07:24:25 -07:00
|
|
|
isSearching,
|
2022-08-17 12:48:04 -07:00
|
|
|
setEditing,
|
2022-09-09 07:24:25 -07:00
|
|
|
setSearching,
|
2022-08-17 12:48:04 -07:00
|
|
|
toggleChatPane,
|
2022-09-09 07:24:25 -07:00
|
|
|
}), [chat, needsAcceptance, isOpen, isEditing, isSearching]);
|
2022-08-17 12:48:04 -07:00
|
|
|
|
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-08-17 06:58:46 -07:00
|
|
|
isEditing: boolean
|
2022-09-09 07:24:25 -07:00
|
|
|
isOpen: boolean
|
|
|
|
isSearching: boolean
|
2022-08-17 12:48:04 -07:00
|
|
|
needsAcceptance: boolean
|
2022-08-16 10:38:17 -07:00
|
|
|
setChat: React.Dispatch<React.SetStateAction<IChat | null>>
|
2022-08-17 06:58:46 -07:00
|
|
|
setEditing: React.Dispatch<React.SetStateAction<boolean>>
|
2022-09-09 07:24:25 -07:00
|
|
|
setSearching: React.Dispatch<React.SetStateAction<boolean>>
|
2022-08-16 13:35:06 -07:00
|
|
|
toggleChatPane(): void
|
2022-08-16 10:38:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const useChatContext = (): IChatContext => useContext(ChatContext);
|
|
|
|
|
|
|
|
export { ChatContext, ChatProvider, useChatContext };
|