import { useState, useRef } from "react" import Image from "next/image" import Link from "next/link" import date from "date-and-time" import type { MessageWithMember } from "../../../../models/Message" import { MessageText } from "./MessageText" import { Loader } from "../../../design/Loader" import { MessageFile } from "./MessageFile" import { useAuthentication } from "../../../../tools/authentication" import { MessageOptions } from "./MessageOptions" import { EditMessage } from "./EditMessage" export interface MessageProps { message: MessageWithMember } export const Message: React.FC = (props) => { const { message } = props const textareaReference = useRef(null) const [isEditing, setIsEditing] = useState(false) const { authentication, user } = useAuthentication() const handleTextareaKeyDown: React.KeyboardEventHandler = ( event, ) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault() event.currentTarget.dispatchEvent( new Event("submit", { cancelable: true, bubbles: true }), ) } } const handleEdit = async (): Promise => { const newMessage = textareaReference.current?.value ?? message.value if ( typeof newMessage === "string" && newMessage.length > 0 && newMessage !== message.value ) { try { await authentication.api.put(`/messages/${message.id}`, { value: newMessage, }) } catch {} } handleEditMode() } const handleEditMode = (): void => { setIsEditing((oldIsEditing) => { return !oldIsEditing }) } return (
{"Users's
{message.member.user.name} {date.format(new Date(message.createdAt), "DD/MM/YYYY - HH:mm:ss")}
{message.member.userId === user.id && ( )} {message.type === "text" ? ( <> {isEditing ? ( ) : ( )} ) : message.type === "file" ? ( ) : ( )}
) }