2022-11-24 15:39:31 +00:00
|
|
|
import type { JSX } from 'solid-js'
|
|
|
|
import { createContext, useContext } from 'solid-js'
|
2022-11-25 07:36:45 +00:00
|
|
|
import type { Message } from '../graphql/types.gen'
|
2022-11-24 15:39:31 +00:00
|
|
|
import { apiClient } from '../utils/apiClient'
|
|
|
|
import { createStore } from 'solid-js/store'
|
|
|
|
|
|
|
|
type InboxContextType = {
|
|
|
|
chatEntities: { [chatId: string]: Message[] }
|
|
|
|
actions: {
|
2022-11-27 05:49:48 +00:00
|
|
|
createChat: (members: string[], title: string) => Promise<void>
|
2022-11-24 15:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const InboxContext = createContext<InboxContextType>()
|
|
|
|
|
|
|
|
export function useInbox() {
|
|
|
|
return useContext(InboxContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const InboxProvider = (props: { children: JSX.Element }) => {
|
|
|
|
const [chatEntities, setChatEntities] = createStore({})
|
|
|
|
|
2022-11-27 05:49:48 +00:00
|
|
|
const createChat = async (members: string[], title: string) => {
|
|
|
|
const chat = await apiClient.createChat({ members, title })
|
2022-11-25 07:36:45 +00:00
|
|
|
setChatEntities((s) => {
|
|
|
|
s[chat.id] = chat
|
|
|
|
})
|
2022-11-25 23:34:46 +00:00
|
|
|
return chat
|
2022-11-24 15:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
createChat
|
|
|
|
}
|
2022-11-27 05:49:48 +00:00
|
|
|
|
2022-11-24 15:39:31 +00:00
|
|
|
const value: InboxContextType = { chatEntities, actions }
|
|
|
|
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
|
|
|
|
}
|