2024-03-09 07:00:27 +01:00
|
|
|
import { Button, Dialog, DialogBody } from '@material-tailwind/react'
|
2023-12-02 11:26:45 +01:00
|
|
|
import { Entity } from 'megalodon'
|
2024-03-09 07:00:27 +01:00
|
|
|
import { useState, useEffect, useCallback } from 'react'
|
|
|
|
import { FaChevronLeft, FaChevronRight } from 'react-icons/fa6'
|
2023-12-02 11:26:45 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
open: boolean
|
|
|
|
close: () => void
|
2024-03-09 07:00:27 +01:00
|
|
|
index: number
|
|
|
|
media: Array<Entity.Attachment>
|
2023-12-02 11:26:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function Media(props: Props) {
|
2024-03-09 07:00:27 +01:00
|
|
|
const [index, setIndex] = useState<number>(0)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIndex(props.index)
|
|
|
|
}, [props.index])
|
|
|
|
|
|
|
|
const next = useCallback(() => {
|
|
|
|
if (index >= props.media.length - 1) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setIndex(current => current + 1)
|
|
|
|
}, [props.media, index, setIndex])
|
|
|
|
|
|
|
|
const previous = useCallback(() => {
|
|
|
|
if (index <= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setIndex(current => current - 1)
|
|
|
|
}, [props.media, index, setIndex])
|
|
|
|
|
|
|
|
const handleKeyPress = useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
if (props.open) {
|
|
|
|
if (event.key === 'ArrowLeft') {
|
|
|
|
previous()
|
|
|
|
} else if (event.key === 'ArrowRight') {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[props.open, previous, next]
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.addEventListener('keydown', handleKeyPress)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handleKeyPress)
|
|
|
|
}
|
|
|
|
}, [handleKeyPress])
|
|
|
|
|
2023-12-02 11:26:45 +01:00
|
|
|
return (
|
2024-01-09 13:30:05 +01:00
|
|
|
<Dialog open={props.open} handler={props.close} size="lg">
|
|
|
|
<DialogBody className="max-h-full max-w-full">
|
2024-03-09 07:00:27 +01:00
|
|
|
<div className="flex">
|
2024-03-14 17:12:19 +01:00
|
|
|
<Button variant="text" color="teal" onClick={previous} disabled={index < 1}>
|
|
|
|
<FaChevronLeft className="text-lg" />
|
2024-03-09 07:00:27 +01:00
|
|
|
</Button>
|
|
|
|
{props.media[index] && (
|
|
|
|
<img
|
|
|
|
src={props.media[index].url}
|
|
|
|
alt={props.media[index].description}
|
|
|
|
title={props.media[index].description}
|
|
|
|
className="object-contain max-h-full m-auto"
|
|
|
|
style={{ maxWidth: '85%' }}
|
|
|
|
/>
|
|
|
|
)}
|
2024-03-14 17:12:19 +01:00
|
|
|
<Button variant="text" color="teal" onClick={next} disabled={index >= props.media.length - 1}>
|
|
|
|
<FaChevronRight className="text-lg" />
|
2024-03-09 07:00:27 +01:00
|
|
|
</Button>
|
|
|
|
</div>
|
2024-01-09 13:30:05 +01:00
|
|
|
</DialogBody>
|
|
|
|
</Dialog>
|
2023-12-02 11:26:45 +01:00
|
|
|
)
|
|
|
|
}
|