Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

350 строки
13 KiB

  1. 'use client';
  2. import { useState, useEffect, useCallback, useRef } from 'react';
  3. import { Card, MediaItem } from '@/types';
  4. const isVideoUrl = (url: string) => /\.(mp4|webm|mov|m4v|ogv)(\?|$)/i.test(url);
  5. function MediaCarousel({
  6. items,
  7. onImageClick,
  8. }: {
  9. items: MediaItem[];
  10. onImageClick?: (index: number) => void;
  11. }) {
  12. const [current, setCurrent] = useState(0);
  13. const touchStartX = useRef<number | null>(null);
  14. const videoRefs = useRef<Record<number, HTMLVideoElement | null>>({});
  15. const prev = useCallback(() => setCurrent(i => (i - 1 + items.length) % items.length), [items.length]);
  16. const next = useCallback(() => setCurrent(i => (i + 1) % items.length), [items.length]);
  17. useEffect(() => {
  18. const onKey = (e: KeyboardEvent) => {
  19. if (e.key === 'ArrowLeft') prev();
  20. if (e.key === 'ArrowRight') next();
  21. };
  22. window.addEventListener('keydown', onKey);
  23. return () => window.removeEventListener('keydown', onKey);
  24. }, [prev, next]);
  25. useEffect(() => { setCurrent(0); }, [items]);
  26. // Pause non-current videos; autoplay current if flagged
  27. useEffect(() => {
  28. Object.entries(videoRefs.current).forEach(([key, vid]) => {
  29. if (!vid) return;
  30. const idx = parseInt(key, 10);
  31. if (idx !== current) { vid.pause(); return; }
  32. const item = items[idx];
  33. if (item && isVideoUrl(item.url) && item.autoplay) {
  34. vid.muted = true;
  35. vid.play().catch(() => {});
  36. }
  37. });
  38. }, [current, items]);
  39. const onTouchStart = (e: React.TouchEvent) => { touchStartX.current = e.touches[0].clientX; };
  40. const onTouchEnd = (e: React.TouchEvent) => {
  41. if (touchStartX.current === null) return;
  42. const delta = e.changedTouches[0].clientX - touchStartX.current;
  43. if (Math.abs(delta) > 50) delta < 0 ? next() : prev();
  44. touchStartX.current = null;
  45. };
  46. if (items.length === 0) {
  47. return <div className="h-72 w-full bg-gray-100 flex items-center justify-center text-gray-400 rounded-t-2xl">No Image</div>;
  48. }
  49. return (
  50. <div
  51. className="relative h-72 w-full bg-black overflow-hidden rounded-t-2xl select-none"
  52. onTouchStart={onTouchStart}
  53. onTouchEnd={onTouchEnd}
  54. >
  55. {items.map((item, i) => {
  56. const isActive = i === current;
  57. const video = isVideoUrl(item.url);
  58. return (
  59. <div
  60. key={item.url + i}
  61. className={`absolute inset-0 transition-opacity duration-300 ${isActive ? 'opacity-100' : 'opacity-0 pointer-events-none'}`}
  62. >
  63. {video ? (
  64. <video
  65. ref={el => { videoRefs.current[i] = el; }}
  66. src={item.url}
  67. className="w-full h-full object-contain bg-black"
  68. controls
  69. playsInline
  70. muted={!!item.autoplay}
  71. preload="metadata"
  72. />
  73. ) : (
  74. <img
  75. src={item.url}
  76. alt=""
  77. className="w-full h-full object-cover cursor-zoom-in"
  78. onClick={() => onImageClick?.(i)}
  79. title="Click to view fullscreen"
  80. />
  81. )}
  82. </div>
  83. );
  84. })}
  85. {items.length > 1 && (
  86. <>
  87. <button
  88. onClick={(e) => { e.stopPropagation(); prev(); }}
  89. className="absolute left-3 top-1/2 -translate-y-1/2 bg-black/50 hover:bg-black/80 text-white w-9 h-9 rounded-full flex items-center justify-center transition-colors shadow-lg z-10"
  90. aria-label="Previous"
  91. >‹</button>
  92. <button
  93. onClick={(e) => { e.stopPropagation(); next(); }}
  94. className="absolute right-3 top-1/2 -translate-y-1/2 bg-black/50 hover:bg-black/80 text-white w-9 h-9 rounded-full flex items-center justify-center transition-colors shadow-lg z-10"
  95. aria-label="Next"
  96. >›</button>
  97. <div className="absolute bottom-3 left-1/2 -translate-x-1/2 flex items-center gap-1.5 z-10">
  98. {items.map((_, i) => (
  99. <button
  100. key={i}
  101. onClick={(e) => { e.stopPropagation(); setCurrent(i); }}
  102. className={`rounded-full transition-all duration-200 ${i === current ? 'w-5 h-2 bg-white' : 'w-2 h-2 bg-white/50 hover:bg-white/80'}`}
  103. aria-label={`Go to slide ${i + 1}`}
  104. />
  105. ))}
  106. </div>
  107. <div className="absolute top-3 right-3 bg-black/50 text-white text-xs font-semibold px-2 py-0.5 rounded-full z-10">
  108. {current + 1} / {items.length}
  109. </div>
  110. </>
  111. )}
  112. </div>
  113. );
  114. }
  115. function FullscreenViewer({
  116. items,
  117. startIndex,
  118. onClose,
  119. }: {
  120. items: MediaItem[];
  121. startIndex: number;
  122. onClose: () => void;
  123. }) {
  124. const [current, setCurrent] = useState(startIndex);
  125. const touchStartX = useRef<number | null>(null);
  126. const prev = useCallback(() => setCurrent(i => (i - 1 + items.length) % items.length), [items.length]);
  127. const next = useCallback(() => setCurrent(i => (i + 1) % items.length), [items.length]);
  128. useEffect(() => {
  129. const onKey = (e: KeyboardEvent) => {
  130. if (e.key === 'Escape') onClose();
  131. else if (e.key === 'ArrowLeft') prev();
  132. else if (e.key === 'ArrowRight') next();
  133. };
  134. window.addEventListener('keydown', onKey);
  135. return () => window.removeEventListener('keydown', onKey);
  136. }, [prev, next, onClose]);
  137. const onTouchStart = (e: React.TouchEvent) => { touchStartX.current = e.touches[0].clientX; };
  138. const onTouchEnd = (e: React.TouchEvent) => {
  139. if (touchStartX.current === null) return;
  140. const delta = e.changedTouches[0].clientX - touchStartX.current;
  141. if (Math.abs(delta) > 50) delta < 0 ? next() : prev();
  142. touchStartX.current = null;
  143. };
  144. return (
  145. <div
  146. className="fixed inset-0 z-[100] bg-black/95 flex items-center justify-center select-none animate-in fade-in duration-200"
  147. onTouchStart={onTouchStart}
  148. onTouchEnd={onTouchEnd}
  149. >
  150. {/* Media — full resolution, contained */}
  151. {items.map((item, i) => {
  152. const isActive = i === current;
  153. const video = isVideoUrl(item.url);
  154. return (
  155. <div
  156. key={item.url + i}
  157. className={`absolute inset-0 flex items-center justify-center p-4 pb-28 transition-opacity duration-200 ${isActive ? 'opacity-100' : 'opacity-0 pointer-events-none'}`}
  158. >
  159. {video ? (
  160. <video
  161. src={item.url}
  162. className="max-w-full max-h-full"
  163. controls
  164. playsInline
  165. autoPlay={!!item.autoplay}
  166. muted={!!item.autoplay}
  167. />
  168. ) : (
  169. <img
  170. src={item.url}
  171. alt=""
  172. className="max-w-full max-h-full object-contain"
  173. />
  174. )}
  175. </div>
  176. );
  177. })}
  178. {/* Counter — top center */}
  179. {items.length > 1 && (
  180. <div className="absolute top-4 left-1/2 -translate-x-1/2 bg-black/60 text-white text-sm font-semibold px-3 py-1 rounded-full z-20">
  181. {current + 1} / {items.length}
  182. </div>
  183. )}
  184. {/* Side arrows */}
  185. {items.length > 1 && (
  186. <>
  187. <button
  188. onClick={(e) => { e.stopPropagation(); prev(); }}
  189. className="absolute left-4 top-1/2 -translate-y-1/2 bg-black/50 hover:bg-black/80 text-white w-12 h-12 rounded-full flex items-center justify-center text-2xl shadow-lg z-20"
  190. aria-label="Previous"
  191. >‹</button>
  192. <button
  193. onClick={(e) => { e.stopPropagation(); next(); }}
  194. className="absolute right-4 top-1/2 -translate-y-1/2 bg-black/50 hover:bg-black/80 text-white w-12 h-12 rounded-full flex items-center justify-center text-2xl shadow-lg z-20"
  195. aria-label="Next"
  196. >›</button>
  197. </>
  198. )}
  199. {/* Close button — bottom center, ABOVE the dots */}
  200. <button
  201. onClick={onClose}
  202. className="absolute bottom-12 left-1/2 -translate-x-1/2 bg-white/90 hover:bg-white text-black px-6 py-2.5 rounded-full font-semibold shadow-2xl flex items-center gap-2 z-20 transition-transform hover:scale-105"
  203. aria-label="Close fullscreen"
  204. >
  205. <span className="text-lg leading-none">✕</span>
  206. <span>Close</span>
  207. </button>
  208. {/* Dots — at the very bottom */}
  209. {items.length > 1 && (
  210. <div className="absolute bottom-4 left-1/2 -translate-x-1/2 flex items-center gap-1.5 z-20">
  211. {items.map((_, i) => (
  212. <button
  213. key={i}
  214. onClick={(e) => { e.stopPropagation(); setCurrent(i); }}
  215. className={`rounded-full transition-all duration-200 ${i === current ? 'w-6 h-2 bg-white' : 'w-2 h-2 bg-white/50 hover:bg-white/80'}`}
  216. aria-label={`Go to slide ${i + 1}`}
  217. />
  218. ))}
  219. </div>
  220. )}
  221. </div>
  222. );
  223. }
  224. export default function PublicGrid({ cards, maxCols = 5 }: { cards: Card[], maxCols?: number }) {
  225. const [activeCard, setActiveCard] = useState<Card | null>(null);
  226. const [fullscreenIndex, setFullscreenIndex] = useState<number | null>(null);
  227. useEffect(() => {
  228. if (activeCard || fullscreenIndex !== null) document.body.style.overflow = 'hidden';
  229. else document.body.style.overflow = 'unset';
  230. return () => { document.body.style.overflow = 'unset'; };
  231. }, [activeCard, fullscreenIndex]);
  232. const gridClasses: Record<number, string> = {
  233. 3: 'xl:grid-cols-3 lg:grid-cols-3 md:grid-cols-2 grid-cols-1',
  234. 4: 'xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1',
  235. 5: 'xl:grid-cols-5 lg:grid-cols-4 md:grid-cols-3 grid-cols-1',
  236. 6: 'xl:grid-cols-6 lg:grid-cols-4 md:grid-cols-3 grid-cols-2',
  237. 7: 'xl:grid-cols-7 lg:grid-cols-5 md:grid-cols-4 grid-cols-2',
  238. 8: 'xl:grid-cols-8 lg:grid-cols-6 md:grid-cols-4 grid-cols-2',
  239. };
  240. const activeGridClass = gridClasses[maxCols] || gridClasses[5];
  241. const carouselItems: MediaItem[] = activeCard
  242. ? [
  243. ...(activeCard.imageUrl ? [{ url: activeCard.imageUrl }] : []),
  244. ...(activeCard.extraMedia || []),
  245. ]
  246. : [];
  247. return (
  248. <>
  249. <div className={`grid gap-4 ${activeGridClass}`}>
  250. {cards.map((card) => {
  251. const galleryCount = (card.extraMedia?.length || 0) + (card.imageUrl ? 1 : 0);
  252. return (
  253. <div
  254. key={card.id}
  255. onClick={() => setActiveCard(card)}
  256. className="group relative cursor-pointer overflow-hidden rounded-xl shadow-md aspect-square bg-gray-200 transition-all duration-300 hover:shadow-xl hover:-translate-y-1"
  257. >
  258. {card.imageUrl ? (
  259. <img src={card.imageUrl} alt={card.title} className="absolute inset-0 w-full h-full object-cover" />
  260. ) : (
  261. <div className="absolute inset-0 flex items-center justify-center bg-gradient-to-br from-blue-100 to-gray-200 text-gray-400">No Image</div>
  262. )}
  263. {galleryCount > 1 && (
  264. <div className="absolute top-2 right-2 bg-black/60 text-white text-xs font-semibold px-1.5 py-0.5 rounded-full flex items-center gap-1">
  265. <span>⊞</span>
  266. <span>{galleryCount}</span>
  267. </div>
  268. )}
  269. <div className="absolute inset-0 bg-gradient-to-t from-black/90 via-black/30 to-transparent flex flex-col justify-end p-5 text-white">
  270. <h3 className="text-xl font-bold drop-shadow-md">{card.title}</h3>
  271. <p className="text-sm opacity-0 group-hover:opacity-100 transition-opacity duration-300 line-clamp-2 mt-1 text-gray-200 drop-shadow">
  272. {card.shortDescription}
  273. </p>
  274. </div>
  275. </div>
  276. );
  277. })}
  278. </div>
  279. {activeCard && (
  280. <div
  281. className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 backdrop-blur-md p-4"
  282. onClick={() => setActiveCard(null)}
  283. >
  284. <div
  285. className="bg-white rounded-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto shadow-2xl animate-in fade-in zoom-in-95 duration-200"
  286. onClick={(e) => e.stopPropagation()}
  287. >
  288. <div className="relative">
  289. <MediaCarousel
  290. items={carouselItems}
  291. onImageClick={(i) => setFullscreenIndex(i)}
  292. />
  293. <button
  294. onClick={() => setActiveCard(null)}
  295. className="absolute top-4 right-4 bg-black/60 text-white w-10 h-10 flex items-center justify-center rounded-full hover:bg-black hover:scale-110 transition-all shadow-lg z-20"
  296. title="Close"
  297. >✕</button>
  298. </div>
  299. <div className="p-8">
  300. <div className="text-xs text-blue-600 font-bold tracking-wider uppercase mb-2">{activeCard.cardType.replace('_', ' ')}</div>
  301. <h2 className="text-3xl font-bold mb-4 text-gray-900">{activeCard.title}</h2>
  302. {activeCard.fullContent ? (
  303. <div className="prose max-w-none text-gray-700" dangerouslySetInnerHTML={{ __html: activeCard.fullContent }} />
  304. ) : (
  305. <p className="text-gray-500 italic text-lg">{activeCard.shortDescription}</p>
  306. )}
  307. </div>
  308. </div>
  309. </div>
  310. )}
  311. {fullscreenIndex !== null && activeCard && (
  312. <FullscreenViewer
  313. items={carouselItems}
  314. startIndex={fullscreenIndex}
  315. onClose={() => setFullscreenIndex(null)}
  316. />
  317. )}
  318. </>
  319. );
  320. }