|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { |
| 4 | + Dialog, |
| 5 | + DialogTitle, |
| 6 | + DialogContent, |
| 7 | + DialogHeader, |
| 8 | + DialogClose, |
| 9 | +} from '@/components/ui/dialog' |
| 10 | +import { X } from 'lucide-react' // Using Lucide Icons as a replacement for MUI icons |
| 11 | +import NextImage from 'next/image' |
| 12 | +import path from 'path' |
| 13 | +import React, { useState } from 'react' |
| 14 | + |
| 15 | +type Menu ={ |
| 16 | + component: string |
| 17 | + collection: string | null |
| 18 | + scope?: string |
| 19 | +} |
| 20 | + |
| 21 | +type LinkedItem ={ |
| 22 | + repo: string |
| 23 | + owner: string |
| 24 | + branch: string |
| 25 | + path: string |
| 26 | +} |
| 27 | + |
| 28 | +type ContentItem ={ |
| 29 | + source: string |
| 30 | + repo: string |
| 31 | + owner: string |
| 32 | + branch: string |
| 33 | + path: string |
| 34 | + reference: string |
| 35 | + icon?: React.ComponentType<React.ComponentProps<'svg'>> | React.JSX.Element |
| 36 | + collections?: string[] |
| 37 | + menu?: Menu |
| 38 | + file?: string |
| 39 | + linked?: LinkedItem |
| 40 | +} |
| 41 | + |
| 42 | +function isExternalUrl(url: string) { |
| 43 | + return url.startsWith('http') |
| 44 | +} |
| 45 | + |
| 46 | +function getAPIUrl(src: string, baseContext: string) { |
| 47 | + let url = src |
| 48 | + if (isExternalUrl(src)) { |
| 49 | + url = src |
| 50 | + } else { |
| 51 | + let newSrc = src.replace('./', '') |
| 52 | + |
| 53 | + if (newSrc.startsWith('/')) { |
| 54 | + newSrc = newSrc.slice(1) |
| 55 | + } |
| 56 | + |
| 57 | + url = baseContext + '/' + newSrc |
| 58 | + } |
| 59 | + return url |
| 60 | +} |
| 61 | + |
| 62 | +function ImageComponent({ src, alt }: { src: string; alt: string }) { |
| 63 | + const [open, setOpen] = useState(false) |
| 64 | + const [zoomable, setZoomable] = useState(false) |
| 65 | + const [imageSize, setImageSize] = useState({ width: 0, height: 0 }) |
| 66 | + |
| 67 | + const handleClickOpen = () => { |
| 68 | + if (zoomable) { |
| 69 | + setOpen(true) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const handleClose = () => { |
| 74 | + setOpen(false) |
| 75 | + } |
| 76 | + |
| 77 | + const handleImageLoaded = (event: any) => { |
| 78 | + const { naturalWidth, naturalHeight } = event.target |
| 79 | + setImageSize({ width: naturalWidth, height: naturalHeight }) |
| 80 | + setZoomable(naturalWidth > 300 || naturalHeight > 300) |
| 81 | + } |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + <div |
| 86 | + className={`relative mx-auto my-4 flex justify-center items-center cursor-pointer ${ |
| 87 | + imageSize.height > 300 ? 'h-[300px]' : `h-[${imageSize.height}px]` |
| 88 | + }`} |
| 89 | + onClick={handleClickOpen} |
| 90 | + > |
| 91 | + <NextImage |
| 92 | + sizes="100vw" |
| 93 | + height={imageSize.height} |
| 94 | + width={imageSize.width} |
| 95 | + src={src} |
| 96 | + alt={alt || ''} |
| 97 | + onLoad={handleImageLoaded} |
| 98 | + unoptimized |
| 99 | + className="object-contain max-w-full max-h-full" |
| 100 | + /> |
| 101 | + </div> |
| 102 | + |
| 103 | + <Dialog open={open} onOpenChange={setOpen}> |
| 104 | + <DialogContent className="flex flex-col items-center justify-center"> |
| 105 | + <DialogHeader className="flex w-full justify-end"> |
| 106 | + <DialogTitle className="hidden">{alt}</DialogTitle> |
| 107 | + <DialogClose /> |
| 108 | + </DialogHeader> |
| 109 | + |
| 110 | + <div className="flex items-center justify-center"> |
| 111 | + <NextImage |
| 112 | + height={imageSize.height} |
| 113 | + width={imageSize.width} |
| 114 | + src={src} |
| 115 | + alt={alt || ''} |
| 116 | + unoptimized |
| 117 | + className="object-contain" |
| 118 | + /> |
| 119 | + </div> |
| 120 | + </DialogContent> |
| 121 | + </Dialog> |
| 122 | + </> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +export function ImageProxy({ props, baseContext }: { props: any; baseContext: string }) { |
| 127 | + let { src } = props |
| 128 | + |
| 129 | + src = getAPIUrl(src, baseContext) |
| 130 | + |
| 131 | + return <ImageComponent src={src} alt={props.alt} /> |
| 132 | +} |
0 commit comments