|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useRef, useEffect, useCallback, RefObject } from "react"; |
| 4 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { cn, noReturnDebounce } from "@/lib/utils"; |
| 7 | + |
| 8 | +const IFRAME_DEFAULTS = { |
| 9 | + WIDTH: 1338, |
| 10 | + HEIGHT: 600, |
| 11 | +} as const; |
| 12 | + |
| 13 | +function calculateResponsiveScale(containerWidth: number, sourceWidth: number): number { |
| 14 | + if (!containerWidth || !sourceWidth) return 1; |
| 15 | + return Math.min(containerWidth / sourceWidth, 1); |
| 16 | +} |
| 17 | + |
| 18 | +function createScaleTransformStyle(scale: number) { |
| 19 | + return { |
| 20 | + transform: `scale(${scale})`, |
| 21 | + transformOrigin: "top left", |
| 22 | + width: `${100 / scale}%`, |
| 23 | + height: `${100 / scale}%`, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A hook to calculate the responsive scale of an element based on its container's width. |
| 29 | + */ |
| 30 | +function useResponsiveScale(targetRef: RefObject<HTMLElement | null>, sourceWidth: number): number { |
| 31 | + const [scale, setScale] = useState(1); |
| 32 | + |
| 33 | + const updateScale = useCallback(() => { |
| 34 | + if (!targetRef.current) return; |
| 35 | + const newScale = calculateResponsiveScale(targetRef.current.clientWidth, sourceWidth); |
| 36 | + setScale(newScale); |
| 37 | + }, [targetRef, sourceWidth]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + updateScale(); |
| 41 | + const debouncedUpdate = noReturnDebounce(updateScale); |
| 42 | + window.addEventListener("resize", debouncedUpdate); |
| 43 | + return () => window.removeEventListener("resize", debouncedUpdate); |
| 44 | + }, [updateScale]); |
| 45 | + |
| 46 | + return scale; |
| 47 | +} |
| 48 | + |
| 49 | +interface ResourceIframeProps { |
| 50 | + websiteUrl: string; |
| 51 | + title: string; |
| 52 | + className?: string; |
| 53 | + scale?: number; |
| 54 | + desktopWidth?: number; |
| 55 | + containerHeight?: number; |
| 56 | +} |
| 57 | + |
| 58 | +export function ResourceIframe({ |
| 59 | + websiteUrl, |
| 60 | + title, |
| 61 | + className, |
| 62 | + scale: forcedScale, |
| 63 | + desktopWidth = IFRAME_DEFAULTS.WIDTH, |
| 64 | + containerHeight = IFRAME_DEFAULTS.HEIGHT, |
| 65 | +}: ResourceIframeProps) { |
| 66 | + const [isLoading, setIsLoading] = useState(true); |
| 67 | + const containerRef = useRef<HTMLDivElement>(null); |
| 68 | + const calculatedScale = useResponsiveScale(containerRef, desktopWidth); |
| 69 | + |
| 70 | + const finalScale = forcedScale ?? calculatedScale; |
| 71 | + const scaleWrapperStyle = createScaleTransformStyle(finalScale); |
| 72 | + |
| 73 | + return ( |
| 74 | + <Card className={cn("w-full", className)}> |
| 75 | + <CardHeader> |
| 76 | + <div className="flex flex-wrap items-center justify-between gap-4"> |
| 77 | + <CardTitle>{title}</CardTitle> |
| 78 | + <Button |
| 79 | + asChild |
| 80 | + variant="outline" |
| 81 | + > |
| 82 | + <a |
| 83 | + href={websiteUrl} |
| 84 | + target="_blank" |
| 85 | + rel="noopener" |
| 86 | + > |
| 87 | + Visit Full Website → |
| 88 | + </a> |
| 89 | + </Button> |
| 90 | + </div> |
| 91 | + </CardHeader> |
| 92 | + <CardContent> |
| 93 | + <div |
| 94 | + ref={containerRef} |
| 95 | + className="relative hidden w-full overflow-hidden rounded-lg border md:block" |
| 96 | + style={{ height: containerHeight }} |
| 97 | + > |
| 98 | + {isLoading && ( |
| 99 | + <div className="absolute inset-0 z-10 flex items-center justify-center bg-muted"> |
| 100 | + <p className="text-muted-foreground">Loading website...</p> |
| 101 | + </div> |
| 102 | + )} |
| 103 | + <div |
| 104 | + className="h-full w-full" |
| 105 | + style={scaleWrapperStyle} |
| 106 | + > |
| 107 | + <iframe |
| 108 | + src={websiteUrl} |
| 109 | + title={title} |
| 110 | + allowFullScreen |
| 111 | + className="h-full w-full border-0" |
| 112 | + onLoad={() => setIsLoading(false)} |
| 113 | + /> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </CardContent> |
| 117 | + </Card> |
| 118 | + ); |
| 119 | +} |
0 commit comments