import { Dialog, Transition } from "@headlessui/react"; import { getChannel } from "@libs/storage"; import { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import { AvatarUploader } from "@shared/avatarUploader"; import { CancelIcon, EditIcon } from "@shared/icons"; import { Image } from "@shared/image"; import { RelayContext } from "@shared/relayProvider"; import { useActiveAccount } from "@stores/accounts"; import { DEFAULT_AVATAR } from "@stores/constants"; import { dateToUnix } from "@utils/date"; import { Fragment, useContext, useEffect, useState } from "react"; import { useForm } from "react-hook-form"; export function ChannelUpdateModal({ id }: { id: string }) { const ndk = useContext(RelayContext); const account = useActiveAccount((state: any) => state.account); const [isOpen, setIsOpen] = useState(false); const [image, setImage] = useState(DEFAULT_AVATAR); const [loading, setLoading] = useState(false); const closeModal = () => { setIsOpen(false); }; const openModal = () => { setIsOpen(true); }; const { register, handleSubmit, reset, setValue, formState: { isDirty, isValid }, } = useForm({ defaultValues: async () => { const channel = await getChannel(id); const metadata = JSON.parse(channel.metadata); // update image state setImage(metadata.image); // set default values return metadata; }, }); const onSubmit = (data: any) => { setLoading(true); try { const signer = new NDKPrivateKeySigner(account.privkey); ndk.signer = signer; const event = new NDKEvent(ndk); // build event event.content = JSON.stringify(data); event.kind = 41; event.created_at = dateToUnix(); event.pubkey = account.pubkey; event.tags = [["e", id]]; // publish event event.publish(); // reset form reset(); // close modal setIsOpen(false); } catch (e) { console.log("error: ", e); } }; useEffect(() => { setValue("picture", image); }, [setValue, image]); return ( <>
Update channel
New metadata will be published on all relays, and will be immediately available to all users, so please carefully.
channel picture