mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2025-08-27 21:19:23 +00:00
38 lines
971 B
TypeScript
38 lines
971 B
TypeScript
![]() |
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);
|
||
|
},
|
||
|
});
|
||
|
}
|