premerge-fixes

This commit is contained in:
Untone 2023-12-18 03:17:58 +03:00
parent dd370144a7
commit 8ed92278bf
7 changed files with 78 additions and 63 deletions

View File

@ -1,7 +1,7 @@
import type { ConfirmEmailSearchParams } from './types' import type { ConfirmEmailSearchParams } from './types'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import { createEffect, createMemo, createSignal, onMount, Show } from 'solid-js' import { createEffect, createSignal, onMount, Show } from 'solid-js'
import { useLocalize } from '../../../context/localize' import { useLocalize } from '../../../context/localize'
import { useSession } from '../../../context/session' import { useSession } from '../../../context/session'

View File

@ -1,4 +1,4 @@
import clsx from 'clsx' import { clsx } from 'clsx'
import { useLocalize } from '../../../context/localize' import { useLocalize } from '../../../context/localize'
import { Icon } from '../../_shared/Icon' import { Icon } from '../../_shared/Icon'

View File

@ -7,7 +7,6 @@ import { For, createSignal, Show, onMount, createEffect, createMemo, on } from '
import { useInbox } from '../../context/inbox' import { useInbox } from '../../context/inbox'
import { useLocalize } from '../../context/localize' import { useLocalize } from '../../context/localize'
import { useSession } from '../../context/session' import { useSession } from '../../context/session'
import { loadRecipients } from '../../stores/inbox'
import { useRouter } from '../../stores/router' import { useRouter } from '../../stores/router'
import { showModal } from '../../stores/ui' import { showModal } from '../../stores/ui'
import { Icon } from '../_shared/Icon' import { Icon } from '../_shared/Icon'
@ -42,7 +41,7 @@ export const InboxView = () => {
const { const {
chats, chats,
messages, messages,
actions: { loadChats, getMessages, sendMessage, createChat }, actions: { loadChats, loadRecipients, getMessages, sendMessage, createChat },
} = useInbox() } = useInbox()
const [recipients, setRecipients] = createSignal<Author[]>([]) const [recipients, setRecipients] = createSignal<Author[]>([])

View File

@ -1,10 +1,10 @@
import type { Chat, Message, MutationCreate_MessageArgs } from '../graphql/schema/chat.gen' import type { Chat, Message, MessagesBy, MutationCreate_MessageArgs } from '../graphql/schema/chat.gen'
import type { Accessor, JSX } from 'solid-js' import type { Accessor, JSX } from 'solid-js'
import { createContext, createMemo, createSignal, useContext } from 'solid-js' import { createContext, createEffect, createSignal, useContext } from 'solid-js'
import { inboxClient } from '../graphql/client/chat' import { inboxClient } from '../graphql/client/chat'
import { loadMessages } from '../stores/inbox' import { Author } from '../graphql/schema/core.gen'
import { SSEMessage, useConnect } from './connect' import { SSEMessage, useConnect } from './connect'
import { useSession } from './session' import { useSession } from './session'
@ -14,8 +14,10 @@ type InboxContextType = {
messages?: Accessor<Message[]> messages?: Accessor<Message[]>
actions: { actions: {
createChat: (members: number[], title: string) => Promise<{ chat: Chat }> createChat: (members: number[], title: string) => Promise<{ chat: Chat }>
loadChats: () => Promise<void> loadChats: () => Promise<Array<Chat>>
getMessages?: (chatId: string) => Promise<void> loadRecipients: () => Promise<Array<Author>>
loadMessages: (by: MessagesBy, limit: number, offset: number) => Promise<Array<Message>>
getMessages?: (chatId: string) => Promise<Array<Message>>
sendMessage?: (args: MutationCreate_MessageArgs) => void sendMessage?: (args: MutationCreate_MessageArgs) => void
} }
} }
@ -29,6 +31,7 @@ export function useInbox() {
export const InboxProvider = (props: { children: JSX.Element }) => { export const InboxProvider = (props: { children: JSX.Element }) => {
const [chats, setChats] = createSignal<Chat[]>([]) const [chats, setChats] = createSignal<Chat[]>([])
const [messages, setMessages] = createSignal<Message[]>([]) const [messages, setMessages] = createSignal<Message[]>([])
const handleMessage = (sseMessage: SSEMessage) => { const handleMessage = (sseMessage: SSEMessage) => {
console.log('[context.inbox]:', sseMessage) console.log('[context.inbox]:', sseMessage)
@ -41,50 +44,77 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
setChats((prev) => [...prev, relivedChat]) setChats((prev) => [...prev, relivedChat])
} }
} }
const {
actions: { getToken },
} = useSession()
const apiClient = createMemo(() => {
const token = getToken()
if (!inboxClient.private) {
inboxClient.connect(token)
return inboxClient
}
})
const { addHandler } = useConnect() const { addHandler } = useConnect()
addHandler(handleMessage) addHandler(handleMessage)
const {
actions: { getToken },
} = useSession()
createEffect(() => {
const token = getToken()
if (!inboxClient.private && token) {
inboxClient.connect(token)
}
})
const loadRecipients = async (limit = 50, offset = 0): Promise<Array<Author>> => {
if (inboxClient.private) {
// TODO: perhaps setMembers(authors) ?
return await inboxClient.loadRecipients({ limit, offset })
}
return []
}
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 []
}
const loadChats = async () => { const loadChats = async () => {
try { try {
const client = apiClient() if (inboxClient.private) {
if (client) { const newChats = await inboxClient.loadChats({ limit: 50, offset: 0 })
const newChats = await client.loadChats({ limit: 50, offset: 0 })
setChats(newChats) setChats(newChats)
return newChats
} }
} catch (error) { } catch (error) {
console.log('[loadChats] error:', error) console.log('[loadChats] error:', error)
} }
return []
} }
const getMessages = async (chatId: string) => { const getMessages = async (chatId: string) => {
if (!chatId) return if (!chatId) return []
try { try {
const response = await loadMessages({ chat: chatId }) const msgs: Message[] = await loadMessages({ chat: chatId })
setMessages(response as unknown as Message[]) setMessages(msgs)
return msgs || []
} catch (error) { } catch (error) {
console.error('Error loading messages:', error) console.error('Error loading messages:', error)
} }
return []
} }
const sendMessage = async (args: MutationCreate_MessageArgs) => { const sendMessage = async (args: MutationCreate_MessageArgs) => {
try { try {
const message = await apiClient().createMessage(args) if (inboxClient.private) {
setMessages((prev) => [...prev, message]) const message = await inboxClient.createMessage(args)
const currentChat = chats().find((chat) => chat.id === args.chat_id) setMessages((prev) => [...prev, message])
setChats((prev) => [ const currentChat = chats().find((chat) => chat.id === args.chat_id)
...prev.filter((c) => c.id !== currentChat.id), setChats((prev) => [
{ ...currentChat, updated_at: message.created_at }, ...prev.filter((c) => c.id !== currentChat.id),
]) { ...currentChat, updated_at: message.created_at },
])
}
} catch (error) { } catch (error) {
console.error('Error sending message:', error) console.error('Error sending message:', error)
} }
@ -92,9 +122,11 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
const createChat = async (members: number[], title: string) => { const createChat = async (members: number[], title: string) => {
try { try {
const chat = await inboxClient.createChat({ members, title }) if (inboxClient.private) {
setChats((prevChats) => [chat, ...prevChats]) const chat = await inboxClient.createChat({ members, title })
return chat setChats((prevChats) => [chat, ...prevChats])
return chat
}
} catch (error) { } catch (error) {
console.error('Error creating chat:', error) console.error('Error creating chat:', error)
} }
@ -103,6 +135,8 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
const actions = { const actions = {
createChat, createChat,
loadChats, loadChats,
loadMessages,
loadRecipients,
getMessages, getMessages,
sendMessage, sendMessage,
} }

View File

@ -1,6 +1,6 @@
import type { Accessor, JSX } from 'solid-js' import type { Accessor, JSX } from 'solid-js'
import { createContext, createMemo, createSignal, onMount, useContext } from 'solid-js' import { createContext, createEffect, createMemo, createSignal, onMount, useContext } from 'solid-js'
import { createStore } from 'solid-js/store' import { createStore } from 'solid-js/store'
import { Portal } from 'solid-js/web' import { Portal } from 'solid-js/web'
@ -44,21 +44,18 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
actions: { getToken }, actions: { getToken },
} = useSession() } = useSession()
const apiClient = createMemo(() => { createEffect(() => {
const token = getToken() const token = getToken()
if (!notifierClient.private) { if (!notifierClient.private && token) {
notifierClient.connect(token) notifierClient.connect(token)
return notifierClient
} }
}) })
const { addHandler } = useConnect() const { addHandler } = useConnect()
const loadNotifications = async (options: { limit?: number; offset?: number }) => { const loadNotifications = async (options: { limit?: number; offset?: number }) => {
const client = apiClient() if (isAuthenticated() && notifierClient?.private) {
if (isAuthenticated() && client) { const { notifications, unread, total } = await notifierClient.getNotifications(options)
console.debug(client)
const { notifications, unread, total } = await client.getNotifications(options)
const newNotificationEntities = notifications.reduce((acc, notification) => { const newNotificationEntities = notifications.reduce((acc, notification) => {
acc[notification.id] = notification acc[notification.id] = notification
return acc return acc
@ -91,18 +88,14 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
}) })
const markNotificationAsRead = async (notification: Notification) => { const markNotificationAsRead = async (notification: Notification) => {
const client = apiClient() if (notifierClient.private) await notifierClient.markNotificationAsRead(notification.id)
if (client) {
await client.markNotificationAsRead(notification.id)
}
const nnn = new Set([...notification.seen, notification.id]) const nnn = new Set([...notification.seen, notification.id])
setNotificationEntities(notification.id, 'seen', [...nnn]) setNotificationEntities(notification.id, 'seen', [...nnn])
setUnreadNotificationsCount((oldCount) => oldCount - 1) setUnreadNotificationsCount((oldCount) => oldCount - 1)
} }
const markAllNotificationsAsRead = async () => { const markAllNotificationsAsRead = async () => {
const client = apiClient() if (isAuthenticated() && notifierClient.private) {
if (isAuthenticated() && client) { await notifierClient.markAllNotificationsAsRead()
await client.markAllNotificationsAsRead()
await loadNotifications({ limit: loadedNotificationsCount() }) await loadNotifications({ limit: loadedNotificationsCount() })
} }
} }

View File

@ -85,13 +85,13 @@ export const SessionProvider = (props: {
const getSession = async (): Promise<AuthToken> => { const getSession = async (): Promise<AuthToken> => {
try { try {
const tkn = getToken() const tkn = getToken()
console.debug('[context.session] token before:', tkn) // console.debug('[context.session] token before:', tkn)
const authResult = await authorizer().getSession({ const authResult = await authorizer().getSession({
Authorization: tkn, Authorization: tkn,
}) })
if (authResult?.access_token) { if (authResult?.access_token) {
mutate(authResult) mutate(authResult)
console.debug('[context.session] token after:', authResult.access_token) // console.debug('[context.session] token after:', authResult.access_token)
await loadSubscriptions() await loadSubscriptions()
return authResult return authResult
} }

View File

@ -1,11 +0,0 @@
import type { MessagesBy } from '../graphql/schema/chat.gen'
import { inboxClient } from '../graphql/client/chat'
export const loadRecipients = async (by = {}): Promise<void> => {
return await inboxClient.loadRecipients(by)
}
export const loadMessages = async (by: MessagesBy): Promise<void> => {
return await inboxClient.loadChatMessages({ by, limit: 50, offset: 0 })
}