|
| 1 | +'use client'; |
| 2 | +import Image from 'next/image'; |
| 3 | +import React, { useEffect, useRef, useState } from 'react'; |
| 4 | +import { AnimatePresence, motion } from 'framer-motion'; |
| 5 | +import { X } from 'lucide-react'; |
| 6 | + |
| 7 | +export function ExpandableCard({ projects }) { |
| 8 | + const [active, setActive] = useState(null); |
| 9 | + const [iframeUrl, setIframeUrl] = useState(''); |
| 10 | + const ref = useRef<HTMLDivElement>(null); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + function onKeyDown(event: KeyboardEvent) { |
| 14 | + if (event.key === 'Escape') { |
| 15 | + setActive(null); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + if (active && typeof active === 'object') { |
| 20 | + document.body.style.overflow = 'hidden'; |
| 21 | + } else { |
| 22 | + document.body.style.overflow = 'auto'; |
| 23 | + } |
| 24 | + |
| 25 | + window.addEventListener('keydown', onKeyDown); |
| 26 | + return () => window.removeEventListener('keydown', onKeyDown); |
| 27 | + }, [active]); |
| 28 | + |
| 29 | + const getWebUrl = async (project) => { |
| 30 | + if (!project) return; |
| 31 | + console.log('project:', project); |
| 32 | + const projectPath = project.path; |
| 33 | + |
| 34 | + try { |
| 35 | + const response = await fetch( |
| 36 | + `/api/runProject?projectPath=${encodeURIComponent(projectPath)}`, |
| 37 | + { |
| 38 | + method: 'GET', |
| 39 | + headers: { |
| 40 | + 'Content-Type': 'application/json', |
| 41 | + }, |
| 42 | + } |
| 43 | + ); |
| 44 | + const json = await response.json(); |
| 45 | + const baseUrl = `http://${json.domain}`; |
| 46 | + setIframeUrl(baseUrl); |
| 47 | + setActive(project); |
| 48 | + } catch (error) { |
| 49 | + console.error('fetching url error:', error); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <AnimatePresence mode="wait"> |
| 56 | + {active && ( |
| 57 | + <motion.div |
| 58 | + onClick={() => setActive(null)} |
| 59 | + initial={{ opacity: 0 }} |
| 60 | + animate={{ opacity: 1 }} |
| 61 | + exit={{ opacity: 0 }} |
| 62 | + transition={{ |
| 63 | + duration: 0.3, |
| 64 | + ease: [0.4, 0, 0.2, 1], |
| 65 | + }} |
| 66 | + className="fixed inset-0 backdrop-blur-[2px] bg-black/20 h-full w-full z-50" |
| 67 | + style={{ willChange: 'opacity' }} |
| 68 | + /> |
| 69 | + )} |
| 70 | + </AnimatePresence> |
| 71 | + |
| 72 | + <AnimatePresence mode="wait"> |
| 73 | + {active ? ( |
| 74 | + <div className="fixed inset-0 grid place-items-center z-[80] m-4"> |
| 75 | + <motion.button |
| 76 | + initial={{ opacity: 0, scale: 0.9 }} |
| 77 | + animate={{ opacity: 1, scale: 1 }} |
| 78 | + exit={{ opacity: 0, scale: 0.9 }} |
| 79 | + transition={{ duration: 0.2 }} |
| 80 | + className="flex absolute top-4 right-4 items-center justify-center bg-white/90 hover:bg-white rounded-full h-8 w-8" |
| 81 | + onClick={() => setActive(null)} |
| 82 | + > |
| 83 | + <X className="h-4 w-4 z-50" /> |
| 84 | + </motion.button> |
| 85 | + |
| 86 | + <motion.div |
| 87 | + layoutId={`card-${active.id}`} |
| 88 | + ref={ref} |
| 89 | + className="w-full h-full flex flex-col bg-white dark:bg-neutral-900 rounded-2xl overflow-hidden" |
| 90 | + style={{ willChange: 'transform, opacity' }} |
| 91 | + > |
| 92 | + <motion.div className="flex-1 p-6 h-[50%]"> |
| 93 | + <motion.div |
| 94 | + layoutId={`content-${active.id}`} |
| 95 | + className="h-full" |
| 96 | + > |
| 97 | + <motion.h3 |
| 98 | + layoutId={`title-${active.id}`} |
| 99 | + className="text-xl font-semibold text-gray-900 dark:text-gray-100" |
| 100 | + > |
| 101 | + {active.name} |
| 102 | + </motion.h3> |
| 103 | + <motion.div |
| 104 | + layoutId={`meta-${active.id}`} |
| 105 | + className="mt-2 w-full h-full" |
| 106 | + > |
| 107 | + <iframe |
| 108 | + src={iframeUrl} |
| 109 | + className="w-full h-[100%]" |
| 110 | + title="Project Preview" |
| 111 | + /> |
| 112 | + </motion.div> |
| 113 | + </motion.div> |
| 114 | + </motion.div> |
| 115 | + </motion.div> |
| 116 | + </div> |
| 117 | + ) : null} |
| 118 | + </AnimatePresence> |
| 119 | + |
| 120 | + <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6"> |
| 121 | + {projects.map((project) => ( |
| 122 | + <motion.div |
| 123 | + key={project.id} |
| 124 | + layoutId={`card-${project.id}`} |
| 125 | + onClick={() => getWebUrl(project)} |
| 126 | + className="group cursor-pointer" |
| 127 | + > |
| 128 | + <motion.div |
| 129 | + layoutId={`image-container-${project.id}`} |
| 130 | + className="relative rounded-xl overflow-hidden" |
| 131 | + > |
| 132 | + <motion.div layoutId={`image-${project.id}`}> |
| 133 | + <Image |
| 134 | + src={project.image} |
| 135 | + alt={project.name} |
| 136 | + width={600} |
| 137 | + height={200} |
| 138 | + className="w-full h-48 object-cover transition duration-300 group-hover:scale-105" |
| 139 | + /> |
| 140 | + </motion.div> |
| 141 | + |
| 142 | + <motion.div |
| 143 | + initial={{ opacity: 0 }} |
| 144 | + whileHover={{ opacity: 1 }} |
| 145 | + transition={{ duration: 0.2 }} |
| 146 | + className="absolute inset-0 bg-black/40 flex items-center justify-center" |
| 147 | + > |
| 148 | + <span className="text-white font-medium px-4 py-2 rounded-lg bg-white/20 backdrop-blur-sm"> |
| 149 | + View Project |
| 150 | + </span> |
| 151 | + </motion.div> |
| 152 | + </motion.div> |
| 153 | + |
| 154 | + <motion.div layoutId={`content-${project.id}`} className="mt-3"> |
| 155 | + <motion.h3 |
| 156 | + layoutId={`title-${project.id}`} |
| 157 | + className="font-medium text-gray-900 dark:text-gray-100" |
| 158 | + > |
| 159 | + {project.name} |
| 160 | + </motion.h3> |
| 161 | + <motion.div |
| 162 | + layoutId={`meta-${project.id}`} |
| 163 | + className="mt-1 text-sm text-gray-500" |
| 164 | + > |
| 165 | + {project.author} |
| 166 | + </motion.div> |
| 167 | + </motion.div> |
| 168 | + </motion.div> |
| 169 | + ))} |
| 170 | + </div> |
| 171 | + </> |
| 172 | + ); |
| 173 | +} |
0 commit comments