webapp/src/context/inbox.tsx

130 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-12-18 00:17:58 +00:00
import type { Chat, Message, MessagesBy, MutationCreate_MessageArgs } from '../graphql/schema/chat.gen'
2023-02-10 01:19:20 +00:00
import type { Accessor, JSX } from 'solid-js'
2023-11-28 15:36:00 +00:00
2023-12-19 13:06:54 +00:00
import { createContext, createSignal, useContext } from 'solid-js'
2023-11-28 15:36:00 +00:00
2023-11-28 13:18:25 +00:00
import { inboxClient } from '../graphql/client/chat'
2023-12-18 00:17:58 +00:00
import { Author } from '../graphql/schema/core.gen'
2023-12-19 09:34:24 +00:00
import { useAuthorsStore } from '../stores/zine/authors'
2023-11-28 15:36:00 +00:00
import { SSEMessage, useConnect } from './connect'
2022-11-24 15:39:31 +00:00
type InboxContextType = {
2022-12-17 03:27:00 +00:00
chats: Accessor<Chat[]>
messages?: Accessor<Message[]>
2022-11-24 15:39:31 +00:00
actions: {
2022-12-17 03:27:00 +00:00
createChat: (members: number[], title: string) => Promise<{ chat: Chat }>
2023-12-18 00:17:58 +00:00
loadChats: () => Promise<Array<Chat>>
2023-12-19 09:34:24 +00:00
loadRecipients: () => Array<Author>
2023-12-18 00:17:58 +00:00
loadMessages: (by: MessagesBy, limit: number, offset: number) => Promise<Array<Message>>
getMessages?: (chatId: string) => Promise<Array<Message>>
2023-12-03 10:22:42 +00:00
sendMessage?: (args: MutationCreate_MessageArgs) => 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 }) => {
2022-12-17 03:27:00 +00:00
const [chats, setChats] = createSignal<Chat[]>([])
const [messages, setMessages] = createSignal<Message[]>([])
2023-12-19 09:34:24 +00:00
const { sortedAuthors } = useAuthorsStore()
2023-12-18 00:17:58 +00:00
2023-11-28 13:18:25 +00:00
const handleMessage = (sseMessage: SSEMessage) => {
2023-11-16 12:23:55 +00:00
console.log('[context.inbox]:', sseMessage)
// handling all action types: create update delete join left seen
2023-11-28 13:18:25 +00:00
if (sseMessage.entity === 'message') {
2023-11-16 12:23:55 +00:00
const relivedMessage = sseMessage.payload
setMessages((prev) => [...prev, relivedMessage])
2023-11-28 13:18:25 +00:00
} else if (sseMessage.entity === 'chat') {
2023-11-16 15:34:37 +00:00
const relivedChat = sseMessage.payload
setChats((prev) => [...prev, relivedChat])
2023-10-19 14:44:26 +00:00
}
2023-10-17 06:37:11 +00:00
}
2023-12-18 00:17:58 +00:00
const { addHandler } = useConnect()
addHandler(handleMessage)
const loadMessages = async (
by: MessagesBy,
limit: number = 50,
offset: number = 0,
): Promise<Array<Message>> => {
if (inboxClient.private) {
const msgs = await inboxClient.loadChatMessages({ by, limit, offset })
setMessages((mmm) => [...mmm, ...msgs]) // TODO: check unique
return msgs || []
}
return []
}
2022-12-17 03:27:00 +00:00
const loadChats = async () => {
try {
2023-12-18 00:17:58 +00:00
if (inboxClient.private) {
const newChats = await inboxClient.loadChats({ limit: 50, offset: 0 })
2023-12-15 13:45:34 +00:00
setChats(newChats)
2023-12-18 00:17:58 +00:00
return newChats
2023-12-15 13:45:34 +00:00
}
2022-12-17 03:27:00 +00:00
} catch (error) {
2023-12-17 12:36:47 +00:00
console.log('[loadChats] error:', error)
2022-12-17 03:27:00 +00:00
}
2023-12-18 00:17:58 +00:00
return []
2022-12-17 03:27:00 +00:00
}
const getMessages = async (chatId: string) => {
2023-12-18 00:17:58 +00:00
if (!chatId) return []
2022-12-17 03:27:00 +00:00
try {
2023-12-18 00:17:58 +00:00
const msgs: Message[] = await loadMessages({ chat: chatId })
setMessages(msgs)
return msgs || []
2022-12-17 03:27:00 +00:00
} catch (error) {
2023-10-13 21:39:08 +00:00
console.error('Error loading messages:', error)
2022-12-17 03:27:00 +00:00
}
2023-12-18 00:17:58 +00:00
return []
2022-12-17 03:27:00 +00:00
}
2023-12-03 10:22:42 +00:00
const sendMessage = async (args: MutationCreate_MessageArgs) => {
2022-12-17 03:27:00 +00:00
try {
2023-12-18 00:17:58 +00:00
if (inboxClient.private) {
const message = await inboxClient.createMessage(args)
setMessages((prev) => [...prev, message])
const currentChat = chats().find((chat) => chat.id === args.chat_id)
setChats((prev) => [
...prev.filter((c) => c.id !== currentChat.id),
{ ...currentChat, updated_at: message.created_at },
])
}
2022-12-17 03:27:00 +00:00
} catch (error) {
2023-10-13 21:39:08 +00:00
console.error('Error sending message:', error)
2022-12-17 03:27:00 +00:00
}
}
2022-11-24 15:39:31 +00:00
2022-12-02 11:11:45 +00:00
const createChat = async (members: number[], title: string) => {
2023-10-13 21:39:08 +00:00
try {
2023-12-18 00:17:58 +00:00
if (inboxClient.private) {
const chat = await inboxClient.createChat({ members, title })
setChats((prevChats) => [chat, ...prevChats])
return chat
}
2023-10-13 21:39:08 +00:00
} catch (error) {
console.error('Error creating chat:', error)
}
2022-11-24 15:39:31 +00:00
}
const actions = {
2022-12-17 03:27:00 +00:00
createChat,
loadChats,
2023-12-18 00:17:58 +00:00
loadMessages,
2023-12-19 09:34:24 +00:00
loadRecipients: sortedAuthors,
2022-12-17 03:27:00 +00:00
getMessages,
2023-11-17 16:22:54 +00:00
sendMessage,
2022-11-24 15:39:31 +00:00
}
2022-11-27 05:49:48 +00:00
2022-12-17 03:27:00 +00:00
const value: InboxContextType = { chats, messages, actions }
2023-10-13 21:39:08 +00:00
2022-11-24 15:39:31 +00:00
return <InboxContext.Provider value={value}>{props.children}</InboxContext.Provider>
}