import { BaseControl, Button, Popover, TextareaControl } from "@wordpress/components" import { close, edit, trash } from "@wordpress/icons" import { useEffect, useState, useRef } from "@wordpress/element" import { __ } from "@wordpress/i18n" import { deleteCommentMutationOptions, updateCommentMutationOptions } from "@/shared/plugin-comments/mutation-options" import { useMutation } from "@tanstack/react-query" import { useForm } from "@tanstack/react-form" import usePluginSelectedStore from "@/stores/pluginSelectedStore" import { autop } from "@wordpress/autop" const NoteItem = ({ comment }: { comment: PluginComment }) => { const {plugin} = usePluginSelectedStore() /** * Delete comment */ const [deletePopoverOpen, setDeletePopoverOpen] = useState(false) const { mutate: deleteComment, isPending: isDeletingComment } = useMutation(deleteCommentMutationOptions()) const handleDelete = () => { deleteComment(comment, { onSettled: () => { setDeletePopoverOpen(false) } }) } /** * Edit comment */ const [editOpen, setEditOpen] = useState(false) const { mutate: updateComment } = useMutation(updateCommentMutationOptions()) const toggleEdit = () => { setEditOpen(!editOpen) } const form = useForm({ defaultValues: { comment_content: comment.comment_content ?? '', }, onSubmit: (values) => { updateComment({ comment_id: comment.id ?? 0, comment_content: values.value.comment_content, plugin_slug: plugin?.slug ?? '', }) toggleEdit() } }) /** * Focus on input when edit is opened */ const inputRef = useRef(null) useEffect(() => { if (editOpen) { const length = inputRef.current?.value.length ?? 0; inputRef.current?.focus(); inputRef.current?.setSelectionRange(length, length); } }, [editOpen]) if (isDeletingComment) { return } return (
{ comment.comment_user_avatar && ( {comment.comment_user_display_name} ) } {comment.comment_user_display_name}
-
{ !editOpen && (
) } { editOpen && (
( field.handleChange(value as string)} placeholder={__('Add your notes here...', 'ploogins-ai-assistant')} rows={1} ref={inputRef} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} />
) }
)}
) } export default NoteItem