Whalebird-desktop-client-ma.../renderer/components/Media.tsx

79 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

import { Button, Dialog, DialogBody } from '@material-tailwind/react'
2023-12-02 11:26:45 +01:00
import { Entity } from 'megalodon'
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
index: number
media: Array<Entity.Attachment>
2023-12-02 11:26:45 +01:00
}
export default function Media(props: Props) {
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 (
<Dialog open={props.open} handler={props.close} size="lg">
<DialogBody className="max-h-full max-w-full">
<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" />
</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" />
</Button>
</div>
</DialogBody>
</Dialog>
2023-12-02 11:26:45 +01:00
)
}