refactor: tweak profile loader

This commit is contained in:
kieran 2024-06-25 16:14:53 +01:00
parent 1a405f7796
commit bdc94cec8b
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 52 additions and 5 deletions

View File

@ -0,0 +1,40 @@
import { useUserProfile } from "@snort/system-react";
import { UserRelays } from "@/Cache";
import { getRelayName } from "@/Utils";
export function UserDebug({ pubkey }: { pubkey: string }) {
const profile = useUserProfile(pubkey);
const relays = UserRelays.getFromCache(pubkey);
return (
<div className="text-xs">
<div className="flex flex-col overflow-wrap">
{Object.entries(profile ?? {}).map(([k, v]) => {
let vv = <div>{v}</div>;
if (k === "loaded") vv = <div>{new Date(Number(v)).toISOString()}</div>;
if (k === "created") vv = <div>{new Date(Number(v) * 1000).toISOString()}</div>;
if (k === "npub" || k === "pubkey") return;
return (
<div key={`${pubkey}-${k}`} className="flex justify-between gap-1">
<div>{k}</div>
{vv}
</div>
);
})}
</div>
<br />
<div className="flex flex-col">
<div>Relays Updated: {new Date(1000 * (relays?.created ?? 0)).toISOString()}</div>
{relays?.relays.map(a => (
<div className="flex hover:bg-[--gray-ultradark]" key={a.url}>
<div className="grow">{getRelayName(a.url)}</div>
<div>{a.settings.read && <>R</>}</div>
<div>{a.settings.write && <>W</>}</div>
</div>
))}
</div>
</div>
);
}

View File

@ -8,6 +8,7 @@ import Text from "@/Components/Text/Text";
import FollowedBy from "@/Components/User/FollowedBy";
import useLogin from "../../Hooks/useLogin";
import { UserDebug } from "./Debug";
import FollowButton from "./FollowButton";
import ProfileImage from "./ProfileImage";
import { UserWebsiteLink } from "./UserWebsiteLink";
@ -26,6 +27,7 @@ export function ProfileCard({
const [showProfileMenu, setShowProfileMenu] = useState(false);
const [t, setT] = useState<ReturnType<typeof setTimeout>>();
const { publicKey: myPublicKey } = useLogin(s => ({ publicKey: s.publicKey }));
const debug = Boolean(localStorage.getItem("debug"));
useEffect(() => {
if (show) {
@ -71,6 +73,7 @@ export function ProfileCard({
/>
<UserWebsiteLink user={user} />
{myPublicKey && <FollowedBy pubkey={pubkey} />}
{debug && <UserDebug pubkey={pubkey} />}
</div>
</ControlledMenu>
);

View File

@ -10,8 +10,9 @@ export interface KeyedHookFilter {
fn: HookFn;
}
export interface CacheEvents {
export interface CacheEvents<T> {
change: (keys: Array<string>) => void;
update: (v: T) => void;
}
export type CachedTable<T> = {
@ -41,12 +42,12 @@ export type CachedTable<T> = {
key(of: T): string;
snapshot(): Array<T>;
search(q: string): Promise<Array<T>>;
} & EventEmitter<CacheEvents>;
} & EventEmitter<CacheEvents<T>>;
/**
* Dexie backed generic hookable store
*/
export abstract class FeedCache<TCached> extends EventEmitter<CacheEvents> implements CachedTable<TCached> {
export abstract class FeedCache<TCached> extends EventEmitter<CacheEvents<TCached>> implements CachedTable<TCached> {
readonly name: string;
#snapshot: Array<TCached> = [];
protected log: ReturnType<typeof debug>;

View File

@ -79,6 +79,9 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
resolve(existing);
this.UntrackKeys(key);
this.cache.off("change", handler);
} else {
// should never happen
reject(new Error("Not found"));
}
}
};

View File

@ -1,6 +1,6 @@
import { unixNowMs } from "@snort/shared";
import { EventKind, TaggedNostrEvent, RequestBuilder } from ".";
import { MetadataRelays, ProfileCacheExpire } from "./const";
import { ProfileCacheExpire } from "./const";
import { mapEventToProfile, CachedMetadata } from "./cache";
import { BackgroundLoader } from "./background-loader";
@ -19,7 +19,7 @@ export class ProfileLoaderService extends BackgroundLoader<CachedMetadata> {
override buildSub(missing: string[]): RequestBuilder {
const sub = new RequestBuilder(`profiles`);
sub.withFilter().kinds([EventKind.SetMetadata]).authors(missing).relay(MetadataRelays);
sub.withFilter().kinds([EventKind.SetMetadata]).authors(missing);
return sub;
}
}