2023-10-23 23:33:39 +02:00
|
|
|
import { useState, useRef } from "react"
|
|
|
|
import Image from "next/image"
|
|
|
|
import Link from "next/link"
|
|
|
|
import date from "date-and-time"
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
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"
|
2022-01-01 20:42:25 +01:00
|
|
|
|
2022-12-13 11:46:31 +01:00
|
|
|
export interface MessageProps {
|
2022-01-01 20:42:25 +01:00
|
|
|
message: MessageWithMember
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Message: React.FC<MessageProps> = (props) => {
|
|
|
|
const { message } = props
|
|
|
|
|
2022-08-29 21:24:38 +02:00
|
|
|
const textareaReference = useRef<HTMLTextAreaElement>(null)
|
|
|
|
const [isEditing, setIsEditing] = useState(false)
|
|
|
|
const { authentication, user } = useAuthentication()
|
|
|
|
|
|
|
|
const handleTextareaKeyDown: React.KeyboardEventHandler<HTMLFormElement> = (
|
2023-10-23 23:33:39 +02:00
|
|
|
event,
|
2022-08-29 21:24:38 +02:00
|
|
|
) => {
|
2023-10-23 23:33:39 +02:00
|
|
|
if (event.key === "Enter" && !event.shiftKey) {
|
2022-08-29 21:24:38 +02:00
|
|
|
event.preventDefault()
|
|
|
|
event.currentTarget.dispatchEvent(
|
2023-10-23 23:33:39 +02:00
|
|
|
new Event("submit", { cancelable: true, bubbles: true }),
|
2022-08-29 21:24:38 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleEdit = async (): Promise<void> => {
|
|
|
|
const newMessage = textareaReference.current?.value ?? message.value
|
|
|
|
if (
|
2023-10-23 23:33:39 +02:00
|
|
|
typeof newMessage === "string" &&
|
2022-08-29 21:24:38 +02:00
|
|
|
newMessage.length > 0 &&
|
|
|
|
newMessage !== message.value
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await authentication.api.put(`/messages/${message.id}`, {
|
2023-10-23 23:33:39 +02:00
|
|
|
value: newMessage,
|
2022-08-29 21:24:38 +02:00
|
|
|
})
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
handleEditMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleEditMode = (): void => {
|
|
|
|
setIsEditing((oldIsEditing) => {
|
|
|
|
return !oldIsEditing
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
return (
|
2022-12-13 11:46:31 +01:00
|
|
|
<div
|
2022-01-07 21:21:38 +01:00
|
|
|
data-cy={`message-${message.id}`}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="group flex w-full p-4 transition hover:bg-gray-200 dark:hover:bg-gray-900"
|
2022-01-07 21:21:38 +01:00
|
|
|
>
|
2022-01-01 20:42:25 +01:00
|
|
|
<Link href={`/application/users/${message.member.user.id}`}>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="mr-4 flex h-12 w-12 flex-shrink-0 items-center justify-center">
|
|
|
|
<div className="h-10 w-10 drop-shadow-md">
|
2022-12-13 11:38:07 +01:00
|
|
|
<Image
|
|
|
|
quality={100}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="rounded-full"
|
2022-12-13 11:38:07 +01:00
|
|
|
src={
|
|
|
|
message.member.user.logo == null
|
2023-10-23 23:33:39 +02:00
|
|
|
? "/images/data/user-default.png"
|
2022-12-13 11:38:07 +01:00
|
|
|
: message.member.user.logo
|
|
|
|
}
|
|
|
|
alt={"Users's profil picture"}
|
|
|
|
width={50}
|
|
|
|
height={50}
|
|
|
|
draggable={false}
|
|
|
|
/>
|
2022-01-01 20:42:25 +01:00
|
|
|
</div>
|
2022-12-13 11:38:07 +01:00
|
|
|
</div>
|
2022-01-01 20:42:25 +01:00
|
|
|
</Link>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="relative w-full whitespace-pre-wrap break-words break-all">
|
|
|
|
<div className="flex w-max items-center">
|
2022-01-01 20:42:25 +01:00
|
|
|
<Link href={`/application/users/${message.member.user.id}`}>
|
2022-12-13 11:38:07 +01:00
|
|
|
<span
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="message-member-user-name"
|
|
|
|
className="font-bold text-gray-900 dark:text-gray-200"
|
2022-12-13 11:38:07 +01:00
|
|
|
>
|
|
|
|
{message.member.user.name}
|
|
|
|
</span>
|
2022-01-01 20:42:25 +01:00
|
|
|
</Link>
|
|
|
|
<span
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="message-date"
|
|
|
|
className="ml-4 select-none text-xs text-gray-500 dark:text-gray-200"
|
2022-01-01 20:42:25 +01:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{date.format(new Date(message.createdAt), "DD/MM/YYYY - HH:mm:ss")}
|
2022-01-01 20:42:25 +01:00
|
|
|
</span>
|
|
|
|
</div>
|
2022-08-31 21:44:33 +02:00
|
|
|
|
|
|
|
{message.member.userId === user.id && (
|
|
|
|
<MessageOptions
|
|
|
|
message={message}
|
2023-10-23 23:33:39 +02:00
|
|
|
editMode={isEditing ? ":white_check_mark:" : ":pencil2:"}
|
2022-08-31 21:44:33 +02:00
|
|
|
handleEdit={isEditing ? handleEdit : handleEditMode}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
{message.type === "text" ? (
|
2022-08-29 21:24:38 +02:00
|
|
|
<>
|
|
|
|
{isEditing ? (
|
2022-08-31 21:44:33 +02:00
|
|
|
<EditMessage
|
|
|
|
message={message}
|
|
|
|
textareaRef={textareaReference}
|
|
|
|
handleEdit={handleEdit}
|
|
|
|
handleKeyDown={handleTextareaKeyDown}
|
|
|
|
/>
|
2022-08-29 21:24:38 +02:00
|
|
|
) : (
|
|
|
|
<MessageText message={message} />
|
|
|
|
)}
|
|
|
|
</>
|
2023-10-23 23:33:39 +02:00
|
|
|
) : message.type === "file" ? (
|
2022-01-07 21:21:38 +01:00
|
|
|
<MessageFile message={message} />
|
|
|
|
) : (
|
|
|
|
<Loader />
|
|
|
|
)}
|
2022-01-01 20:42:25 +01:00
|
|
|
</div>
|
2022-12-13 11:46:31 +01:00
|
|
|
</div>
|
2022-01-01 20:42:25 +01:00
|
|
|
)
|
|
|
|
}
|