'use client'; import React, { useState, useRef, useEffect, useCallback } from 'react'; import Link from 'next/link'; import { ArrowRight, Sparkles } from 'lucide-react'; import { TextAnimate } from '@/components/magicui/text-animate'; import { ToolItem } from '@/types'; export interface ToolMediaItem { id: string; slug: string; title: string; mediaType: 'video' | 'photo' | 'comparison'; videoUrl?: string; poster?: string; imageUrl?: string; beforeUrl?: string; afterUrl?: string; } export interface ShiftableToolsGridProps { title?: string; subtitle?: string; tools?: ToolItem[]; items?: ToolMediaItem[]; locale?: string; } export const DEFAULT_TOOL_ITEMS: ToolMediaItem[] = [ // ROW 1 (4 items) - Landscape 16:9 assets { id: 'tool-blemish', slug: 'blemish-remover', title: 'Skin & Blemish Retoucher', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/blemish-remover/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/blemish-remover/after.jpg', }, { id: 'tool-bg-remover', slug: 'background-remover', title: 'Background Remover (Alpha Matting)', mediaType: 'video', videoUrl: '/management/pages/home/sections/our-tools/background-remover/sample_reel.mp4', poster: '/management/pages/home/sections/our-tools/background-remover/poster.jpg', }, { id: 'tool-enhancer', slug: 'photo-enhancer', title: 'Photo Enhancer 4K', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/photo-enhancer/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/photo-enhancer/after.jpg', }, { id: 'tool-restorer', slug: 'old-photo-restorer', title: 'Vintage Photo Restorer', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/old-photo-restorer/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/old-photo-restorer/after.jpg', }, // ROW 2 (4 items) - Landscape 16:9 assets { id: 'tool-colorization', slug: 'colorization', title: 'AI Colorizer', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/colorization/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/colorization/after.jpg', }, { id: 'tool-teeth', slug: 'teeth-whitener', title: 'Teeth Whitener & Brightener', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/teeth-whitener/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/teeth-whitener/after.jpg', }, { id: 'tool-cartoon', slug: 'cartoon-avatar', title: 'Cartoon & Anime Avatar', mediaType: 'comparison', beforeUrl: '/management/pages/home/sections/our-tools/cartoon-avatar/before.jpg', afterUrl: '/management/pages/home/sections/our-tools/cartoon-avatar/after.jpg', }, { id: 'tool-polaroid', slug: 'polaroid-maker', title: 'Vintage Polaroid Maker', mediaType: 'photo', imageUrl: '/management/pages/home/sections/our-tools/polaroid-maker/image.jpg', }, ]; export function ShiftableToolsGridSection({ title = 'Our Tools', subtitle = 'Click on any tool window to try with your own photos in seconds.', items, }: ShiftableToolsGridProps) { const displayItems = items && items.length >= 8 ? items : DEFAULT_TOOL_ITEMS; const row1 = displayItems.slice(0, 4); const row2 = displayItems.slice(4, 8); // Expansion only starts when that section reaches the middle (top of section <= 50% of viewport) const [isMiddleReached, setIsMiddleReached] = useState(false); useEffect(() => { const checkPosition = () => { const section = document.getElementById('our-tools'); if (!section) return; const rect = section.getBoundingClientRect(); // Only start expansion once the section reaches the middle of the viewport const inMiddle = rect.top <= window.innerHeight * 0.5 && rect.bottom >= window.innerHeight * 0.15; setIsMiddleReached(inMiddle); }; checkPosition(); window.addEventListener('scroll', checkPosition, { passive: true }); window.addEventListener('resize', checkPosition, { passive: true }); return () => { window.removeEventListener('scroll', checkPosition); window.removeEventListener('resize', checkPosition); }; }, []); return (
{/* Symbolic Elemental Ambient Highlights */}
{/* Header */}
AI Creative Tools

{title}

{subtitle}

{/* Desktop View (md and up): 2-Row Shiftable Expansion Grid */}
{/* Mobile / Phone View (
{/* See More Section Button Redirecting to /tools */}
See More Tools
); } function MobileVerticalToolsList({ items }: { items: ToolMediaItem[] }) { return (
{items.map((item) => (
{item.mediaType === 'comparison' ? (
TRY TOOL
) : ( )} {/* Title Below Window */} {item.title}
))}
); } function ShiftableRow({ items, isEnabled = true }: { items: ToolMediaItem[]; isEnabled?: boolean }) { const [hoveredId, setHoveredId] = useState(null); // Clear any active hover expansion if the section leaves the active middle viewport zone useEffect(() => { if (!isEnabled) { setHoveredId(null); } }, [isEnabled]); return (
{items.map((item) => { const isHovered = isEnabled && hoveredId === item.id; const hasOtherHovered = isEnabled && hoveredId !== null && !isHovered; // Subtle, refined expansion (lesser shift, lesser expansion): // Hovered card: 1.28 flex-grow (~32% width, gentle expansion) // Other cards: 0.9066 flex-grow (~22.6% width, minimal displacement) // At rest or before middle reached: 1.0 flex-grow (25% width each) // Sum is mathematically conserved at exactly 4.0 (1.28 + 3 * 0.9066 = 4.00) const flexGrowValue = isHovered ? 1.28 : hasOtherHovered ? 0.9066 : 1; return (
{ if (isEnabled) setHoveredId(item.id); }} onMouseLeave={() => setHoveredId(null)} className="w-[82vw] max-w-[320px] md:max-w-none md:w-auto shrink-0 md:shrink md:flex-1 snap-start transition-[flex-grow] duration-500 ease-[cubic-bezier(0.16,1,0.3,1)] flex flex-col min-w-0" style={{ flexGrow: flexGrowValue, }} > {/* Window Container with strictly locked pixel height & landscape aspect */} {item.mediaType === 'comparison' ? (
{/* GPU-Composited Shadow Elevation Layer */} ) : ( {/* GPU-Composited Shadow Elevation Layer (Opacity-only transition, zero paint thrash) */} ); })}
); } function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered: boolean }) { const videoRef = useRef(null); const containerRef = useRef(null); const dividerRef = useRef(null); const clipLayerRef = useRef(null); const handleRef = useRef(null); // High performance refs - completely eliminates per-frame React re-renders! const isManualRef = useRef(false); const isPointerDownRef = useRef(false); const currentPercentRef = useRef(50); const animFrameRef = useRef(null); const pointerRafRef = useRef(null); const cachedRectRef = useRef<{ left: number; width: number } | null>(null); // Directly mutate DOM styles on the GPU layer without triggering React reconciliation const setSliderDOM = useCallback((percent: number) => { const clamped = Math.max(0, Math.min(100, percent)); currentPercentRef.current = clamped; if (dividerRef.current) { dividerRef.current.style.left = `${clamped}%`; } if (clipLayerRef.current) { clipLayerRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0 round 28px 0 0 28px)`; (clipLayerRef.current.style as any).webkitClipPath = `inset(0 ${100 - clamped}% 0 0 round 28px 0 0 28px)`; } if (handleRef.current) { handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`); } }, []); // Continuous Autoplay Muted Loop when visible in viewport // Pauses automatically when scrolled out of view or tab is hidden to save GPU/battery and eliminate device heating useEffect(() => { if (item.mediaType !== 'video') return; const video = videoRef.current; if (!video) return; video.muted = true; video.defaultMuted = true; video.playsInline = true; let isIntersecting = false; const updatePlayState = () => { if (isIntersecting && !document.hidden) { video.play().catch(() => {}); } else { video.pause(); } }; const observer = new IntersectionObserver( ([entry]) => { isIntersecting = entry.isIntersecting; updatePlayState(); }, { threshold: 0.05 } ); observer.observe(video); const handleVisibility = () => { updatePlayState(); }; document.addEventListener('visibilitychange', handleVisibility); return () => { observer.disconnect(); document.removeEventListener('visibilitychange', handleVisibility); video.pause(); }; }, [item.mediaType]); // When unhovered, reset slider smoothly to 50% useEffect(() => { if (item.mediaType !== 'comparison') return; if (!isHovered) { isManualRef.current = false; if (animFrameRef.current) { cancelAnimationFrame(animFrameRef.current); animFrameRef.current = null; } setSliderDOM(50); } }, [isHovered, item.mediaType, setSliderDOM]); // Instant pixel-precise cursor alignment calculation const updateToClientX = useCallback((clientX: number) => { if (!containerRef.current) return; const rect = cachedRectRef.current || containerRef.current.getBoundingClientRect(); if (rect.width <= 0) return; const x = Math.max(0, Math.min(clientX - rect.left, rect.width)); const percent = (x / rect.width) * 100; setSliderDOM(percent); }, [setSliderDOM]); // Desktop Mouse Movement: Immediately aligns comparison bar with cursor on hover and stays locked const handleContainerPointerEnter = (e: React.PointerEvent | React.MouseEvent) => { if (item.mediaType !== 'comparison') return; if (!('pointerType' in e) || e.pointerType === 'mouse') { isManualRef.current = true; updateToClientX(e.clientX); } }; const handleContainerPointerMove = (e: React.PointerEvent | React.MouseEvent) => { if (item.mediaType !== 'comparison') return; if (!('pointerType' in e) || e.pointerType === 'mouse') { isManualRef.current = true; const clientX = e.clientX; if (pointerRafRef.current) cancelAnimationFrame(pointerRafRef.current); pointerRafRef.current = requestAnimationFrame(() => { updateToClientX(clientX); }); } }; const handleContainerPointerLeave = (e: React.PointerEvent | React.MouseEvent) => { if (isPointerDownRef.current) return; if (!('pointerType' in e) || e.pointerType === 'mouse') { isManualRef.current = false; if (pointerRafRef.current) { cancelAnimationFrame(pointerRafRef.current); pointerRafRef.current = null; } setSliderDOM(50); } }; // Dedicated Drag Handlers for Center Arrow Handle (Works on mobile touch & desktop click-drag) const handleHandlePointerDown = (e: React.PointerEvent) => { if (item.mediaType !== 'comparison') return; if (e.button !== 0 && e.pointerType === 'mouse') return; e.preventDefault(); e.stopPropagation(); isManualRef.current = true; isPointerDownRef.current = true; if (animFrameRef.current) { cancelAnimationFrame(animFrameRef.current); animFrameRef.current = null; } try { e.currentTarget.setPointerCapture(e.pointerId); } catch { // Fallback } if (containerRef.current) { const r = containerRef.current.getBoundingClientRect(); cachedRectRef.current = { left: r.left, width: r.width }; } updateToClientX(e.clientX); }; const handleHandlePointerMove = (e: React.PointerEvent) => { if (!isPointerDownRef.current) return; e.stopPropagation(); isManualRef.current = true; const clientX = e.clientX; if (pointerRafRef.current) { cancelAnimationFrame(pointerRafRef.current); } pointerRafRef.current = requestAnimationFrame(() => { updateToClientX(clientX); }); }; const handleHandlePointerUp = (e: React.PointerEvent) => { if (!isPointerDownRef.current) return; e.stopPropagation(); isPointerDownRef.current = false; cachedRectRef.current = null; if (pointerRafRef.current) { cancelAnimationFrame(pointerRafRef.current); pointerRafRef.current = null; } try { e.currentTarget.releasePointerCapture(e.pointerId); } catch { // Fallback } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (item.mediaType !== 'comparison') return; const step = e.shiftKey ? 10 : 2; if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { e.preventDefault(); isManualRef.current = true; setSliderDOM(currentPercentRef.current - step); } else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { e.preventDefault(); isManualRef.current = true; setSliderDOM(currentPercentRef.current + step); } }; // 1. VIDEO (Strictly locked to fixed 680px centered width - ZERO ZOOM) if (item.mediaType === 'video' && item.videoUrl) { return (