2023-01-16 00:07:27 +00:00
|
|
|
import { useLiveQuery } from "dexie-react-hooks";
|
2023-01-16 13:17:29 +00:00
|
|
|
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
|
|
|
|
2023-01-16 17:48:25 +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>();
|
2023-01-16 13:17:29 +00:00
|
|
|
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]))
|
2023-01-16 13:17:29 +00:00
|
|
|
} 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 13:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
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(() => {
|
2023-01-16 13:17:29 +00:00
|
|
|
if (pubKey) {
|
|
|
|
System.TrackMetadata(pubKey);
|
|
|
|
return () => System.UntrackMetadata(pubKey);
|
|
|
|
}
|
2023-01-15 19:40:47 +00:00
|
|
|
}, [pubKey]);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|