webapp/src/context/inbox.tsx

37 lines
1.0 KiB
TypeScript
Raw Normal View History

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: {
createChat: (memberSlugs: string[], title?: string) => Promise<void>
}
}
const InboxContext = createContext<InboxContextType>()
export function useInbox() {
return useContext(InboxContext)
}
export const InboxProvider = (props: { children: JSX.Element }) => {
const [chatEntities, setChatEntities] = createStore({})
2022-11-25 07:36:45 +00:00
const createChat = async (members: string[], title?: string) => {
const chat = await apiClient.createChat({ members, title })
2022-11-24 15:39:31 +00:00
2022-11-25 07:36:45 +00:00
setChatEntities((s) => {
s[chat.id] = chat
})
2022-11-24 15:39:31 +00:00
}
const actions = {
createChat
}
const value: InboxContextType = { chatEntities, actions }
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
}