import React, { useState, useRef, useLayoutEffect, memo } from "react" import { useWindowSize } from "react-use" interface ThumbnailsProps { images: string[] style?: React.CSSProperties setImages?: React.Dispatch> onHeightChange?: (height: number) => void } const Thumbnails = ({ images, style, setImages, onHeightChange }: ThumbnailsProps) => { const [hoveredIndex, setHoveredIndex] = useState(null) const containerRef = useRef(null) const { width } = useWindowSize() useLayoutEffect(() => { if (containerRef.current) { let height = containerRef.current.clientHeight // some browsers return 0 for clientHeight if (!height) { height = containerRef.current.getBoundingClientRect().height } onHeightChange?.(height) } setHoveredIndex(null) }, [images, width, onHeightChange]) const handleDelete = (index: number) => { setImages?.((prevImages) => prevImages.filter((_, i) => i !== index)) } const isDeletable = setImages !== undefined return (
{images.map((image, index) => (
setHoveredIndex(index)} onMouseLeave={() => setHoveredIndex(null)}> {`Thumbnail {isDeletable && hoveredIndex === index && (
handleDelete(index)} style={{ position: "absolute", top: -4, right: -4, width: 13, height: 13, borderRadius: "50%", backgroundColor: "var(--vscode-badge-background)", display: "flex", justifyContent: "center", alignItems: "center", cursor: "pointer", }}>
)}
))}
) } export default memo(Thumbnails)