import React, { createContext, useContext, useState } from 'react'; import { useDispatch } from 'react-redux'; import { toggleMainWindow } from 'soapbox/actions/chats'; import { useSettings } from 'soapbox/hooks'; import type { IChat } from 'soapbox/queries/chats'; type WindowState = 'open' | 'minimized'; const ChatContext = createContext({ chat: null, isOpen: false, isEditing: false, }); const ChatProvider: React.FC = ({ children }) => { const dispatch = useDispatch(); const settings = useSettings(); const [chat, setChat] = useState(null); const [isEditing, setEditing] = useState(false); const mainWindowState = settings.getIn(['chats', 'mainWindow']) as WindowState; const isOpen = mainWindowState === 'open'; const toggleChatPane = () => dispatch(toggleMainWindow()); return ( {children} ); }; interface IChatContext { chat: IChat | null isOpen: boolean isEditing: boolean setChat: React.Dispatch> setEditing: React.Dispatch> toggleChatPane(): void } const useChatContext = (): IChatContext => useContext(ChatContext); export { ChatContext, ChatProvider, useChatContext };