import { Menu } from '@headlessui/react'; import { ExclamationIcon } from '@heroicons/react/outline'; import { DotsVerticalIcon } from '@heroicons/react/solid'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import Link from 'next/link'; import React, { useState } from 'react'; import { defineMessages, FormattedRelativeTime, useIntl } from 'react-intl'; import ReactMarkdown from 'react-markdown'; import * as Yup from 'yup'; import type { default as IssueCommentType } from '../../../../server/entity/IssueComment'; import { Permission, useUser } from '../../../hooks/useUser'; import Button from '../../Common/Button'; import Modal from '../../Common/Modal'; import Transition from '../../Transition'; const messages = defineMessages({ postedby: 'Posted {relativeTime} by {username}', postedbyedited: 'Posted {relativeTime} by {username} (Edited)', delete: 'Delete Comment', areyousuredelete: 'Are you sure you want to delete this comment?', validationComment: 'You must enter a message', edit: 'Edit Comment', }); interface IssueCommentProps { comment: IssueCommentType; isReversed?: boolean; isActiveUser?: boolean; onUpdate?: () => void; } const IssueComment: React.FC = ({ comment, isReversed = false, isActiveUser = false, onUpdate, }) => { const intl = useIntl(); const [showDeleteModal, setShowDeleteModal] = useState(false); const [isEditing, setIsEditing] = useState(false); const { hasPermission } = useUser(); const EditCommentSchema = Yup.object().shape({ newMessage: Yup.string().required( intl.formatMessage(messages.validationComment) ), }); const deleteComment = async () => { try { await axios.delete(`/api/v1/issueComment/${comment.id}`); } catch (e) { // something went wrong deleting the comment } finally { if (onUpdate) { onUpdate(); } } }; return (
setShowDeleteModal(false)} onOk={() => deleteComment()} okText={intl.formatMessage(messages.delete)} okButtonType="danger" iconSvg={} > {intl.formatMessage(messages.areyousuredelete)}
{(isActiveUser || hasPermission(Permission.MANAGE_ISSUES)) && ( {({ open }) => ( <>
Open options
{isActiveUser && ( {({ active }) => ( )} )} {({ active }) => ( )}
)}
)}
{isEditing ? ( { await axios.put(`/api/v1/issueComment/${comment.id}`, { message: values.newMessage, }); if (onUpdate) { onUpdate(); } setIsEditing(false); }} validationSchema={EditCommentSchema} > {({ isValid, isSubmitting, errors, touched }) => { return (
{errors.newMessage && touched.newMessage && (
{errors.newMessage}
)}
); }}
) : (
{comment.message}
)}
{intl.formatMessage( comment.createdAt !== comment.updatedAt ? messages.postedbyedited : messages.postedby, { username: ( {comment.user.displayName} ), relativeTime: ( ), } )}
); }; export default IssueComment;