Files
webb/src/components/sections/ToolGridSection.tsx
T

621 lines
24 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useState, useMemo, useRef, useEffect } from 'react';
import Link from 'next/link';
import {
Search,
ArrowRight,
Wand2,
Scissors,
Smile,
Palette,
Sparkles,
ChevronLeft,
ChevronRight,
X,
} from 'lucide-react';
import { ToolItem, CategoryItem, AppItem, resolveLocalised } from '@/types';
interface ToolGridSectionProps {
title?: any;
subtitle?: any;
tools?: ToolItem[];
categories?: CategoryItem[];
apps?: AppItem[];
showSearch?: boolean;
showCategoryFilter?: boolean;
limit?: number;
columns?: number;
locale?: string;
}
type ToolMediaConfig =
| { type: 'comparison'; before: string; after: string }
| { type: 'single'; image: string; badge: string };
// Media resolver for tool cards
function getToolMedia(tool: ToolItem): ToolMediaConfig {
const example = tool.content?.examples?.[0];
// Specific bespoke high-res local media
if (tool.slug === 'background-remover') {
return {
type: 'single',
image: '/management/pages/home/sections/our-tools/background-remover/poster.jpg',
badge: 'Alpha Matting',
};
}
if (tool.slug === 'polaroid-maker') {
return {
type: 'single',
image: '/management/pages/home/sections/our-tools/polaroid-maker/image.jpg',
badge: 'Vintage Polaroid',
};
}
const localDefaults: Record<string, { before: string; after: string }> = {
'blemish-remover': {
before: '/management/pages/home/sections/our-tools/blemish-remover/before.jpg',
after: '/management/pages/home/sections/our-tools/blemish-remover/after.jpg',
},
'teeth-whitener': {
before: '/management/pages/home/sections/our-tools/teeth-whitener/before.jpg',
after: '/management/pages/home/sections/our-tools/teeth-whitener/after.jpg',
},
'photo-enhancer': {
before: '/management/pages/home/sections/our-tools/photo-enhancer/before.jpg',
after: '/management/pages/home/sections/our-tools/photo-enhancer/after.jpg',
},
'old-photo-restorer': {
before: '/management/pages/home/sections/our-tools/old-photo-restorer/before.jpg',
after: '/management/pages/home/sections/our-tools/old-photo-restorer/after.jpg',
},
'colorization': {
before: '/management/pages/home/sections/our-tools/colorization/before.jpg',
after: '/management/pages/home/sections/our-tools/colorization/after.jpg',
},
'cartoon-avatar': {
before: '/management/pages/home/sections/our-tools/cartoon-avatar/before.jpg',
after: '/management/pages/home/sections/our-tools/cartoon-avatar/after.jpg',
},
};
const matched = localDefaults[tool.slug];
if (matched) {
return {
type: 'comparison',
before: matched.before,
after: matched.after,
};
}
if (example && example.before && example.after) {
return {
type: 'comparison',
before: example.before,
after: example.after,
};
}
return {
type: 'single',
image: example?.after || '/management/pages/home/sections/our-tools/blemish-remover/after.jpg',
badge: 'AI Studio',
};
}
// Category design themes
const CATEGORY_THEMES: Record<
string,
{
icon: React.ReactNode;
color: string;
badgeBg: string;
border: string;
}
> = {
'portrait-retouch': {
icon: <Smile className="w-5 h-5 text-rose-500" />,
color: 'text-rose-600',
badgeBg: 'bg-rose-50 text-rose-700 border-rose-200',
border: 'border-rose-100',
},
'restoration': {
icon: <Sparkles className="w-5 h-5 text-indigo-500" />,
color: 'text-indigo-600',
badgeBg: 'bg-indigo-50 text-indigo-700 border-indigo-200',
border: 'border-indigo-100',
},
'background-cutout': {
icon: <Scissors className="w-5 h-5 text-emerald-500" />,
color: 'text-emerald-600',
badgeBg: 'bg-emerald-50 text-emerald-700 border-emerald-200',
border: 'border-emerald-100',
},
'creative-art': {
icon: <Palette className="w-5 h-5 text-amber-500" />,
color: 'text-amber-600',
badgeBg: 'bg-amber-50 text-amber-700 border-amber-200',
border: 'border-amber-100',
},
};
// Interactive Before/After Card Image Component
function ToolCardImage({ tool, toolName }: { tool: ToolItem; toolName: string }) {
const [splitPos, setSplitPos] = useState<number>(50);
const media = getToolMedia(tool);
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = e.clientX - rect.left;
const pct = Math.max(5, Math.min(95, (x / rect.width) * 100));
setSplitPos(pct);
};
const handleMouseLeave = () => {
setSplitPos(50);
};
if (media.type === 'single') {
return (
<div className="relative w-full aspect-[4/3] rounded-2xl overflow-hidden bg-slate-950 select-none group/img shadow-inner">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={media.image}
alt={toolName}
loading="lazy"
decoding="async"
className="absolute inset-0 w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
/>
{/* Floating badge */}
<span className="absolute bottom-2.5 right-2.5 bg-blue-600/90 backdrop-blur-md text-[9px] font-mono font-bold uppercase tracking-wider text-white px-2 py-0.5 rounded-md border border-white/20 pointer-events-none shadow-sm">
{media.badge}
</span>
<span className="absolute top-2.5 right-2.5 bg-white/90 backdrop-blur-md text-[9.5px] font-mono font-bold text-slate-800 px-2.5 py-0.5 rounded-full border border-slate-200 pointer-events-none shadow-sm opacity-0 group-hover:opacity-100 transition-opacity duration-200">
Try Tool ↗
</span>
</div>
);
}
return (
<div
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
className="relative w-full aspect-[4/3] rounded-2xl overflow-hidden bg-slate-950 select-none cursor-ew-resize group/img shadow-inner"
>
{/* Enhanced / After Image (Underneath) */}
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={media.after}
alt={`${toolName} after`}
loading="lazy"
decoding="async"
className="absolute inset-0 w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
/>
{/* Original / Before Image (Clipped overlay) */}
<div
className="absolute inset-0 overflow-hidden"
style={{
clipPath: `polygon(0 0, ${splitPos}% 0, ${splitPos}% 100%, 0 100%)`,
}}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={media.before}
alt={`${toolName} before`}
loading="lazy"
decoding="async"
className="absolute inset-0 w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
/>
</div>
{/* Vertical Slider Divider */}
<div
className="absolute top-0 bottom-0 w-[2px] bg-white shadow-[0_0_8px_rgba(0,0,0,0.6)] pointer-events-none"
style={{ left: `${splitPos}%`, transform: 'translateX(-50%)' }}
>
{/* Sleek handle pill */}
<div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-6 h-6 rounded-full bg-white/95 backdrop-blur-sm text-slate-800 shadow-md flex items-center justify-center border border-slate-300">
<div className="flex items-center gap-[2px]">
<div className="w-0 h-0 border-y-[3px] border-y-transparent border-r-[4px] border-r-slate-700" />
<div className="w-0 h-0 border-y-[3px] border-y-transparent border-l-[4px] border-l-slate-700" />
</div>
</div>
</div>
{/* Floating badges */}
<span className="absolute bottom-2.5 left-2.5 bg-black/60 backdrop-blur-md text-[9px] font-mono font-bold uppercase tracking-wider text-white px-2 py-0.5 rounded-md border border-white/10 pointer-events-none shadow-sm">
Before
</span>
<span className="absolute bottom-2.5 right-2.5 bg-blue-600/90 backdrop-blur-md text-[9px] font-mono font-bold uppercase tracking-wider text-white px-2 py-0.5 rounded-md border border-white/20 pointer-events-none shadow-sm">
CRE AI
</span>
<span className="absolute top-2.5 right-2.5 bg-white/90 backdrop-blur-md text-[9.5px] font-mono font-bold text-slate-800 px-2.5 py-0.5 rounded-full border border-slate-200 pointer-events-none shadow-sm opacity-0 group-hover:opacity-100 transition-opacity duration-200">
Try Tool ↗
</span>
</div>
);
}
// Single Image-Dominant Tool Card Component (~75% Image, Minimal Text)
function ToolCard({ tool, locale }: { tool: ToolItem; locale: string }) {
const toolName = resolveLocalised(tool.name, locale);
const toolSummary = resolveLocalised(tool.summary, locale);
return (
<Link
href={`/tools/${tool.slug}`}
className="group block bg-white rounded-3xl p-3 sm:p-3.5 border border-slate-900/[0.08] hover:border-blue-400 shadow-cream-sm hover:shadow-cream-md transition-all duration-300 hover:-translate-y-1"
>
{/* Image Dominant Preview (~75% of visual area) */}
<ToolCardImage tool={tool} toolName={toolName} />
{/* Minimalist Info Area: Only Title & Subtitle */}
<div className="mt-3.5 px-1 pb-1">
<div className="flex items-center justify-between gap-2">
<h4 className="text-base sm:text-lg font-display font-bold text-slate-900 group-hover:text-blue-600 transition-colors truncate">
{toolName}
</h4>
<div className="w-6 h-6 rounded-full bg-slate-100 flex items-center justify-center text-slate-400 group-hover:bg-blue-600 group-hover:text-white transition-all duration-200 shrink-0 shadow-sm">
<ArrowRight className="w-3.5 h-3.5 group-hover:translate-x-0.5 transition-transform" />
</div>
</div>
<p className="mt-1 text-xs sm:text-sm text-slate-500 line-clamp-2 leading-relaxed">
{toolSummary}
</p>
</div>
</Link>
);
}
export function ToolGridSection({
title,
subtitle,
tools = [],
categories = [],
showSearch = true,
showCategoryFilter = true,
locale = 'en',
}: ToolGridSectionProps) {
const [selectedCategory, setSelectedCategory] = useState<string>('all');
const [searchQuery, setSearchQuery] = useState<string>('');
const railRef = useRef<HTMLDivElement>(null);
// Group tools by category
const categorizedTools = useMemo(() => {
const orderedCategoryIds = [
'portrait-retouch',
'restoration',
'background-cutout',
'creative-art',
];
const catMap = new Map<string, CategoryItem>();
categories.forEach((cat) => catMap.set(cat.id, cat));
return orderedCategoryIds
.map((catId) => {
const cat = catMap.get(catId);
const catTools = tools.filter((t) => t.category === catId);
return {
id: catId,
category: cat,
tools: catTools,
};
})
.filter((group) => group.tools.length > 0);
}, [categories, tools]);
// Filter tools when search query is active
const searchResults = useMemo(() => {
if (!searchQuery.trim()) return null;
const q = searchQuery.toLowerCase();
return tools.filter((t) => {
const name = resolveLocalised(t.name, locale).toLowerCase();
const summary = resolveLocalised(t.summary, locale).toLowerCase();
return name.includes(q) || summary.includes(q);
});
}, [tools, searchQuery, locale]);
// Smooth scroll rail for categories
const scrollRail = (direction: 'left' | 'right') => {
if (railRef.current) {
railRef.current.scrollBy({
left: direction === 'left' ? -220 : 220,
behavior: 'smooth',
});
}
};
// Scroll to category section
const handleCategoryClick = (catId: string) => {
setSelectedCategory(catId);
if (searchQuery) setSearchQuery('');
if (catId === 'all') {
const el = document.getElementById('tools-catalog');
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
} else {
const el = document.getElementById(`category-${catId}`);
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
};
// Active category observer as user scrolls
useEffect(() => {
if (searchQuery) return;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const id = entry.target.id.replace('category-', '');
setSelectedCategory(id);
}
});
},
{
rootMargin: '-30% 0px -60% 0px',
threshold: 0,
}
);
categorizedTools.forEach((group) => {
const el = document.getElementById(`category-${group.id}`);
if (el) observer.observe(el);
});
return () => observer.disconnect();
}, [categorizedTools, searchQuery]);
return (
<section id="tools-catalog" className="py-12 sm:py-16 bg-[#FAF8F5]">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
{/* Section Heading */}
{(title || subtitle) && (
<div className="text-center max-w-3xl mx-auto mb-10 sm:mb-14 space-y-3">
{title && (
<h2 className="text-3xl sm:text-4xl lg:text-5xl font-display font-black text-slate-900 tracking-tight">
{resolveLocalised(title, locale)}
</h2>
)}
{subtitle && (
<p className="text-base sm:text-lg text-slate-600 font-normal">
{resolveLocalised(subtitle, locale)}
</p>
)}
</div>
)}
{/* Sticky Horizontal Scrolling Category Bar & Search */}
{(showCategoryFilter || showSearch) && (
<div className="sticky top-[64px] sm:top-[72px] z-30 mb-12 -mx-4 sm:-mx-6 lg:-mx-8 px-4 sm:px-6 lg:px-8 py-3 bg-[#FAF8F5]/90 backdrop-blur-md border-y border-slate-200/80 shadow-[0_4px_20px_-10px_rgba(0,0,0,0.06)]">
<div className="max-w-7xl mx-auto flex flex-col md:flex-row items-stretch md:items-center justify-between gap-3 sm:gap-4">
{/* Category Rail with Smooth Scrolling */}
{showCategoryFilter && (
<div className="relative flex-1 flex items-center min-w-0">
{/* Left Scroll Arrow */}
<button
onClick={() => scrollRail('left')}
aria-label="Scroll categories left"
className="hidden sm:flex items-center justify-center w-8 h-8 rounded-full bg-white border border-slate-200 text-slate-600 hover:text-slate-900 shadow-sm shrink-0 mr-1.5 transition-colors"
>
<ChevronLeft className="w-4 h-4" />
</button>
{/* Horizontal Scrollable Categories Container */}
<div
ref={railRef}
style={{ WebkitOverflowScrolling: 'touch' }}
className="flex items-center gap-2 overflow-x-auto no-scrollbar scroll-smooth py-1 w-full touch-pan-x"
>
{/* All Categories Pill */}
<button
onClick={() => handleCategoryClick('all')}
className={`shrink-0 flex items-center gap-2 px-4 py-2 rounded-full text-xs font-bold transition-all duration-200 shadow-sm ${
selectedCategory === 'all' && !searchQuery
? 'bg-blue-600 text-white shadow-blue-500/25 ring-2 ring-blue-500/30'
: 'bg-white text-slate-700 hover:text-slate-900 hover:bg-slate-50 border border-slate-200/90'
}`}
>
<span>All Categories</span>
<span
className={`text-[10px] font-mono px-1.5 py-0.2 rounded-full ${
selectedCategory === 'all' && !searchQuery
? 'bg-white/20 text-white'
: 'bg-slate-100 text-slate-600'
}`}
>
{tools.length}
</span>
</button>
{/* Individual Category Pills */}
{categorizedTools.map((group) => {
const isSelected =
selectedCategory === group.id && !searchQuery;
const catName = group.category
? resolveLocalised(group.category.name, locale)
: group.id;
const theme = CATEGORY_THEMES[group.id];
return (
<button
key={group.id}
onClick={() => handleCategoryClick(group.id)}
className={`shrink-0 flex items-center gap-2 px-3.5 py-2 rounded-full text-xs font-bold transition-all duration-200 shadow-sm ${
isSelected
? 'bg-blue-600 text-white shadow-blue-500/25 ring-2 ring-blue-500/30'
: 'bg-white text-slate-700 hover:text-slate-900 hover:bg-slate-50 border border-slate-200/90'
}`}
>
<span className={isSelected ? 'text-white' : ''}>
{theme?.icon || <Wand2 className="w-4 h-4" />}
</span>
<span>{catName}</span>
<span
className={`text-[10px] font-mono px-1.5 py-0.2 rounded-full ${
isSelected
? 'bg-white/20 text-white'
: 'bg-slate-100 text-slate-600'
}`}
>
{group.tools.length}
</span>
</button>
);
})}
</div>
{/* Right Scroll Arrow */}
<button
onClick={() => scrollRail('right')}
aria-label="Scroll categories right"
className="hidden sm:flex items-center justify-center w-8 h-8 rounded-full bg-white border border-slate-200 text-slate-600 hover:text-slate-900 shadow-sm shrink-0 ml-1.5 transition-colors"
>
<ChevronRight className="w-4 h-4" />
</button>
</div>
)}
{/* Integrated Search Input */}
{showSearch && (
<div className="relative w-full md:w-72 shrink-0">
<Search className="w-4 h-4 absolute left-3.5 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none" />
<input
type="text"
placeholder="Search AI photo tools..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="w-full pl-10 pr-9 py-2 rounded-full text-xs sm:text-sm border border-slate-200 bg-white text-slate-900 placeholder-slate-400 focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500 shadow-sm"
/>
{searchQuery && (
<button
onClick={() => setSearchQuery('')}
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600"
>
<X className="w-3.5 h-3.5" />
</button>
)}
</div>
)}
</div>
</div>
)}
{/* Content: Categorized Sections or Search Results */}
{searchResults ? (
/* Search Results View */
<div className="space-y-6">
<div className="flex items-center justify-between pb-3 border-b border-slate-200">
<h3 className="text-xl font-display font-bold text-slate-900">
Found {searchResults.length}{' '}
{searchResults.length === 1 ? 'tool' : 'tools'} matching &quot;
{searchQuery}&quot;
</h3>
<button
onClick={() => setSearchQuery('')}
className="text-xs font-mono font-bold text-blue-600 hover:text-blue-700 underline"
>
Clear Search
</button>
</div>
{searchResults.length > 0 ? (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{searchResults.map((tool) => (
<ToolCard key={tool.id} tool={tool} locale={locale} />
))}
</div>
) : (
<div className="text-center py-16 text-slate-500 space-y-3 font-mono">
<p className="font-bold text-slate-800 text-base">
No photo tools found matching &quot;{searchQuery}&quot;
</p>
<p className="text-xs text-slate-500">
Try searching for &quot;blemish&quot;, &quot;restoration&quot;,
&quot;background&quot;, or &quot;avatar&quot;.
</p>
<button
onClick={() => setSearchQuery('')}
className="mt-2 inline-flex items-center px-4 py-2 rounded-full bg-blue-600 text-white text-xs font-bold shadow-sm"
>
View All Tools
</button>
</div>
)}
</div>
) : (
/* Categorized Tools Sections */
<div className="space-y-14 sm:space-y-16">
{categorizedTools.map((group) => {
const theme = CATEGORY_THEMES[group.id];
const catName = group.category
? resolveLocalised(group.category.name, locale)
: group.id;
const catDesc = group.category
? resolveLocalised(group.category.description, locale)
: '';
return (
<div
key={group.id}
id={`category-${group.id}`}
className="scroll-mt-36 space-y-6"
>
{/* Category Header */}
<div className="flex flex-col sm:flex-row sm:items-end justify-between gap-3 pb-4 border-b border-slate-200/90">
<div className="flex items-center gap-3">
<div
className={`w-11 h-11 rounded-2xl flex items-center justify-center bg-white shadow-sm border border-slate-200/80 shrink-0`}
>
{theme?.icon || <Wand2 className="w-5 h-5 text-blue-600" />}
</div>
<div>
<div className="flex items-center gap-2.5">
<h3 className="text-xl sm:text-2xl font-display font-black text-slate-900 tracking-tight">
{catName}
</h3>
<span
className={`text-xs font-mono font-bold px-2.5 py-0.5 rounded-full border ${
theme?.badgeBg ||
'bg-slate-100 text-slate-700 border-slate-200'
}`}
>
{group.tools.length}{' '}
{group.tools.length === 1 ? 'tool' : 'tools'}
</span>
</div>
{catDesc && (
<p className="text-xs sm:text-sm text-slate-500 mt-0.5">
{catDesc}
</p>
)}
</div>
</div>
</div>
{/* Category Tools Grid with Image-Dominant Cards */}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3 gap-6 sm:gap-7">
{group.tools.map((tool) => (
<ToolCard key={tool.id} tool={tool} locale={locale} />
))}
</div>
</div>
);
})}
</div>
)}
</div>
</section>
);
}