From 416a383caf3bebe2adb2914f0328deed472b5365 Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Tue, 12 Mar 2024 15:48:28 +0100 Subject: [PATCH] Store selected feed and reload on 99+ future notes --- src/components/FeedSelect/FeedSelect.tsx | 6 +++++- src/contexts/HomeContext.tsx | 6 +++++- src/lib/localStore.ts | 23 ++++++++++++++++++++++- src/pages/Home.tsx | 5 +++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/components/FeedSelect/FeedSelect.tsx b/src/components/FeedSelect/FeedSelect.tsx index 21dbfc6..e47c691 100644 --- a/src/components/FeedSelect/FeedSelect.tsx +++ b/src/components/FeedSelect/FeedSelect.tsx @@ -1,19 +1,23 @@ import { Component } from 'solid-js'; +import { useAccountContext } from '../../contexts/AccountContext'; import { useHomeContext } from '../../contexts/HomeContext'; import { useSettingsContext } from '../../contexts/SettingsContext'; import { hookForDev } from '../../lib/devTools'; +import { fetchStoredFeed } from '../../lib/localStore'; import { FeedOption, PrimalFeed, SelectionOption } from '../../types/primal'; import SelectBox from '../SelectBox/SelectBox'; import SelectionBox from '../SelectionBox/SelectionBox'; const FeedSelect: Component<{ isPhone?: boolean, id?: string}> = (props) => { + const account = useAccountContext(); const home = useHomeContext(); const settings = useSettingsContext(); const findFeed = (hex: string, includeReplies: string) => { const ir = includeReplies === 'undefined' ? undefined : includeReplies === 'true'; + return settings?.availableFeeds.find(f => { const isHex = f.hex === hex; const isOpt = typeof ir === typeof f.includeReplies ? @@ -74,7 +78,7 @@ const FeedSelect: Component<{ isPhone?: boolean, id?: string}> = (props) => { }; const initialValue = () => { - const selected = home?.selectedFeed; + const selected = home?.selectedFeed || fetchStoredFeed(account?.publicKey); if (!selected) { return { diff --git a/src/contexts/HomeContext.tsx b/src/contexts/HomeContext.tsx index 2b9aac1..8fcc750 100644 --- a/src/contexts/HomeContext.tsx +++ b/src/contexts/HomeContext.tsx @@ -4,6 +4,7 @@ import { createStore, reconcile, unwrap } from "solid-js/store"; import { APP_ID } from "../App"; import { Kind } from "../constants"; import { getEvents, getExploreFeed, getFeed, getFutureExploreFeed, getFutureFeed } from "../lib/feed"; +import { fetchStoredFeed, saveStoredFeed } from "../lib/localStore"; import { setLinkPreviews } from "../lib/notes"; import { getScoredUsers, searchContent } from "../lib/search"; import { isConnected, refreshSocketListeners, removeSocketListeners, socket } from "../sockets"; @@ -378,6 +379,8 @@ export const HomeProvider = (props: { children: ContextChildren }) => { const selectFeed = (feed: PrimalFeed | undefined) => { if (feed?.hex !== undefined && (feed.hex !== currentFeed?.hex || feed.includeReplies !== currentFeed?.includeReplies)) { currentFeed = { ...feed }; + saveStoredFeed(account?.publicKey, currentFeed); + updateStore('selectedFeed', reconcile({...feed})); clearNotes(); fetchNotes(feed.hex , `${APP_ID}`, 0, feed.includeReplies); @@ -665,7 +668,8 @@ export const HomeProvider = (props: { children: ContextChildren }) => { createEffect(() => { if (account?.isKeyLookupDone && settings?.defaultFeed) { - selectFeed(settings?.defaultFeed); + const storedFeed = fetchStoredFeed(account.publicKey); + selectFeed(storedFeed || settings?.defaultFeed); } }); diff --git a/src/lib/localStore.ts b/src/lib/localStore.ts index e609be0..91dd068 100644 --- a/src/lib/localStore.ts +++ b/src/lib/localStore.ts @@ -25,6 +25,7 @@ export type LocalStore = { noteDraft: Record, noteDraftUserRefs: Record>, uploadTime: Record, + selectedFeed: PrimalFeed | undefined, }; export type UploadTime = { @@ -43,7 +44,7 @@ export const defaultUploadTime: UploadTime = { final: 100, }; -export const emptyStorage = { +export const emptyStorage: LocalStore = { following: [], followingSince: 0, muted: [], @@ -61,6 +62,7 @@ export const emptyStorage = { noteDraft: {}, noteDraftUserRefs: {}, uploadTime: defaultUploadTime, + selectedFeed: undefined, } export const storageName = (pubkey?: string) => { @@ -402,3 +404,22 @@ export const loadMsgContacts = (pubkey: string) => { return store.msgContacts || { profiles: {}, counts: {} }; }; + + +export const fetchStoredFeed = (pubkey: string | undefined) => { + if (!pubkey) return undefined; + + const store = getStorage(pubkey) + + return store.selectedFeed; +}; + +export const saveStoredFeed = (pubkey: string | undefined, feed: PrimalFeed) => { + if (!pubkey) return; + + const store = getStorage(pubkey); + + store.selectedFeed = { ...feed }; + + setStorage(pubkey, store); +}; diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 328eba5..7c44074 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -110,6 +110,11 @@ const Home: Component = () => { }); const loadNewContent = () => { + if (newNotesCount() > 100) { + location.reload(); + return; + } + context?.actions.loadFutureContent(); scrollWindowTo(0, true); setHasNewPosts(false);