inbox-context
This commit is contained in:
parent
f3cfa97254
commit
b70e70cba3
|
@ -14,7 +14,6 @@ type InboxContextType = {
|
||||||
loadChats: () => Promise<void>
|
loadChats: () => Promise<void>
|
||||||
getMessages?: (chatId: string) => Promise<void>
|
getMessages?: (chatId: string) => Promise<void>
|
||||||
sendMessage?: (args: MutationCreateMessageArgs) => void
|
sendMessage?: (args: MutationCreateMessageArgs) => void
|
||||||
// unsubscribe: () => void
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,37 +28,30 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
|
||||||
const [messages, setMessages] = createSignal<Message[]>([])
|
const [messages, setMessages] = createSignal<Message[]>([])
|
||||||
|
|
||||||
fetchEventSource('https://chat.discours.io/connect', {
|
fetchEventSource('https://chat.discours.io/connect', {
|
||||||
method: 'POST',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: 'Bearer ' + getToken()
|
Authorization: 'Bearer ' + getToken()
|
||||||
},
|
},
|
||||||
onmessage(event) {
|
onmessage(event) {
|
||||||
const message = JSON.parse(event.data)
|
const message = JSON.parse(event.data)
|
||||||
|
console.log('Received message:', message)
|
||||||
// TODO: Do something with the message
|
// TODO: Add the message to the appropriate chat
|
||||||
|
|
||||||
console.log(message)
|
|
||||||
},
|
},
|
||||||
onclose() {
|
onclose() {
|
||||||
// if no error thrown - it will reconnect
|
|
||||||
console.log('sse connection closed by server')
|
console.log('sse connection closed by server')
|
||||||
},
|
},
|
||||||
onerror(err) {
|
onerror(err) {
|
||||||
console.warn(err)
|
console.error('sse connection closed by error', err)
|
||||||
console.error('sse connection closed by error')
|
|
||||||
throw new Error() // hack to close the connection
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: maybe sometimes need to call /disconnect
|
|
||||||
|
|
||||||
const loadChats = async () => {
|
const loadChats = async () => {
|
||||||
try {
|
try {
|
||||||
const newChats = await inboxClient.loadChats({ limit: 50, offset: 0 })
|
const newChats = await inboxClient.loadChats({ limit: 50, offset: 0 })
|
||||||
setChats(newChats)
|
setChats(newChats)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.error('Error loading chats:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,11 +61,11 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
|
||||||
const response = await loadMessages({ chat: chatId })
|
const response = await loadMessages({ chat: chatId })
|
||||||
setMessages(response as unknown as Message[])
|
setMessages(response as unknown as Message[])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[loadMessages]', error)
|
console.error('Error loading messages:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendMessage = async (args) => {
|
const sendMessage = async (args: MutationCreateMessageArgs) => {
|
||||||
try {
|
try {
|
||||||
const message = await inboxClient.createMessage(args)
|
const message = await inboxClient.createMessage(args)
|
||||||
setMessages((prev) => [...prev, message])
|
setMessages((prev) => [...prev, message])
|
||||||
|
@ -83,16 +75,18 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
|
||||||
{ ...currentChat, updatedAt: message.createdAt }
|
{ ...currentChat, updatedAt: message.createdAt }
|
||||||
])
|
])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[post message error]:', error)
|
console.error('Error sending message:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const createChat = async (members: number[], title: string) => {
|
const createChat = async (members: number[], title: string) => {
|
||||||
const chat = await inboxClient.createChat({ members, title })
|
try {
|
||||||
setChats((prevChats) => {
|
const chat = await inboxClient.createChat({ members, title })
|
||||||
return [chat, ...prevChats]
|
setChats((prevChats) => [chat, ...prevChats])
|
||||||
})
|
return chat
|
||||||
return chat
|
} catch (error) {
|
||||||
|
console.error('Error creating chat:', error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
|
@ -103,5 +97,6 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const value: InboxContextType = { chats, messages, actions }
|
const value: InboxContextType = { chats, messages, actions }
|
||||||
|
|
||||||
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
|
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user