Skip to content

Commit bf184ac

Browse files
committed
fix: image and sidebar
1 parent 7fa1329 commit bf184ac

3 files changed

Lines changed: 134 additions & 1 deletion

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

src/components/Cards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { CustomerCard } from './CustomerCard'
33
export { EventCard } from './EventCard'
44
export { ProjectCard } from './ProjectCard'
55
export { Image } from './Image'
6+
export { ImageProxy } from './ImageProxy'
67
export * from './ServiceCard'
78
export * from './PricingCard'
89
export * from './HeroCard'

src/components/Menus/SidebarLeftMulti.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function SidebarLeftMulti({
158158
{mainNav &&
159159
!loading &&
160160
mainNav.map((item, index) => (
161-
<SidebarGroup>
161+
<SidebarGroup key={index}>
162162
<SidebarGroupLabel>{item.name}</SidebarGroupLabel>
163163
{ item && !loading && item.nav.map((item, index) => (
164164
<Menu key={index} subNav={item} pathName={pathName} Link={Link} />

0 commit comments

Comments
 (0)