'use client';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import Link from 'next/link';
import { ArrowRight } from 'lucide-react';
import { ToolItem } from '@/types';
import { KineticText } from '@/components/magicui/kinetic-text';
import { AnimatedShinyText } from '@/components/magicui/animated-shiny-text';
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)
{
id: 'tool-blemish',
slug: 'blemish-remover',
title: 'AI Blemish Remover',
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: 'AI Background Remover',
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: 'Ultra HD AI Photo Enhancer',
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)
{
id: 'tool-colorization',
slug: 'colorization',
title: 'Black & White Photo 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: 'AI Teeth Whitener & Smile Glow',
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: 'CartoonGAN & 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 & Film Filter',
mediaType: 'photo',
imageUrl: '/management/pages/home/sections/our-tools/polaroid-maker/image.jpg',
},
];
export function ShiftableToolsGridSection({
title = 'Our tools',
subtitle = 'Test any tool directly in your browser with your own photos.',
items,
}: ShiftableToolsGridProps) {
const displayItems = items && items.length >= 8 ? items : DEFAULT_TOOL_ITEMS;
const row1 = displayItems.slice(0, 4);
const row2 = displayItems.slice(4, 8);
return (
);
}
function MobileVerticalToolsList({ items }: { items: ToolMediaItem[] }) {
return (
{items.map((item) => (
))}
);
}
function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
const [hoveredId, setHoveredId] = useState(null);
return (
{items.map((item) => {
const isHovered = hoveredId === item.id;
const hasOtherHovered = hoveredId !== null && !isHovered;
// Subtle and gentle sideways expansion (~25-30% wider on hover, no drastic jumping or zoom)
const flexGrowValue = isHovered ? 1.25 : hasOtherHovered ? 0.915 : 1;
return (
setHoveredId(item.id)}
onMouseLeave={() => setHoveredId(null)}
className="flex-1 transition-[flex-grow] duration-500 ease-[cubic-bezier(0.16,1,0.3,1)] flex flex-col min-w-0"
style={{ flexGrow: flexGrowValue }}
>
{item.title}
);
})}
);
}
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);
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);
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)`;
(clipLayerRef.current.style as any).webkitClipPath = `inset(0 ${100 - clamped}% 0 0)`;
}
if (handleRef.current) {
handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
}
}, []);
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]);
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]);
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]);
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);
}
};
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
}
};
// 1. VIDEO
if (item.mediaType === 'video' && item.videoUrl) {
return (
);
}
// 2. COMPARISON
if (item.mediaType === 'comparison' && item.beforeUrl && item.afterUrl) {
return (
{/* Layer 1: Enhanced Image */}

{/* Layer 2: Original Image */}
{/* Slider Divider Line */}
{/* Circular Grab Handle */}
{/* Badges in colored pills */}
Original
Enhanced
);
}
// 3. PHOTO
return (
);
}