import { CharacterEntity, NoteEntity } from "crossbell.js" import { useTranslation } from "next-i18next" import { useRouter } from "next/router" import { useEffect, useState } from "react" import { useForm } from "react-hook-form" import { useAccountState } from "@crossbell/connect-kit" import { Popover } from "@headlessui/react" import { Avatar } from "~/components/ui/Avatar" import { Button } from "~/components/ui/Button" import { Input } from "~/components/ui/Input" import { Profile } from "~/lib/types" import { useCommentPage, useUpdateComment } from "~/queries/page" import { useAccountSites } from "~/queries/site" import { EmojiPicker } from "./EmojiPicker" export const CommentInput: React.FC<{ pageId?: string originalId?: string onSubmitted?: () => void comment?: NoteEntity & { character?: CharacterEntity | null } }> = ({ pageId, originalId, onSubmitted, comment }) => { const account = useAccountState((s) => s.computed.account) const userSites = useAccountSites() const commentPage = useCommentPage() const updateComment = useUpdateComment() const router = useRouter() const [viewer, setViewer] = useState(null) const { t } = useTranslation(["common", "site"]) useEffect(() => { if (userSites.isSuccess && userSites.data?.length) { setViewer(userSites.data[0]) } }, [userSites, router]) const form = useForm({ defaultValues: { content: comment?.metadata?.content?.content || "", }, }) const handleSubmit = form.handleSubmit(async (values) => { if (pageId) { if (comment) { if (values.content) { updateComment.mutate({ pageId, content: values.content, externalUrl: window.location.href, originalId, characterId: comment.characterId, noteId: comment.noteId, }) } } else { commentPage.mutate({ pageId: pageId, content: values.content, externalUrl: window.location.href, originalId: originalId, }) } } }) useEffect(() => { if (commentPage.isSuccess || updateComment.isSuccess) { form.reset() onSubmitted?.() } }, [commentPage.isSuccess, updateComment.isSuccess, form, onSubmitted]) return (
{({ open }: { open: boolean }) => ( <> form.setValue( "content", form.getValues("content") + e.native, ) } /> )}
) }