Fix useLoggedInAccounts: safely extract pubkey from amber login type

NLoginType doesn't expose pubkey field directly — amber login is cast
as unknown as NLoginType so l.pubkey was undefined. Use getPubkey()
helper that casts to extract pubkey safely for all login types.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-07-09 14:06:01 +07:00
parent fc1c8aca26
commit 0b1bf05e74

View file

@ -21,17 +21,25 @@ export function useLoggedInAccounts() {
const { nostr } = useNostr();
const { logins, setLogin, removeLogin } = useNostrLogin();
// Extract pubkey safely — works for both standard NLoginType and custom AmberLoginType
const getPubkey = (login: NLoginType): string | undefined => {
return (login as unknown as { pubkey?: string }).pubkey;
};
const loginPubkeys = logins.map(getPubkey).filter((p): p is string => !!p);
const { data: authors = [] } = useQuery({
queryKey: ['nostr', 'logins', logins.map((l) => l.id).join(';')],
queryFn: async ({ signal }) => {
if (logins.length === 0) return [];
if (loginPubkeys.length === 0) return [];
// Try primary relays first with longer timeout
let events: NostrEvent[] = [];
try {
events = await nostr.query(
[{ kinds: [0], authors: logins.map((l) => l.pubkey) }],
[{ kinds: [0], authors: loginPubkeys }],
{ signal: AbortSignal.any([signal, AbortSignal.timeout(5000)]) },
);
} catch (error) {
@ -43,7 +51,7 @@ export function useLoggedInAccounts() {
try {
const fallbackGroup = nostr.group(FALLBACK_RELAYS);
events = await fallbackGroup.query(
[{ kinds: [0], authors: logins.map((l) => l.pubkey) }],
[{ kinds: [0], authors: loginPubkeys }],
{ signal: AbortSignal.any([signal, AbortSignal.timeout(8000)]) },
);
if (events.length > 0) {
@ -54,7 +62,9 @@ export function useLoggedInAccounts() {
}
}
return logins.map(({ id, pubkey }): Account => {
return logins.map((login): Account => {
const id = login.id;
const pubkey = getPubkey(login) ?? '';
const event = events.find((e) => e.pubkey === pubkey);
try {
const metadata = n.json().pipe(n.metadata()).parse(event?.content);
@ -73,8 +83,9 @@ export function useLoggedInAccounts() {
const currentUser: Account | undefined = (() => {
const login = logins[0];
if (!login) return undefined;
const pubkey = getPubkey(login) ?? '';
const author = authors.find((a) => a.id === login.id);
return { metadata: {}, ...author, id: login.id, pubkey: login.pubkey };
return { metadata: {}, ...author, id: login.id, pubkey };
})();
// Other users are all logins except the current one