From d849f984ab10a5e3ff6310cf8292f08ce85c6c1a Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Thu, 11 Jan 2024 17:12:55 +0100 Subject: [PATCH] Fix data reload on socket reset --- src/contexts/HomeContext.tsx | 10 ++++++++-- src/contexts/MessagesContext.tsx | 16 ++++++++++++---- src/pages/Home.tsx | 2 -- src/pages/Profile.tsx | 5 ++++- src/pages/Thread.tsx | 9 ++++++++- src/sockets.tsx | 6 +++--- src/types/primal.d.ts | 2 ++ 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/contexts/HomeContext.tsx b/src/contexts/HomeContext.tsx index e914fa7..bb7e1ba 100644 --- a/src/contexts/HomeContext.tsx +++ b/src/contexts/HomeContext.tsx @@ -344,8 +344,11 @@ export const HomeProvider = (props: { children: ContextChildren }) => { updateStore('scrollTop', () => top); }; + let currentFeed = ''; + const selectFeed = (feed: PrimalFeed | undefined) => { - if (feed !== undefined && feed.hex !== undefined) { + if (feed?.hex !== undefined && feed.hex !== currentFeed) { + currentFeed = feed.hex; updateStore('selectedFeed', reconcile({...feed})); clearNotes(); fetchNotes(feed.hex , `${APP_ID}`, 0, feed.includeReplies); @@ -623,8 +626,11 @@ export const HomeProvider = (props: { children: ContextChildren }) => { } }); + let keyIsDone = false; + createEffect(() => { - if (account?.isKeyLookupDone) { + if (account?.isKeyLookupDone && !keyIsDone && settings?.defaultFeed) { + keyIsDone = true; selectFeed(settings?.defaultFeed); } }); diff --git a/src/contexts/MessagesContext.tsx b/src/contexts/MessagesContext.tsx index 619b342..ef55d44 100644 --- a/src/contexts/MessagesContext.tsx +++ b/src/contexts/MessagesContext.tsx @@ -564,10 +564,12 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { const orderedSenders = () => { - if (!store.senders) { + const senders = store.senders; + + if (!senders) { return []; } - const senders = store.senders; + const counts = store.messageCountPerSender; const ids = Object.keys(senders); @@ -797,8 +799,11 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { } }); + let isSecSet = false + createEffect(() => { - if (!account?.sec) { + if (!account?.sec && !isSecSet) { + isSecSet = true; unsubscribeToMessagesStats(subidMsgCount); updateStore('messageCount', () => 0); updateStore('messageCountPerSender', reconcile({})); @@ -818,9 +823,12 @@ export const MessagesProvider = (props: { children: ContextChildren }) => { updateStore('now', () => Math.floor((new Date()).getTime() / 1000)); }; + let currentSender = ''; + // When a sender is selected, get the first page of the conversation createEffect(() => { - if (store.selectedSender) { + if (store.selectedSender && store.selectedSender !== currentSender) { + currentSender = store.selectedSender; clearInterval(conversationRefreshInterval); updateStore('encryptedMessages', () => []); diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index dd38f6e..480a25b 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -96,8 +96,6 @@ const Home: Component = () => { } setNewNotesCount(count); - - }); onCleanup(()=> { diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx index 713e80c..c352c7a 100644 --- a/src/pages/Profile.tsx +++ b/src/pages/Profile.tsx @@ -105,8 +105,11 @@ const Profile: Component = () => { profile?.actions.clearZaps(); } + let keyIsDone = false + createEffect(() => { - if (account?.isKeyLookupDone) { + if (account?.isKeyLookupDone && !keyIsDone) { + keyIsDone = true; setProfile(getHex()); } }); diff --git a/src/pages/Thread.tsx b/src/pages/Thread.tsx index a431b06..7cfdefa 100644 --- a/src/pages/Thread.tsx +++ b/src/pages/Thread.tsx @@ -32,6 +32,8 @@ const Thread: Component = () => { let repliesHolder: HTMLDivElement | undefined; + let initialPostId = ''; + const postId = () => { if (params.postId.startsWith('note')) { return params.postId; @@ -95,7 +97,12 @@ const Thread: Component = () => { const isFetching = () => threadContext?.isFetching; createEffect(() => { - threadContext?.actions.fetchNotes(postId()); + const pid = postId(); + + if (pid !== initialPostId) { + threadContext?.actions.fetchNotes(pid); + initialPostId = pid; + } }); let observer: IntersectionObserver | undefined; diff --git a/src/sockets.tsx b/src/sockets.tsx index b1ff1b2..4c90d6f 100644 --- a/src/sockets.tsx +++ b/src/sockets.tsx @@ -13,9 +13,9 @@ const onOpen = () => { const hook = (window as PrimalWindow).onPrimalCacheServerConnected; hook && hook(cacheServer, socket()); - socket().addEventListener('message', function(event) { - const hook = (window as PrimalWindow).onPrimalCacheServerMessageReceived; - hook && hook(cacheServer, event.data); + socket()?.addEventListener('message', function(event) { + const hook = (window as PrimalWindow).onPrimalCacheServerMessageReceived; + hook && hook(cacheServer, event.data); }); } } diff --git a/src/types/primal.d.ts b/src/types/primal.d.ts index 0e62133..31e379a 100644 --- a/src/types/primal.d.ts +++ b/src/types/primal.d.ts @@ -277,6 +277,8 @@ export type FeedPage = { postStats: NostrPostStats, mentions?: Record, noteActions: Record, + since?: number, + until?: number, }; export type TrendingNotesStore = {