Store selected feed and reload on 99+ future notes

This commit is contained in:
Bojan Mojsilovic 2024-03-12 15:48:28 +01:00
parent a5bf233b25
commit 416a383caf
4 changed files with 37 additions and 3 deletions

View File

@ -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 {

View File

@ -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);
}
});

View File

@ -25,6 +25,7 @@ export type LocalStore = {
noteDraft: Record<string, string>,
noteDraftUserRefs: Record<string, Record<string, PrimalUser>>,
uploadTime: Record<string, number>,
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);
};

View File

@ -110,6 +110,11 @@ const Home: Component = () => {
});
const loadNewContent = () => {
if (newNotesCount() > 100) {
location.reload();
return;
}
context?.actions.loadFutureContent();
scrollWindowTo(0, true);
setHasNewPosts(false);