import { Dialog, Transition } from '@headlessui/react'; import { NDKEvent, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Fragment, useContext, useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; import { useNDK } from '@libs/ndk/provider'; import { createChannel } from '@libs/storage'; import { AvatarUploader } from '@shared/avatarUploader'; import { CancelIcon, LoaderIcon, PlusIcon } from '@shared/icons'; import { Image } from '@shared/image'; import { DEFAULT_AVATAR } from '@stores/constants'; import { dateToUnix } from '@utils/date'; import { useAccount } from '@utils/hooks/useAccount'; export function ChannelCreateModal() { const { ndk } = useNDK(); const queryClient = useQueryClient(); const navigate = useNavigate(); const [isOpen, setIsOpen] = useState(false); const [loading, setLoading] = useState(false); const [image, setImage] = useState(DEFAULT_AVATAR); const { account } = useAccount(); const closeModal = () => { setIsOpen(false); }; const openModal = () => { setIsOpen(true); }; const { register, handleSubmit, reset, setValue, formState: { isDirty, isValid }, } = useForm(); const addChannel = useMutation({ mutationFn: (event: any) => { return createChannel( event.id, event.pubkey, event.name, event.picture, event.about, event.created_at ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['channels'] }); }, }); 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 = 40; event.created_at = dateToUnix(); event.pubkey = account.pubkey; event.tags = []; // publish event event.publish(); // insert to database addChannel.mutate({ ...event, name: data.name, picture: data.picture, about: data.about, }); // reset form reset(); setTimeout(() => { // close modal setIsOpen(false); // redirect to channel page navigate(`/channel/${event.id}`); }, 1000); } catch (e) { console.log('error: ', e); } }; useEffect(() => { setValue('picture', image); }, [setValue, image]); return ( <> openModal()} className="inline-flex h-9 items-center gap-2.5 rounded-md px-2.5" > Create channel Create channel Channels are freedom square, everyone can speech freely, no one can stop you or deceive what to speech Picture Channel name * Description Encrypted All messages are encrypted and only invited members can view and send message {loading ? ( ) : ( 'Create channel →' )} > ); }
All messages are encrypted and only invited members can view and send message