import { Button, Dialog, DialogBody } from '@material-tailwind/react' import { Entity } from 'megalodon' import { useState, useEffect, useCallback } from 'react' import { FaChevronLeft, FaChevronRight } from 'react-icons/fa6' type Props = { open: boolean close: () => void index: number media: Array } export default function Media(props: Props) { const [index, setIndex] = useState(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]) return (
{props.media[index] && ( {props.media[index].description} )}
) }