import { Dialog, Transition } from '@headlessui/react'; import { Combobox } from '@headlessui/react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { nip19 } from 'nostr-tools'; import { Fragment, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useHotkeys } from 'react-hotkeys-hook'; import { User } from '@app/auth/components/user'; import { createBlock } from '@libs/storage'; import { CancelIcon, CheckCircleIcon, CommandIcon, LoaderIcon } from '@shared/icons'; import { DEFAULT_AVATAR } from '@stores/constants'; import { ADD_FEEDBLOCK_SHORTCUT } from '@stores/shortcuts'; import { useAccount } from '@utils/hooks/useAccount'; export function AddFeedBlock() { const queryClient = useQueryClient(); const [loading, setLoading] = useState(false); const [isOpen, setIsOpen] = useState(false); const [selected, setSelected] = useState([]); const [query, setQuery] = useState(''); const { status, account } = useAccount(); const openModal = () => { setIsOpen(true); }; const closeModal = () => { setIsOpen(false); }; useHotkeys(ADD_FEEDBLOCK_SHORTCUT, () => openModal()); const block = useMutation({ mutationFn: (data: any) => { return createBlock(data.kind, data.title, data.content); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['blocks'] }); }, }); const { register, handleSubmit, reset, formState: { isDirty, isValid }, } = useForm(); const onSubmit = (data: any) => { setLoading(true); selected.forEach((item, index) => { if (item.substring(0, 4) === 'npub') { selected[index] = nip19.decode(item).data; } }); // insert to database block.mutate({ kind: 1, title: data.title, content: JSON.stringify(selected), }); setLoading(false); // reset form reset(); // close modal closeModal(); }; return ( <> openModal()} className="inline-flex h-9 w-56 items-center justify-start gap-2.5 rounded-md px-2.5" > F New feed block Create feed block Specific newsfeed space for people you want to keep up to date Title * Choose at least 1 user * setQuery(event.target.value)} spellCheck={false} placeholder="Enter pubkey or npub..." className="relative mb-2 h-10 w-full rounded-md bg-zinc-700 px-3 py-2 text-zinc-100 !outline-none placeholder:text-zinc-500" /> {query.length > 0 && ( {({ selected }) => ( <> {query} {selected && ( )} > )} )} {status === 'loading' ? ( Loading... ) : ( JSON.parse(account.follows).map((follow) => ( {({ selected }) => ( <> {selected && ( )} > )} )) )} {loading ? ( ) : ( 'Confirm' )} > ); }
Loading...