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-16 00:07:27 +00:00
|
|
|
import { db } from "../db";
|
2023-01-15 19:40:47 +00:00
|
|
|
import { HexKey } from "../nostr";
|
2023-01-16 00:07:27 +00:00
|
|
|
import { System } from "../nostr/System";
|
2023-01-15 19:40:47 +00:00
|
|
|
|
2023-01-16 13:17:29 +00:00
|
|
|
export default function useProfile(pubKey: HexKey | Array<HexKey>) {
|
2023-01-16 00:07:27 +00:00
|
|
|
const user = useLiveQuery(async () => {
|
2023-01-16 13:17:29 +00:00
|
|
|
if (pubKey) {
|
|
|
|
if (Array.isArray(pubKey)) {
|
|
|
|
let ret = await db.users.bulkGet(pubKey);
|
|
|
|
return ret.filter(a => a !== undefined).map(a => a!);
|
|
|
|
} else {
|
|
|
|
return await db.users.get(pubKey);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|