2023-10-23 23:33:39 +02:00
|
|
|
import { useState, useEffect } from "react"
|
|
|
|
import axios from "axios"
|
|
|
|
import prettyBytes from "pretty-bytes"
|
|
|
|
import { DownloadIcon } from "@heroicons/react/solid"
|
2022-01-07 21:21:38 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import type { MessageWithMember } from "../../../../../models/Message"
|
|
|
|
import { Loader } from "../../../../design/Loader"
|
|
|
|
import { FileIcon } from "./FileIcon"
|
|
|
|
import { api } from "../../../../../tools/api"
|
2022-01-07 21:21:38 +01:00
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
const supportedImageMimetype = [
|
2023-10-23 23:33:39 +02:00
|
|
|
"image/png",
|
|
|
|
"image/jpg",
|
|
|
|
"image/jpeg",
|
|
|
|
"image/gif",
|
2022-03-05 18:22:30 +01:00
|
|
|
]
|
|
|
|
|
2022-01-07 21:21:38 +01:00
|
|
|
export interface FileData {
|
|
|
|
blob: Blob
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessageContentProps {
|
|
|
|
message: MessageWithMember
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MessageFile: React.FC<MessageContentProps> = (props) => {
|
|
|
|
const { message } = props
|
|
|
|
|
|
|
|
const [file, setFile] = useState<FileData | null>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const ourRequest = axios.CancelToken.source()
|
|
|
|
|
|
|
|
const fetchData = async (): Promise<void> => {
|
2022-04-08 20:59:04 +02:00
|
|
|
const { data } = await api.get(message.value, {
|
2023-10-23 23:33:39 +02:00
|
|
|
responseType: "blob",
|
|
|
|
cancelToken: ourRequest.token,
|
2022-01-07 21:21:38 +01:00
|
|
|
})
|
|
|
|
const fileURL = URL.createObjectURL(data)
|
|
|
|
setFile({ blob: data, url: fileURL })
|
|
|
|
}
|
|
|
|
fetchData().catch(() => {})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
ourRequest.cancel()
|
|
|
|
}
|
2022-04-08 20:59:04 +02:00
|
|
|
}, [message.value])
|
2022-01-07 21:21:38 +01:00
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
return <Loader />
|
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
if (supportedImageMimetype.includes(message.mimetype)) {
|
2022-01-07 21:21:38 +01:00
|
|
|
return (
|
2023-10-23 23:33:39 +02:00
|
|
|
<a href={file.url} target="_blank" rel="noreferrer">
|
2022-01-13 18:21:45 +01:00
|
|
|
<img
|
2022-01-07 21:21:38 +01:00
|
|
|
data-cy={`message-file-image-${message.id}`}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="max-h-80 sm:max-w-xs"
|
2022-01-07 21:21:38 +01:00
|
|
|
src={file.url}
|
|
|
|
alt={message.value}
|
|
|
|
/>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (message.mimetype.startsWith("audio/")) {
|
2022-01-07 21:21:38 +01:00
|
|
|
return (
|
|
|
|
<audio controls data-cy={`message-file-audio-${message.id}`}>
|
|
|
|
<source src={file.url} type={message.mimetype} />
|
|
|
|
</audio>
|
|
|
|
)
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
if (message.mimetype.startsWith("video/")) {
|
2022-01-07 21:21:38 +01:00
|
|
|
return (
|
|
|
|
<video
|
2023-10-23 23:33:39 +02:00
|
|
|
className="max-h-80 max-w-xs"
|
2022-01-07 21:21:38 +01:00
|
|
|
controls
|
|
|
|
data-cy={`message-file-video-${message.id}`}
|
|
|
|
>
|
|
|
|
<source src={file.url} type={message.mimetype} />
|
|
|
|
</video>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<a href={file.url} download data-cy={`message-file-download-${message.id}`}>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex items-center">
|
|
|
|
<div className="flex items-center">
|
2022-01-07 21:21:38 +01:00
|
|
|
<div>
|
|
|
|
<FileIcon />
|
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="ml-4">
|
2022-01-07 21:21:38 +01:00
|
|
|
<p>{file.blob.type}</p>
|
2023-10-23 23:33:39 +02:00
|
|
|
<p className="mt-1">{prettyBytes(file.blob.size)}</p>
|
2022-01-07 21:21:38 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<DownloadIcon className="ml-4 h-8 w-8" />
|
2022-01-07 21:21:38 +01:00
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|