From e1a3c881e1738f0d1918984cf0bc1934ae255981 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Thu, 16 Nov 2023 15:23:55 +0300 Subject: [PATCH] Render new messages (WIP) --- src/components/Views/Inbox.tsx | 38 ++++++++++++++++++++++++++++------ src/context/inbox.tsx | 14 ++++++------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/components/Views/Inbox.tsx b/src/components/Views/Inbox.tsx index 438c1d26..f4136f90 100644 --- a/src/components/Views/Inbox.tsx +++ b/src/components/Views/Inbox.tsx @@ -1,4 +1,4 @@ -import { For, createSignal, Show, onMount, createEffect, createMemo } from 'solid-js' +import { For, createSignal, Show, onMount, createEffect, createMemo, on } from 'solid-js' import type { Author, Chat, Message as MessageType } from '../../graphql/types.gen' import DialogCard from '../Inbox/DialogCard' import Search from '../Inbox/Search' @@ -50,6 +50,11 @@ export const InboxView = () => { const { session } = useSession() const currentUserId = createMemo(() => session()?.user.id) const { changeSearchParam, searchParams } = useRouter() + + const messagesContainerRef: { current: HTMLDivElement } = { + current: null + } + // Поиск по диалогам const getQuery = (query) => { if (query().length >= 2) { @@ -60,8 +65,6 @@ export const InboxView = () => { } } - let chatWindow - const handleOpenChat = async (chat: Chat) => { setCurrentDialog(chat) changeSearchParam({ @@ -72,7 +75,10 @@ export const InboxView = () => { } catch (error) { console.error('[getMessages]', error) } finally { - chatWindow.scrollTop = chatWindow.scrollHeight + messagesContainerRef.current.scroll({ + top: messagesContainerRef.current.scrollHeight, + behavior: 'instant' + }) } } @@ -108,7 +114,7 @@ export const InboxView = () => { }) setClear(true) setMessageToReply(null) - chatWindow.scrollTop = chatWindow.scrollHeight + messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight setClear(false) } @@ -152,6 +158,26 @@ export const InboxView = () => { return messages().find((message) => message.id === messageId) } + createEffect( + on( + () => messages(), + () => { + if (!messagesContainerRef.current) { + return + } + if (messagesContainerRef.current.scrollTop >= messagesContainerRef.current.scrollHeight) { + //TODO: show new message arrow - bubble + return + } + messagesContainerRef.current.scroll({ + top: messagesContainerRef.current.scrollHeight, + behavior: 'smooth' + }) + } + ), + { defer: true } + ) + return (
@@ -232,7 +258,7 @@ export const InboxView = () => { >
-
+
(messagesContainerRef.current = el)}> {(message) => ( { actions: { setMessageHandler } } = useNotifications() - const handleMessage = (m) => { - console.log('[context.inbox]:', m) + const handleMessage = (sseMessage) => { + console.log('[context.inbox]:', sseMessage) // TODO: handle all action types: create update delete join left - if (['create', 'update', 'delete'].includes(m.action)) { - const msg = m.payload - setMessages((mmm) => [msg, ...mmm]) - } else if (['left', 'join'].includes(m.action)) { + if (['create', 'update', 'delete'].includes(sseMessage.action)) { + const relivedMessage = sseMessage.payload + setMessages((prev) => [...prev, relivedMessage]) + } else if (['left', 'join'].includes(sseMessage.action)) { // TODO: set chat members - console.debug(m) + console.debug(sseMessage) } }