From a573f9e4eb37329ae76eae20c71251afb9f84ffc Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sun, 16 Apr 2023 17:31:31 +0700 Subject: [PATCH] migrated channel messages to jotai state (same as chat) --- src/app/channels/[id]/page.tsx | 20 +++++++++++++------- src/components/channels/messages/index.tsx | 14 +++++++++++++- src/stores/channel.tsx | 7 +++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/app/channels/[id]/page.tsx b/src/app/channels/[id]/page.tsx index 48e3f601..6fa2a4e7 100644 --- a/src/app/channels/[id]/page.tsx +++ b/src/app/channels/[id]/page.tsx @@ -4,17 +4,19 @@ import { ChannelMessages } from '@components/channels/messages/index'; import FormChannelMessage from '@components/form/channelMessage'; import { RelayContext } from '@components/relaysProvider'; -import { channelReplyAtom } from '@stores/channel'; +import { channelMessagesAtom, channelReplyAtom } from '@stores/channel'; import useLocalStorage from '@rehooks/local-storage'; +import { useSetAtom } from 'jotai'; import { useResetAtom } from 'jotai/utils'; -import { useContext, useEffect, useRef, useState } from 'react'; +import { Suspense, useContext, useEffect, useRef } from 'react'; export default function Page({ params }: { params: { id: string } }) { const [pool, relays]: any = useContext(RelayContext); - - const [messages, setMessages] = useState([]); const [activeAccount]: any = useLocalStorage('activeAccount', {}); + + const setChannelMessages = useSetAtom(channelMessagesAtom); + const resetChannelMessages = useResetAtom(channelMessagesAtom); const resetChannelReply = useResetAtom(channelReplyAtom); const muted = useRef(new Set()); @@ -23,6 +25,8 @@ export default function Page({ params }: { params: { id: string } }) { useEffect(() => { // reset channel reply resetChannelReply(); + // reset channel messages + resetChannelMessages(); // subscribe event const unsubscribe = pool.subscribe( [ @@ -49,7 +53,7 @@ export default function Page({ params }: { params: { id: string } }) { } else if (hided.current.has(event.id)) { console.log('hided'); } else { - setMessages((messages) => [event, ...messages]); + setChannelMessages((messages) => [event, ...messages]); } } } @@ -58,11 +62,13 @@ export default function Page({ params }: { params: { id: string } }) { return () => { unsubscribe(); }; - }, [pool, relays, activeAccount.pubkey, params.id, resetChannelReply]); + }, [pool, relays, activeAccount.pubkey, params.id, setChannelMessages, resetChannelReply, resetChannelMessages]); return (
- a.created_at - b.created_at)} /> + Loading...}> + +
diff --git a/src/components/channels/messages/index.tsx b/src/components/channels/messages/index.tsx index bc18c4ac..2d832b23 100644 --- a/src/components/channels/messages/index.tsx +++ b/src/components/channels/messages/index.tsx @@ -1,11 +1,17 @@ import ChannelMessageItem from '@components/channels/messages/item'; +import { Placeholder } from '@components/note/placeholder'; +import { sortedChannelMessagesAtom } from '@stores/channel'; + +import { useAtomValue } from 'jotai'; import { useCallback, useRef } from 'react'; import { Virtuoso } from 'react-virtuoso'; -export const ChannelMessages = ({ data }: { data: any }) => { +export const ChannelMessages = () => { const virtuosoRef = useRef(null); + const data = useAtomValue(sortedChannelMessagesAtom); + const itemContent: any = useCallback( (index: string | number) => { return ; @@ -25,6 +31,7 @@ export const ChannelMessages = ({ data }: { data: any }) => { {
); }; + +const COMPONENTS = { + EmptyPlaceholder: () => , + ScrollSeekPlaceholder: () => , +}; diff --git a/src/stores/channel.tsx b/src/stores/channel.tsx index 983e2b5b..e76ae9d6 100644 --- a/src/stores/channel.tsx +++ b/src/stores/channel.tsx @@ -1,4 +1,11 @@ +import { atom } from 'jotai'; import { atomWithReset } from 'jotai/utils'; // channel reply id export const channelReplyAtom = atomWithReset({ id: null, pubkey: null, content: null }); +// channel messages +export const channelMessagesAtom = atomWithReset([]); +export const sortedChannelMessagesAtom = atom((get) => { + const messages = get(channelMessagesAtom); + return messages.sort((x: { created_at: number }, y: { created_at: number }) => x.created_at - y.created_at); +});