mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 16:33:36 +00:00
Add custom Nostr hooks, improve goosehints
This commit is contained in:
9 files changed
+193
-16
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import { type NostrEvent, type NostrMetadata, NSchema as n } from '@nostrify/nostrify';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export function useAuthor(pubkey: string | undefined) {
|
||||
const { nostr } = useNostr();
|
||||
|
||||
return useQuery<{ event?: NostrEvent; metadata?: NostrMetadata }>({
|
||||
queryKey: ['author', pubkey ?? ''],
|
||||
queryFn: async ({ signal }) => {
|
||||
if (!pubkey) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const [event] = await nostr.query(
|
||||
[{ kinds: [0], authors: [pubkey!], limit: 1 }],
|
||||
{ signal: AbortSignal.any([signal, AbortSignal.timeout(500)]) },
|
||||
);
|
||||
|
||||
if (!event) {
|
||||
return {};
|
||||
}
|
||||
|
||||
try {
|
||||
const metadata = n.json().pipe(n.metadata()).parse(event.content);
|
||||
return { metadata, event };
|
||||
} catch {
|
||||
return { event };
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { type NLoginType, NUser, useNostrLogin } from '@nostrify/react/login';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
|
||||
import { useAuthor } from './useAuthor.ts';
|
||||
|
||||
export function useCurrentUser() {
|
||||
const { nostr } = useNostr();
|
||||
const { logins } = useNostrLogin();
|
||||
|
||||
const loginToUser = useCallback((login: NLoginType): NUser => {
|
||||
switch (login.type) {
|
||||
case 'nsec':
|
||||
return NUser.fromNsecLogin(login);
|
||||
case 'bunker':
|
||||
return NUser.fromBunkerLogin(login, nostr);
|
||||
case 'extension':
|
||||
return NUser.fromExtensionLogin(login);
|
||||
default:
|
||||
// Learn how to define other login types: https://nostrify.dev/react/logins#custom-login-types
|
||||
throw new Error(`Unsupported login type: ${login.type}`);
|
||||
}
|
||||
}, [nostr]);
|
||||
|
||||
const users = useMemo(() => {
|
||||
const users: NUser[] = [];
|
||||
|
||||
for (const login of logins) {
|
||||
try {
|
||||
const user = loginToUser(login);
|
||||
users.push(user);
|
||||
} catch (error) {
|
||||
console.warn('Skipped invalid login', login.id, error);
|
||||
}
|
||||
}
|
||||
|
||||
return users;
|
||||
}, [logins, loginToUser]);
|
||||
|
||||
const user = users[0] as NUser | undefined;
|
||||
const data = useAuthor(user?.pubkey);
|
||||
|
||||
return {
|
||||
user,
|
||||
data,
|
||||
users,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { NLogin, useNostrLogin } from '@nostrify/react/login';
|
||||
|
||||
export function useLoginActions() {
|
||||
const { nostr } = useNostr();
|
||||
const { addLogin } = useNostrLogin();
|
||||
|
||||
return {
|
||||
nsec(nsec: string): void {
|
||||
const login = NLogin.fromNsec(nsec);
|
||||
addLogin(login);
|
||||
},
|
||||
async bunker(uri: string): Promise<void> {
|
||||
const login = await NLogin.fromBunker(uri, nostr);
|
||||
addLogin(login);
|
||||
},
|
||||
async extension(): Promise<void> {
|
||||
const login = await NLogin.fromExtension();
|
||||
addLogin(login);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useNostr } from "@nostrify/react";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
|
||||
import { useCurrentUser } from "./useCurrentUser";
|
||||
|
||||
interface EventTemplate {
|
||||
kind: number;
|
||||
content?: string;
|
||||
tags?: string[][];
|
||||
created_at?: number;
|
||||
}
|
||||
|
||||
export function useNostrPublish() {
|
||||
const { nostr } = useNostr();
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (t: EventTemplate) => {
|
||||
if (user) {
|
||||
const event = await user.signer.signEvent({
|
||||
kind: t.kind,
|
||||
content: t.content ?? "",
|
||||
tags: t.tags ?? [],
|
||||
created_at: t.created_at ?? Math.floor(Date.now() / 1000),
|
||||
});
|
||||
nostr.event(event);
|
||||
} else {
|
||||
throw new Error("User is not logged in");
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Failed to publish event:", error);
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
console.log("Event published successfully:", data);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user