bigbuffet-rw/app/soapbox/contexts/chat-context.tsx

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-16 10:38:17 -07:00
import React, { createContext, useContext, useState } from 'react';
2022-08-16 13:35:06 -07:00
import { useDispatch } from 'react-redux';
import { toggleMainWindow } from 'soapbox/actions/chats';
import { 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-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 06:58:46 -07:00
const [chat, setChat] = useState<IChat | null>(null);
const [isEditing, setEditing] = useState<boolean>(false);
2022-08-16 13:35:06 -07:00
const mainWindowState = settings.getIn(['chats', 'mainWindow']) as WindowState;
const isOpen = mainWindowState === 'open';
const toggleChatPane = () => dispatch(toggleMainWindow());
2022-08-16 10:38:17 -07:00
return (
2022-08-17 06:58:46 -07:00
<ChatContext.Provider value={{ chat, setChat, isOpen, isEditing, setEditing, toggleChatPane }}>
{children}
</ChatContext.Provider>
2022-08-16 10:38:17 -07:00
);
};
interface IChatContext {
chat: IChat | null
2022-08-16 13:35:06 -07:00
isOpen: boolean
2022-08-17 06:58:46 -07:00
isEditing: 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-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 };