snort/src/Feed/ProfileFeed.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-01-16 00:07:27 +00:00
import { useLiveQuery } from "dexie-react-hooks";
import { useEffect, useMemo } from "react";
2023-01-20 11:11:50 +00:00
import { db } from "Db";
import { MetadataCache } from "Db/User";
import { HexKey } from "Nostr";
import { System } from "Nostr/System";
2023-01-15 19:40:47 +00:00
export default function useProfile(pubKey?: HexKey | Array<HexKey> | undefined): Map<HexKey, MetadataCache> | undefined {
2023-01-16 00:07:27 +00:00
const user = useLiveQuery(async () => {
2023-01-16 17:48:25 +00:00
let userList = new Map<HexKey, MetadataCache>();
if (pubKey) {
if (Array.isArray(pubKey)) {
let ret = await db.users.bulkGet(pubKey);
2023-01-16 17:48:25 +00:00
let filtered = ret.filter(a => a !== undefined).map(a => a!);
return new Map(filtered.map(a => [a.pubkey, a]))
} else {
2023-01-16 17:48:25 +00:00
let ret = await db.users.get(pubKey);
if (ret) {
userList.set(ret.pubkey, ret);
}
}
}
2023-01-16 17:48:25 +00:00
return userList;
2023-01-16 00:07:27 +00:00
}, [pubKey]);
2023-01-15 19:40:47 +00:00
useEffect(() => {
if (pubKey) {
System.TrackMetadata(pubKey);
return () => System.UntrackMetadata(pubKey);
}
2023-01-15 19:40:47 +00:00
}, [pubKey]);
return user;
}