Persist profile history

This commit is contained in:
Bojan Mojsilovic 2024-01-11 12:58:12 +01:00
parent 0218a2647d
commit e9f320698c
2 changed files with 48 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import {
createContext, createContext,
createEffect, createEffect,
onCleanup, onCleanup,
onMount,
useContext useContext
} from "solid-js"; } from "solid-js";
import { import {
@ -52,6 +53,7 @@ import { subscribeTo } from "../sockets";
import { parseBolt11 } from "../utils"; import { parseBolt11 } from "../utils";
import { convertToUser } from "../stores/profile"; import { convertToUser } from "../stores/profile";
import { sortBreakpoints } from "@solid-primitives/media"; import { sortBreakpoints } from "@solid-primitives/media";
import { readRecomendedUsers, saveRecomendedUsers } from "../lib/localStore";
export type UserStats = { export type UserStats = {
pubkey: string, pubkey: string,
@ -916,6 +918,7 @@ export const ProfileProvider = (props: { children: ContextChildren }) => {
list.splice(index, 1); list.splice(index, 1);
updateStore('profileHistory', 'profiles', () => [user, ...list]); updateStore('profileHistory', 'profiles', () => [user, ...list]);
return; return;
} }
@ -1151,6 +1154,21 @@ export const ProfileProvider = (props: { children: ContextChildren }) => {
} }
}); });
createEffect(() => {
if (account && account.hasPublicKey()) {
const history = readRecomendedUsers(account.publicKey);
history && updateStore('profileHistory', reconcile(history));
}
});
createEffect(() => {
const profiles = [...store.profileHistory.profiles];
const stats = { ...store.profileHistory.stats };
saveRecomendedUsers(account?.publicKey, { profiles, stats });
});
onCleanup(() => { onCleanup(() => {
removeSocketListeners( removeSocketListeners(
socket(), socket(),

View File

@ -1,3 +1,4 @@
import { UserStats } from "../contexts/ProfileContext";
import { NostrRelays, PrimalFeed, PrimalUser, SelectionOption } from "../types/primal"; import { NostrRelays, PrimalFeed, PrimalUser, SelectionOption } from "../types/primal";
export type LocalStore = { export type LocalStore = {
@ -12,6 +13,10 @@ export type LocalStore = {
theme: string, theme: string,
homeSidebarSelection: SelectionOption | undefined, homeSidebarSelection: SelectionOption | undefined,
userProfile: PrimalUser | undefined, userProfile: PrimalUser | undefined,
recomended: {
profiles: PrimalUser[],
stats: Record<string, UserStats>,
},
}; };
export const emptyStorage = { export const emptyStorage = {
@ -26,6 +31,7 @@ export const emptyStorage = {
theme: 'sunset', theme: 'sunset',
homeSidebarSelection: undefined, homeSidebarSelection: undefined,
userProfile: undefined, userProfile: undefined,
recomended: { profiles: [], stats: {} },
} }
export const storageName = (pubkey?: string) => { export const storageName = (pubkey?: string) => {
@ -146,6 +152,30 @@ export const saveTheme = (pubkey: string | undefined, theme: string) => {
setStorage(pubkey, store); setStorage(pubkey, store);
}; };
export const saveRecomendedUsers = (pubkey: string | undefined, recomended: { profiles: PrimalUser[], stats: Record<string, UserStats> }) => {
if (!pubkey) {
return;
}
const store = getStorage(pubkey);
store.recomended = recomended;
setStorage(pubkey, store);
}
export const readRecomendedUsers = (pubkey: string | undefined) => {
if (!pubkey) {
return;
}
const store = getStorage(pubkey);
const recomended = store.recomended;
return recomended ? recomended as { profiles: PrimalUser[], stats: Record<string, UserStats> } : undefined;
}
export const saveHomeSidebarSelection = (pubkey: string | undefined, selection: SelectionOption | undefined) => { export const saveHomeSidebarSelection = (pubkey: string | undefined, selection: SelectionOption | undefined) => {
if (!pubkey) { if (!pubkey) {
return; return;