flockstr/components/Modals/ShortTextNote.tsx

166 lines
4.7 KiB
TypeScript
Raw Permalink Normal View History

2023-10-18 08:15:06 -04:00
import { useEffect, useState } from "react";
import FormModal from "./FormModal";
import { z } from "zod";
import useEvents from "@/lib/hooks/useEvents";
2023-10-18 12:23:39 -04:00
import { createEventHandler } from "@/lib/actions/create";
2023-10-18 08:15:06 -04:00
import { unixTimeNowInSeconds } from "@/lib/nostr/dates";
import { useModal } from "@/app/_providers/modal/provider";
import { toast } from "sonner";
import { useNDK } from "@/app/_providers/ndk";
2023-10-18 12:23:39 -04:00
import { useSigner, type SignerStoreItem } from "@/app/_providers/signer";
2023-10-18 08:15:06 -04:00
import { getTagValues } from "@/lib/nostr/utils";
2023-10-18 12:23:39 -04:00
import useCurrentUser from "@/lib/hooks/useCurrentUser";
import { saveEphemeralSigner } from "@/lib/actions/ephemeral";
import useLists from "@/lib/hooks/useLists";
2023-10-18 22:09:45 -04:00
import { getUrls } from "@/components/TextRendering";
2023-10-20 15:26:55 -04:00
import { log } from "@/lib/utils";
2023-10-18 08:15:06 -04:00
const ShortTextNoteSchema = z.object({
content: z.string(),
isPrivate: z.boolean().optional(),
2023-10-20 15:26:55 -04:00
subscriptionTiers: z.string().array().optional(),
2023-10-18 08:15:06 -04:00
});
type ShortTextNoteType = z.infer<typeof ShortTextNoteSchema>;
export default function ShortTextNoteModal() {
const modal = useModal();
const [isLoading, setIsLoading] = useState(false);
2023-10-20 15:26:55 -04:00
const { currentUser, initSubscriptions, mySubscription } = useCurrentUser();
2023-10-18 08:15:06 -04:00
const [sent, setSent] = useState(false);
const { ndk } = useNDK();
2023-10-18 12:23:39 -04:00
const { getSigner } = useSigner()!;
useEffect(() => {
if (currentUser) {
2023-10-20 15:26:55 -04:00
void initSubscriptions(currentUser.pubkey);
2023-10-18 12:23:39 -04:00
}
}, [currentUser]);
2023-10-18 08:15:06 -04:00
// useEffect(() => {
// if (events.length) {
// console.log("Done!");
// setIsLoading(false);
// toast.success("List Updated!");
// modal?.hide();
// }
// }, [events]);
2023-10-18 12:23:39 -04:00
async function handleSubmit(data: ShortTextNoteType) {
2023-10-18 08:15:06 -04:00
setIsLoading(true);
2023-10-18 12:23:39 -04:00
if (!ndk) {
toast.error("Error connecting");
return;
}
2023-10-18 22:09:45 -04:00
const urls = getUrls(data.content);
const urlTags = urls.map((u) => ["r", u]);
2023-10-20 15:26:55 -04:00
if (data.isPrivate) {
if (!mySubscription) {
2023-10-18 12:23:39 -04:00
toast.error("No list found");
return;
}
2023-10-20 15:26:55 -04:00
let delegateSigner: SignerStoreItem | undefined = undefined;
2023-10-18 12:23:39 -04:00
if (data.isPrivate) {
2023-10-20 15:26:55 -04:00
delegateSigner = await getSigner(mySubscription);
if (!delegateSigner?.signer) {
2023-10-18 12:23:39 -04:00
toast.error("Error creating signer");
return;
}
2023-10-20 15:26:55 -04:00
if (!delegateSigner?.saved) {
2023-10-18 12:23:39 -04:00
console.log("Saving delegate...");
2023-10-20 15:26:55 -04:00
await saveEphemeralSigner(ndk!, delegateSigner.signer, {
associatedEvent: mySubscription,
2023-10-18 12:23:39 -04:00
keyProfile: {
2023-10-20 15:26:55 -04:00
name: delegateSigner.title,
2023-10-18 12:23:39 -04:00
picture: currentUser?.profile?.image,
lud06: currentUser?.profile?.lud06,
lud16: currentUser?.profile?.lud16,
},
});
}
}
2023-10-20 15:26:55 -04:00
log(
"info",
"about to create private event with ",
JSON.stringify(delegateSigner),
);
2023-10-18 12:23:39 -04:00
const result = await createEventHandler(
ndk,
{
content: data.content,
kind: 1,
2023-10-18 22:09:45 -04:00
tags: [...urlTags],
2023-10-18 12:23:39 -04:00
},
data.isPrivate,
2023-10-20 15:26:55 -04:00
mySubscription,
delegateSigner?.signer,
2023-10-18 12:23:39 -04:00
);
if (result) {
toast.success("Note added!");
modal?.hide();
}
} else {
2023-10-20 15:26:55 -04:00
const result = await createEventHandler(ndk, {
content: data.content,
kind: 1,
tags: [...urlTags],
});
2023-10-18 12:23:39 -04:00
if (result) {
toast.success("Note added!");
modal?.hide();
}
}
2023-10-18 08:15:06 -04:00
}
return (
<FormModal
title="Short Text Note"
fields={[
{
label: "Content",
type: "text-area",
slug: "content",
},
2023-10-20 15:26:55 -04:00
...(mySubscription
? ([
{
label: "Subs only",
type: "toggle",
slug: "isPrivate",
},
] as const)
: []),
// {
// label: "Choose Tiers",
// type: "select-search",
// placeholder: "Search your lists",
// slug: "subscriptionTiers",
// options: lists
// .map((l) => {
// const title =
// getTagValues("title", l.tags) ??
// getTagValues("name", l.tags) ??
// "Untitled";
// const description = getTagValues("description", l.tags);
// const value = getTagValues("d", l.tags);
// if (!value) return;
// return {
// label: title,
// description,
// value: value,
// };
// })
// .filter(Boolean),
// condition: "subscriptions",
// },
2023-10-18 08:15:06 -04:00
]}
formSchema={ShortTextNoteSchema}
onSubmit={handleSubmit}
isSubmitting={isLoading}
cta={{
text: "Publish",
}}
/>
);
}